Reputation: 1601
I have implemented a Generic (n-ary) tree in Java as given here and by referencing the source given on the GitHub repository of the author 1. 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.
I am trying to follow the pseudo-codes given in this question but I am not able to plug it in the next method of Iterator which I've written below
public class DepthFirstIterator<T> implements Iterator<TreeNode<T>> {
private Stack<TreeNode<T>> dfsStack;
private Tree<T> tree;
private TreeNode<T> start;
public DepthFirstIterator(Tree<T> tree) {
this.tree = tree;
this.dfsStack = new Stack<TreeNode<T>>();
if (!this.tree.isEmpty())
this.dfsStack.push(this.tree.getRoot());
}
public DepthFirstIterator(Tree<T> tree, TreeNode<T> startNode) {
this.tree = tree;
this.dfsStack = new Stack<TreeNode<T>>();
if (startNode != null)
this.dfsStack.push(startNode);
}
public boolean hasNext() {
return (!this.dfsStack.isEmpty());
}
public TreeNode<T> next() {
// Iterative code to obtain pre/post-order traversal
}
public void remove() {
// Do nothing
}
Tree Class:
public class Tree<T> {
private TreeNode<T> root;
public TreeNode<T> getRoot() {
return this.root;
}
public void setRoot(TreeNode<T> 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<T> node) {
int num = 0;
Stack<TreeNode<T>> nodeStack = new Stack<TreeNode<T>>();
nodeStack.push(node);
while (!nodeStack.isEmpty()) {
TreeNode<T> top = nodeStack.pop();
for (TreeNode<T> child : top.getChildren()) {
num++;
nodeStack.push(child);
}
}
return num;
}
}
TreeNode class:
public class TreeNode<T> {
private T data;
private List<TreeNode<T>> children;
private TreeNode<T> parent;
public TreeNode() {
super();
children = new ArrayList<TreeNode<T>>();
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<TreeNode<T>> getChildren() {
return children;
}
public void setChildren(List<TreeNode<T>> children) {
for (TreeNode<T> child : children)
child.parent = this;
this.children = children;
}
public void addChild(TreeNode<T> child) {
child.parent = this;
children.add(child);
}
public void insertChildAt(int index, TreeNode<T> child)
throws IndexOutOfBoundsException {
child.parent = this;
children.add(index, child);
}
public TreeNode<T> 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() > 0);
}
public TreeNode<T> getParent() {
return this.parent;
}
}
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.
Thanks, Chaitanya
Upvotes: 2
Views: 7196
Reputation: 389
Stack<Treenode<T>> preorder;
/*in constructor: */
preorder.push(tree.getRoot());
//Preorder next
public TreeNode<T> next() {
Treenode<t> ret = preorder.pop();
for (int i = ret.getChildren().size()-1 ; i>=0; i--) {
preorder.push(ret.getChildAt(i));
}
return ret;
}
Upvotes: 4