Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Could you maybe share some more of your code and your input data, to get a better understanding of what you are trying to do?</p> <p>This Stack Overflow discussion on <strong>Trie data structures in Java</strong> might be helpful: <a href="https://stackoverflow.com/questions/3806788/trie-data-structures-java?rq=1">Trie data structures - Java</a>. I found the following link (from one of the answers) quite useful: <a href="https://forums.oracle.com/forums/thread.jspa?messageID=8787521" rel="nofollow noreferrer">https://forums.oracle.com/forums/thread.jspa?messageID=8787521</a>.</p> <p>EDIT: with help from <a href="https://forums.oracle.com/forums/thread.jspa?messageID=8787521" rel="nofollow noreferrer">https://forums.oracle.com/forums/thread.jspa?messageID=8787521</a> and <a href="https://stackoverflow.com/questions/3522454/java-tree-data-structure?rq=1">Java tree data-structure?</a>, I created the following code:</p> <pre><code>public Stack&lt;List&lt;Character&gt;&gt; createTreeAndGetAllWords() { // Create the tree. final Tree&lt;Character&gt; rootTree = new Tree&lt;Character&gt;('*'); final Tree&lt;Character&gt; cTree = rootTree.addChild(new Tree&lt;Character&gt;('c')); final Tree&lt;Character&gt; aTree = cTree.addChild(new Tree&lt;Character&gt;('a')); aTree.addChild(new Tree&lt;Character&gt;('r')); aTree.addChild(new Tree&lt;Character&gt;('t')); final Tree&lt;Character&gt; dTree = rootTree.addChild(new Tree&lt;Character&gt;('d')); final Tree&lt;Character&gt; oTree = dTree.addChild(new Tree&lt;Character&gt;('o')); oTree.addChild(new Tree&lt;Character&gt;('g')); // Traverse the tree. return getAllWords(rootTree); } private Stack&lt;List&lt;Character&gt;&gt; getAllWords(final Tree&lt;Character&gt; tree) { final Stack&lt;List&lt;Character&gt;&gt; listStack = new Stack&lt;List&lt;Character&gt;&gt;(); for (final Tree&lt;Character&gt; child : tree.getChildren()) { listStack.push(new ArrayList&lt;Character&gt;()); traverseSubtree(child, listStack); } return listStack; } private void traverseSubtree(final Tree&lt;Character&gt; tree, final Stack&lt;List&lt;Character&gt;&gt; listStack) { final List&lt;Character&gt; currentWord = listStack.pop(); if (tree.hasChildren()) { for (final Tree&lt;Character&gt; child : tree.getChildren()) { final List&lt;Character&gt; extendedWord = new ArrayList&lt;Character&gt;(currentWord); extendedWord.add(tree.getValue()); listStack.push(extendedWord); traverseSubtree(child, listStack); } } else { currentWord.add(tree.getValue()); listStack.push(currentWord); } } public class Tree&lt;T&gt; { private T value; private List&lt;Tree&lt;T&gt;&gt; children; public Tree(T value) { this.value = value; this.children = new ArrayList&lt;Tree&lt;T&gt;&gt;(); } public T getValue() { return value; } public boolean hasChildren() { return children.size() &gt; 0; } public Tree&lt;T&gt; addChild(Tree&lt;T&gt; child) { children.add(child); return child; } public List&lt;Tree&lt;T&gt;&gt; getChildren() { return children; } } </code></pre> <p>When I call createTreeAndGetAllWords, it returns the words I expected as three lists of characters: [c, a, r], [c, a, t] and [d, o, g].</p> <pre><code>for (final List&lt;Character&gt; word : createTreeAndGetAllWords()) { System.out.println("word = " + word); } </code></pre> <p>Cheers, Freek</p>
 

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