Note that there are some explanatory texts on larger screens.

plurals
  1. POMost efficient way to search multi-tree data structure
    primarykey
    data
    text
    <p>I am wondering if there is a more efficient way to search through a multi-tree than what I have been doing. I recently built a multi-tree data structure for a project that is illustrated below. <img src="https://i.stack.imgur.com/D7fql.png" alt="Multi-Tree data structure using ArrayLists"></p> <p>The cave would hold the parties in an array list. The different parties would hold different creatures. Different creatures would hold different items.</p> <p>I needed a way to search the whole tree to match an object to an attribute it had such as index. This is a snippet of my program that searches through every ArrayList to see if either a party, creature, treasure, or artifact matches an int I call index.</p> <p><strong>Edit</strong>(Explaining my code) The classes Party, Creature, Treasure, and Artifact all have attributes such as index, name, type, etc. The multi-tree has the Cave set as the root note. Cave has an ArrayList that can contain a number of Party objects. Each Party has an ArrayList that can contain a a number of Creature objects. Each creature has two array lists one to hold Artifact objects and one for Treasure objects. </p> <p>Below I am searching to see which party, creature, artifact, or treasure holds the specific index that I am searching for. I do this by iterating over parties, with each party I look in the creatures, and with each creature I look in the artifacts and treasures. That is why I have so many for loops inside of for loops :(.</p> <pre><code>case 0 : int index = Integer.parseInt( stat ); for ( Party p : SorcerersCave.theCave.parties ) { if ( index == p.getIndex()) { generateInterface.theGame.printOutput( "\t" + p ); break; } else { for ( Creature c : p.members ){ if ( index == c.getIndex() ){ generateInterface.theGame.printOutput( "\t" + c ); break; } else { for ( Treasure t : c.inventory ){ if ( index == t.getIndex() ){ generateInterface.theGame.printOutput( "\t" + t ); break; } } for ( Artifact a : c.artifacts ){ if ( index == a.getIndex() ){ generateInterface.theGame.printOutput( "\t" + a ); break; } } } } } } </code></pre> <p>I feel this code is too complicated and is difficult to follow. The code works but its an ugly stain on my otherwise really good looking code. I've been looking around to find a better way to do this, or even a way to improve it.</p> <p>Note* Project requirements forbid us from placing every object in the same ArrayList.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload