【问题标题】:Threaded Binary Search Tree Java线程二叉搜索树 Java
【发布时间】:2013-11-10 03:07:43
【问题描述】:

我已经设法通过它的插入方法创建了一个线程二叉搜索树。我现在需要遍历树并按顺序打印。我有有效的代码,但我使用了一个布尔标志来确定我是否已经打印了那个特定的节点。对于此分配,它不能是递归的。我想知道是否有一种可能的方法可以将所有布尔标志完全清除为 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();
    }
}

【问题讨论】:

    标签: java binary-search-tree inorder


    【解决方案1】:

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

    顺便说一句,将布尔表达式与常量值truefalse 进行比较是很糟糕的风格。例如

    while( e == false )
    

    更有效地表达为

    while( !e )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-23
      • 1970-01-01
      • 2023-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多