【发布时间】:2012-03-11 13:22:24
【问题描述】:
我已经按照给定的here 并通过引用作者1 的GitHub 存储库中给出的源代码在Java 中实现了一个通用(n 元)树。我想在java中使用Iterator实现n叉树的前序和后序遍历。因此,只要有节点,方法 hasNext() 将返回 true,而方法 next() 将返回将在前/后遍历中出现的下一个节点。
我正在尝试遵循 question 中给出的伪代码,但我无法将其插入到下面编写的 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
}
树类:
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 类:
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;
}
}
我知道随着树的深度使用尽可能多的块是完全错误的,但堆栈逻辑对我来说并不直观。如果有人可以指导我,我将不胜感激。
谢谢, 柴坦尼亚
【问题讨论】:
-
我不明白你的问题。您已经(或复制)了一个
next()方法。你有什么问题? -
方法高度不正确。我正在使用 2 个 for 循环,它仅适用于 n-ary 树中的 3 深度。因此,它根本不是通用的。
-
你必须使用递归。如果你用谷歌搜索“递归树遍历”,我相信你会找到类似的例子。
-
我想我有一个使用递归的示例代码。我实际上想试一试迭代方法。因为,我被卡住了,所以我在这里发布了这个问题。
-
如果您要迭代地执行此操作,则必须跟踪树的顺序(n 元中的 n)
标签: java data-structures iterator