本地 class 变量在每次调用时始终设置为空

Local class variables are always set to null on each time call

所以,我写了一个打印二叉搜索树的方法,但是我想把它保存在一个String类型的变量中,不再打印了,所以我尝试将结果保存在一个局部变量中,然而,每次我调用该函数时,它都没有正确附加结果,因为每次我调用它时,它都被设置为 null。这是我的代码:

   static class TreeNode {
    private int data;
    private TreeNode leftChild;
    private TreeNode rightChild;
    private String outPut;

    public TreeNode(int data) {
        this.data = data;
    }


    public void add(int data) {
        if (data >= this.data) {
            if (this.rightChild == null) {
                this.rightChild = new TreeNode(data);
            } else {
                this.rightChild.add(data);
            }
        } else {
            if (this.leftChild == null) {
                this.leftChild = new TreeNode(data);
            } else {
                this.leftChild.add(data);
            }
        }
    }

    public void addOutput(String s){
        this.outPut=this.outPut+s;
    }

    public void print() {
        print("", "", "", "");
    }

    public void print(String prefix, String left, String mid, String right) {
        String indent = " ".repeat(String.valueOf(data).length());
        if (leftChild != null) {
            leftChild.print(prefix + left + indent, " ", "┌", "│");
        }
        System.out.println(prefix + mid + data
                + " ┐┘┤".charAt((leftChild  != null ? 2 : 0)
                + (rightChild != null ? 1 : 0)));

        //Here i added method to append to a local string
        addOutput(prefix + mid + data
                + " ┐┘┤".charAt((leftChild  != null ? 2 : 0)
                + (rightChild != null ? 1 : 0)));
        if (rightChild != null) {
            rightChild.print(prefix + right + indent, "│", "└", " ");
        }

    }

    public int getData() {
        return data;
    }

    public void setLeftChild(TreeNode leftChild) {
        this.leftChild = leftChild;
    }

    public void setRightChild(TreeNode rightChild) {
        this.rightChild = rightChild;
    }

    public TreeNode getLeftChild() {
        return leftChild;
    }

    public TreeNode getRightChild() {
        return rightChild;
    }

}

static class BinaryTree{

  private TreeNode root;

  public void pprint() {
      if (root != null) {
          root.print();
      }
  }

  public void insert(int data){
        if(root == null){
            this.root = new TreeNode(data);
        }else{
            root.add(data);
        }
    }}

打印输出应该如下所示:

然而,执行后输出字符串始终为空。

那么,如何将字符串中的结果保存到局部变量?

您不应在字段中收集字符串。相反,您应该使方法 return 成为字符串。

像这样:

    public String toString() {
        return toString("", "", "", "");
    }

    public String toString(String prefix, String left, String mid, String right) {
        String indent = " ".repeat(String.valueOf(data).length());
        String treeRepr = prefix + mid + data 
                        + " ┐┘┤".charAt((leftChild  != null ? 2 : 0) 
                                      + (rightChild != null ? 1 : 0));
        if (leftChild != null) {
            treeRepr = leftChild.toString(prefix + left + indent, " ", "┌", "│")
                     + "\n" 
                     + treeRepr;
        }
        
        if (rightChild != null) {
            treeRepr = treeRepr
                     + "\n"
                     + rightChild.toString(prefix + right + indent, "│", "└", " ");
        }
        return treeRepr;
    }

如果您随后想要打印树,而不是执行 root.print(),您可以调用 toString 方法:

    System.out.println(root.toString());