Note that there are some explanatory texts on larger screens.

plurals
  1. POPre-order/Post-order iterative traversal of n-ary tree using Iterator pattern
    primarykey
    data
    text
    <p>I have implemented a Generic (n-ary) tree in Java as given <a href="http://vivin.net/2010/01/30/generic-n-ary-tree-in-java/" rel="nofollow noreferrer">here</a> and by referencing the source given on the <a href="https://github.com/vivin/GenericTree" rel="nofollow noreferrer">GitHub</a> repository of the author <a href="http://vivin.net/2010/01/30/generic-n-ary-tree-in-java/" rel="nofollow noreferrer">1</a>. I want to implement a pre-order and post-order traversal of the n-ary tree using the Iterator in java. Thus, the methods hasNext() will return a true whenever there is a node and the method next() will return the node which would be present next in the pre/post-order traversal.</p> <p>I am trying to follow the pseudo-codes given in this <a href="https://stackoverflow.com/questions/5987867/traversing-a-n-ary-tree-without-using-recurrsion">question</a> but I am not able to plug it in the next method of Iterator which I've written below</p> <pre><code>public class DepthFirstIterator&lt;T&gt; implements Iterator&lt;TreeNode&lt;T&gt;&gt; { private Stack&lt;TreeNode&lt;T&gt;&gt; dfsStack; private Tree&lt;T&gt; tree; private TreeNode&lt;T&gt; start; public DepthFirstIterator(Tree&lt;T&gt; tree) { this.tree = tree; this.dfsStack = new Stack&lt;TreeNode&lt;T&gt;&gt;(); if (!this.tree.isEmpty()) this.dfsStack.push(this.tree.getRoot()); } public DepthFirstIterator(Tree&lt;T&gt; tree, TreeNode&lt;T&gt; startNode) { this.tree = tree; this.dfsStack = new Stack&lt;TreeNode&lt;T&gt;&gt;(); if (startNode != null) this.dfsStack.push(startNode); } public boolean hasNext() { return (!this.dfsStack.isEmpty()); } public TreeNode&lt;T&gt; next() { // Iterative code to obtain pre/post-order traversal } public void remove() { // Do nothing } </code></pre> <p>Tree Class:</p> <pre><code>public class Tree&lt;T&gt; { private TreeNode&lt;T&gt; root; public TreeNode&lt;T&gt; getRoot() { return this.root; } public void setRoot(TreeNode&lt;T&gt; element) { this.root = element; } public boolean isEmpty() { return (this.root == null); } public int size() { if (isEmpty()) return 0; else return getNumberOfNodes(root) + 1; } private int getNumberOfNodes(TreeNode&lt;T&gt; node) { int num = 0; Stack&lt;TreeNode&lt;T&gt;&gt; nodeStack = new Stack&lt;TreeNode&lt;T&gt;&gt;(); nodeStack.push(node); while (!nodeStack.isEmpty()) { TreeNode&lt;T&gt; top = nodeStack.pop(); for (TreeNode&lt;T&gt; child : top.getChildren()) { num++; nodeStack.push(child); } } return num; } } </code></pre> <p>TreeNode class:</p> <pre><code>public class TreeNode&lt;T&gt; { private T data; private List&lt;TreeNode&lt;T&gt;&gt; children; private TreeNode&lt;T&gt; parent; public TreeNode() { super(); children = new ArrayList&lt;TreeNode&lt;T&gt;&gt;(); parent = null; } public TreeNode(T data) { this(); setData(data); } public void setData(T data) { this.data = data; } public T getData() { return this.data; } public List&lt;TreeNode&lt;T&gt;&gt; getChildren() { return children; } public void setChildren(List&lt;TreeNode&lt;T&gt;&gt; children) { for (TreeNode&lt;T&gt; child : children) child.parent = this; this.children = children; } public void addChild(TreeNode&lt;T&gt; child) { child.parent = this; children.add(child); } public void insertChildAt(int index, TreeNode&lt;T&gt; child) throws IndexOutOfBoundsException { child.parent = this; children.add(index, child); } public TreeNode&lt;T&gt; getChildAt(int index) throws IndexOutOfBoundsException { return children.get(index); } public void removeChildAt(int index) throws IndexOutOfBoundsException { children.remove(index); } public void removeChildren() { children.clear(); } public int getNumberOfChildren() { return children.size(); } public String toString() { return getData().toString(); } public boolean hasChildren() { return (getChildren().size() &gt; 0); } public TreeNode&lt;T&gt; getParent() { return this.parent; } } </code></pre> <p>I know that using as-many for blocks as the depth of the tree goes is totally wrong, but the stack logic was not being intuitive to me. If somebody,could please guide me I would really appreciate it.</p> <p>Thanks, Chaitanya</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.
 

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