在LCRS树中预订迭代器

时间:2015-11-17 12:56:37

标签: java tree iterator

我有一个LCRS tree,我想实现Post Order迭代器。我已经实现了预订和前面,这是我的代码:

public class FrontIterator<E> implements Iterator<Position<E>> {

private final Deque<Position<E>> nodeStack;
private final Tree<E> tree;

public FrontIterator(Tree<E> tree, Position<E> start) {
    nodeStack = new ArrayDeque<Position<E>>();
    this.tree = tree;
    nodeStack.push(start);
}

public FrontIterator(Tree<E> tree) {
    nodeStack = new ArrayDeque<Position<E>>();
    this.tree = tree;
    nodeStack.push(tree.root());
}

@Override
public boolean hasNext() {
    return !nodeStack.isEmpty();
}

@Override
public Position<E> next() {
    Position<E> next = null;
    Deque<Position<E>> nodeChildren = new ArrayDeque<Position<E>>();
    while (!nodeStack.isEmpty() && next == null) {
        Position<E> node = nodeStack.pop();
        if (tree.isLeaf(node)) {
            next = node;
        }
        for (Position<E> child : tree.children(node)) {
            nodeChildren.push(child);
        }
        while (!nodeChildren.isEmpty()) {
            nodeStack.push(nodeChildren.pop());
        }
    }
    return next;
}



public class PreorderIterator<E> implements Iterator<Position<E>> {

private final Deque<Position<E>> nodeStack;
private final Tree<E> tree;

public PreorderIterator(Tree<E> tree, Position<E> start) {
    nodeStack = new ArrayDeque<Position<E>>();
    this.tree = tree;
    nodeStack.push(start);
}

public PreorderIterator(Tree<E> tree) {
    nodeStack = new ArrayDeque<Position<E>>();
    this.tree = tree;
    nodeStack.push(tree.root());
}

@Override
public boolean hasNext() {
    return !nodeStack.isEmpty();
}

@Override
public Position<E> next() {
    Position<E> aux = nodeStack.pop();
    Deque<Position<E>> nodeChildren = new ArrayDeque<Position<E>>();
    for (Position<E> node : tree.children(aux)) {
        nodeChildren.push(node);
    }
    while (!nodeChildren.isEmpty()) {
        nodeStack.push(nodeChildren.pop());
    }
    return aux;
}

我用二叉树检查了例子,但我不明白push&#34; left&#34;和&#34;对&#34;孩子。

解决

public class PostorderIterator<E> implements Iterator<Position<E>> {

private final Deque<Position<E>> nodeStack;
private final Tree<E> tree;
private final List<Position<E>> visited;

public PostorderIterator(Tree<E> tree, Position<E> start) {
    nodeStack = new ArrayDeque<Position<E>>();
    this.tree = tree;
    this.visited = new ArrayList<Position<E>>();
    nodeStack.push(start);
}

public PostorderIterator(Tree<E> tree) {
    nodeStack = new ArrayDeque<Position<E>>();
    this.tree = tree;
    this.visited = new ArrayList<Position<E>>();
    nodeStack.push(tree.root());
}

@Override
public boolean hasNext() {
    return !nodeStack.isEmpty();
}

@Override
public Position<E> next() {
    Position<E> next = null;
    Deque<Position<E>> nodeChildren = new ArrayDeque<Position<E>>();
    while (!nodeStack.isEmpty() && next == null) {
        Position<E> node = nodeStack.pop();
        if (this.visited.contains(node)) {
            next = node;
            break;
        }
        if (tree.isLeaf(node)) {
            next = node;
        } else {
            nodeStack.push(node);
        }
        for (Position<E> child : tree.children(node)) {
            nodeChildren.push(child);
        }
        while (!nodeChildren.isEmpty()) {
            nodeStack.push(nodeChildren.pop());
        }
        this.visited.add(node);
    }
    return next;
}

0 个答案:

没有答案