如何解决二叉树打印问题

How to solve problem with binary tree print

我做了一段代码,输入整数,例如:123 11 200 1 100 150 2000,需要输出一个二叉树,形式如下:

      ┌1
   ┌11┤
   │  └100
123┤
   │   ┌150
   └200┤
       └2000

但在我的代码输出中是:

0┐
 │      ┌1 
 │   ┌11┤
 │   │  └100 
 └123┤
     │   ┌150 
     └200┤
         └2000

在他的根部出现了一个零,我不知道为什么。 这是我的代码。我认为问题出在 add() 方法中,但不知道如何解决。我将不胜感激。

    public class TreeNode extends BinaryTree implements PrintableTree {
    private int i;
    private TreeNode leftChild;
    private TreeNode rightChild;

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

    public TreeNode() {

    }

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

    public int getI() {
        return i;
    }

    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;
    }



    @Override
    public String prettyPrint() {

        StringBuilderPlus builder = new StringBuilderPlus();
        prettyPrint(builder, "", "", "", "");

        return builder.toString();
    }

   public void print() {
       StringBuilderPlus res = new StringBuilderPlus();
       prettyPrint(res, "", "", "", "");
    }

    public void prettyPrint(StringBuilderPlus result, String prefix, String left, String mid, String right) {
        String indent = " ".repeat(String.valueOf(i).length());
        if (leftChild != null) {
            leftChild.prettyPrint(result, prefix + left + indent, " ", "┌", "│");
        }
        result.appendLine(prefix + mid + i
                + " ┐┘┤".charAt((leftChild != null ? 2 : 0)
                + (rightChild != null ? 1 : 0)));
        if (rightChild != null) {
            rightChild.prettyPrint(result, prefix + right + indent, "│", "└", " ");
        }
    }
}
public class StringBuilderPlus {

    private StringBuilder sb;

    public StringBuilderPlus(){
        sb = new StringBuilder();
    }

    public void append(String str)
    {
        sb.append(str != null ? str : "");
    }

    public void appendLine(String str)
    {
        sb.append(str != null ? str : "").append(System.getProperty("line.separator"));
    }

    public String toString()
    {
        return sb.toString();
    }
}
class BinaryTree {
    private TreeNode root;

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

    public void add(int i) {
        if (root == null) {
            root = new TreeNode(i);
        } else {
            root.add(i);
        }
    }
}
public interface PrintableTree {

    void add(int i);
    String prettyPrint();

    static PrintableTree getInstance() {
        return new TreeNode();
    }
}

感谢您发布其余代码。您的问题与方法 getInstance 中的 PrintableTree 接口有关。你 return 一个新的 TreeNode(它用 0 初始化,因为 int 是原始类型,不能是 null)。然后您将其余节点添加到带有 0 的原始节点。您很可能想要 return 一个新的 BinaryTree。但是,您需要更改代码,因为 BinaryTree 未实现 PrintableTree 接口。

以下是修复它所必需的更改:

PrintableTree.java

static PrintableTree getInstance() {
    return new BinaryTree();
}

BinaryTree.java

// notice the added implements
class BinaryTree implements PrintableTree {
// the rest of the class is fine, add this to the end
    @Override
    public String prettyPrint() {
        if (root != null) {
            return root.prettyPrint();
        }
        return "";
    }
}

此外,class TreeNode 不需要扩展 BinaryTree,因为节点不是树(对于包含值和根)。如果您的作业需要此关系,请相应地编辑代码。