[英]Pre-order/Post-order iterative traversal of n-ary tree using Iterator pattern
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 . 我已经在Java中实现通用(n元)树给出这里通过引用给定源GitHub的作者库1 。 I want to implement a pre-order and post-order traversal of the n-ary tree using the Iterator in java. 我想使用Java中的Iterator来实现对n元树的预遍历和后遍历。 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. 因此,只要有节点,hasNext()方法将返回true,next()方法将返回在前/后遍历中接下来出现的节点。
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 我试图遵循此问题中给出的伪代码,但无法将其插入下面编写的Iterator的下一个方法中
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: TreeNode类:
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 谢谢,Chaitanya
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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.