使用迭代器模式对n元树进行预订/后序迭代遍历

时间:2012-02-21 08:04:16

标签: java data-structures iterator

我已经在Java中使用here实现了一个通用(n-ary)树,并引用了作者GitHub1存储库中提供的源代码。我想使用java中的Iterator实现n-ary树的预订和后序遍历。因此,方法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;
    }
}

我知道使用as-many作为树的深度是完全错误的,但堆栈逻辑对我来说并不直观。如果有人,请指导我,我真的很感激。

谢谢, 切塔尼亚

1 个答案:

答案 0 :(得分:4)

Stack<Treenode<T>> preorder;
构造函数中的

/ *:* /

preorder.push(tree.getRoot());

//预订下一个

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;

}