【问题标题】:How to solve problem with binary tree print如何解决二叉树打印的问题
【发布时间】:2022-01-02 17:45:21
【问题描述】:

我做了一个以整数作为输入的代码,例如: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();
    }
}

【问题讨论】:

  • 0 只能用这段代码部分解释:注意new TreeNode() 的用法(这是i = 0; 可以“潜入”的地方)。
  • 请提供创建树然后调用打印方法的驱动代码。

标签: java printing binary-tree


【解决方案1】:

感谢您发布其余代码。您的问题在于方法getInstance 中的PrintableTree 接口。你返回一个新的TreeNode(它用0初始化,因为int是一个原始类型,不能是null)。然后您将其余节点添加到具有 0 的原始节点。您很可能希望返回一个新的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 "";
    }
}

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

【讨论】:

  • 感谢您的明确回答
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-11
  • 1970-01-01
  • 1970-01-01
  • 2013-08-24
相关资源
最近更新 更多