1

我已经设法通过它的插入方法创建了一个线程二叉搜索树。我现在需要遍历树并按顺序打印。我有有效的代码,但我使用了一个布尔标志来确定我是否已经打印了那个特定的节点。对于此分配,它不能是递归的。我想知道是否有一种可能的方法可以将所有布尔标志完全清除为 false,因为如果我再次尝试打印,它会并且确实不起作用。有什么建议么?这是我的显示方法的副本。

public void display(){
    Node parent=top;
    Node current=top;
    while (current != null){
        parent = current;
        current = current.getLeft();
    }
    System.out.println(parent);
    current=parent.getRight();
    while(current!= null){
        while(current.isHasLeftThread()==false && current.getLeft().hasBeenHere()==false){
            parent = current;
            current=current.getLeft();
        }
        System.out.println(current);
        current.setBeenHere(true);
        current=current.getRight();
        System.out.println(current);
        current.setBeenHere(true);
        current = current.getRight();
    }
}
4

1 回答 1

0

Collections.newSetFromMap( new IdentityHashMap< Node, Boolean >() )您可以每次都使用新鲜来为访问的节点进行簿记,而不是在Node类本身中包含一个标志。

顺便说一句,将布尔表达式与常量值进行比较,true或者false只是一种糟糕的风格。例如

while( e == false )

更有效地表示为

while( !e )
于 2013-11-10T03:52:32.377 回答