【问题标题】:My binary search tree won't print the values, and I don't know whether its the printing method or the adding method that's wrong我的二叉搜索树不打印值,不知道是打印方法还是加法错误
【发布时间】:2020-05-11 17:33:38
【问题描述】:

我正在尝试创建二叉搜索树,但代码似乎不起作用。 我的 BinaryNode 的构造函数和变量

 private BinaryNode left;
 private BinaryNode right;
 public BinaryNode() {
     val = null;
     left = null;
     right = null;
 }
 public BinaryNode(String a, BinaryNode b, BinaryNode c) {
     val = a;
     left = b;
     right = c;
 }

这是我的 add 和 printPostOrder 方法。 BinaryNode x 是一个以 y 为值的节点将成为其子节点的根。

public BinaryNode add(BinaryNode x, String y) {
        if(x==null) {
            x = new BinaryNode(y,null,null);
            return x;
        }
        if(y.compareTo(x.getVal())<0) {
            BinaryNode temp = x.getLeft();
            temp = add(x.getLeft(),y);
            x.setLeft(x);
        }
            else if(y.compareTo(x.getVal())>0){
                BinaryNode temp = x.getRight();
                temp = add(x.getRight(),y);
                x.setLeft(x);
            }
        return x;
        }
 public void printPostOrder(BinaryNode x) {
        if(x!=null) {
            System.out.print(x.getVal()+" ");
            printPostOrder(x.getRight());
            printPostOrder(x.getLeft());
        }
    }

错误信息 -

    at sun.nio.cs.SingleByte.access$000(Unknown Source)
    at sun.nio.cs.SingleByte$Encoder.encodeArrayLoop(Unknown Source)
    at sun.nio.cs.SingleByte$Encoder.encodeLoop(Unknown Source)
    at java.nio.charset.CharsetEncoder.encode(Unknown Source)
    at sun.nio.cs.StreamEncoder.implWrite(Unknown Source)
    at sun.nio.cs.StreamEncoder.write(Unknown Source)
    at java.io.OutputStreamWriter.write(Unknown Source)
    at java.io.BufferedWriter.flushBuffer(Unknown Source)
    at java.io.PrintStream.write(Unknown Source)
    at java.io.PrintStream.print(Unknown Source)
    at BinarySearchTree.printPostOrder(BinarySearchTree.java:95)
    at BinarySearchTree.printPostOrder(BinarySearchTree.java:97)

最后一行重复多次。 帮助将不胜感激。

【问题讨论】:

  • 请添加完整的堆栈跟踪。
  • 我试过了,但它说的太长了 68689 个字符
  • 如果包含填充树的代码会更容易。

标签: java binary-tree binary-search-tree nodes


【解决方案1】:

您的代码有些混乱且难以理解!这是创建二叉搜索树的简单方法:

defination of bst class:
class BinaryNode {
    String val;
    BinaryNode left, right;
    public BinaryNode() {}
    public BinaryNode(String val) {
        this.val = val;
        this.left = this.right = null;
    }
    // setters - getters of all fields
 }



/* building bst */
private BinaryNode add(BinaryNode root, String val) {
    if(root == null) {
        root = new BinaryNode(val);
    }
    // if it is smaller than root's value, set in left hand
    if(root.getVal().compareTo(val) < 0) {
        root.setLeft(add(root.left, val));
    }
    // if it is greater than root's value, set in right hand
    else if(root.getVal().compareTo(val) > 0){
        root.setRight(add(root.right, val));
    }

    return root;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多