【问题标题】:How do I do a pre-order traversal in Java?如何在 Java 中进行预购遍历?
【发布时间】:2016-04-22 22:46:37
【问题描述】:

我正在尝试使用递归方法在二叉树上编写一个前序遍历算法。这是我所拥有的:

void traverse(BT t) {
        if (t == null){
            return;
        }

        System.out.print(t);  
        traverse(t.left);
        traverse(t.right);
        }

由于某种原因无法编译。我认为问题出在我的其余代码上。完整代码如下:

class ZOrep extends TreeAndRepresentation {
  private int k;
  ZOrep left;  
  ZOrep right;  
  ZOrep( int m, int[] b ) { // given sequence build tree
     super( m, b );
     N = (M-1)/2;
     k  = -1;
     t = build();
    }
  ZOrep( int n, BT t ) { // given tree build sequence
      super(n, t);
      t = build();
      traverse( t );
    }
  BT build() {
      return(a[++k] == 0 ? null : new BT( build(), build() ));
    }

  void traverse(BT t) {
    if (t == null){
        return;
    }

    System.out.print(t);  
    traverse(t.left);
    traverse(t.right);
    }
}

在构建树时(使用我的 ZOrep 方法),我感觉自己遗漏了一些东西。这里还有 BT 类:

class BT {
  BT L; BT R;
  BT( BT l, BT r ) { L = l; R = r; }
}

目前我的编译器说它找不到 t.left 和 t.right 的符号。

【问题讨论】:

    标签: java algorithm compiler-errors binary-tree traversal


    【解决方案1】:

    //Java

    静态字符串树 = "";

    private static void preOrder(HuffTree currentObject) {

        if (currentObject == null) {
            return;
        }
        if (currentObject.filling == null) tree += 1;
        else tree += 0;
        preOrder(currentObject.child0);
        preOrder(currentObject.child1);
    }
    

    }

    //这里的类代码

    导入 java.util.Objects;

    /**

    • 作为类的霍夫曼树 */

    类 HuffTree 实现 Comparable {

    // element filling
    Byte filling;
    // element repeats
    int repeats;
    // zero child
    HuffTree child0;
    // child 1
    HuffTree child1;
    
    /**
     * constructor for tree fathers and leaves
     */
    public HuffTree(Byte filling, int repeats, HuffTree child0, HuffTree child1) {
        // father filling
        this.filling = filling;
        // father repeats
        this.repeats = repeats;
        // zero child
        this.child0 = child0;
        // child 1
        this.child1 = child1;
    }
    
    /**
     * finding difference between our tree's items
     */
    @Override
    public int compareTo(HuffTree currentByte) {
        return currentByte.repeats - repeats;
    }
    
    
    /**
     * take byte code as a string by recursive three search in depth
     */
    public String getCodeForByte(Byte currentByte, String wayToFather) {
        // there is 4 cases:
        if (!Objects.equals(filling, currentByte)) {
            // case 1 - zero child found
            if (child0 != null) {
                // recursive code add for zero child
                String currentWay = child0.getCodeForByte(currentByte, wayToFather + "0");
                // return temporary string
                if (currentWay != null) return currentWay;
            }
            // case 2 - child 1 found. recursive code add for child 1. return temporary string
            if (child1 != null) return child1.getCodeForByte(currentByte, wayToFather + "1");
        }
        // case 3 - correct leaf found. return correct code
        if (Objects.equals(filling, currentByte)) return wayToFather;
        // case 4 - wrong leaf found. return null
        return null;
    }
    

    }

    【讨论】:

    • 请您格式化您的答案中的代码并添加一些解释?谢谢
    【解决方案2】:

    你将什么传递给你的横向函数?如果它是一个BT对象,那么你不能使用left和right,你必须使用L和R。left和right是从BT扩展的对象的一部分,但看起来你正在传递一个BT。

    【讨论】:

      【解决方案3】:

      当编译器说它找不到符号时,这意味着你试图引用的字段不存在。

      看看你的班级BT,这是正确的; BT 没有leftright,它有LR。因此,替换

      traverse(t.left);
      traverse(t.right);
      

      traverse(t.L);
      traverse(t.R);
      

      将解决此问题。

      【讨论】:

        【解决方案4】:

        目前我的编译器说它找不到 t.left 和 t.right 的符号。

        这是因为tBT,它没有leftright

        我建议您决定要调用什么树节点类。是ZOrep 还是BT 并且只使用其中一种,否则会造成混乱。

        System.out.print(t); 
        

        如果你想打印一个 BT,你需要给它添加一个toString() 方法,因为默认不会告诉你任何有用的东西。

        【讨论】:

          猜你喜欢
          • 2014-11-01
          • 2017-03-05
          • 1970-01-01
          • 2012-09-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-08-18
          • 2014-09-18
          相关资源
          最近更新 更多