简体   繁体   English

如何打印二叉树

[英]How to print Binary Tree

I've been trying to switch over to Java from Node and one thing I'm wondering about is how to print an object such as a binary Tree in a similar format to how node would display it. 我一直在尝试从Node切换到Java,我想知道的一件事是如何以类似于Node的显示格式打印诸如二进制树之类的对象。 For instance, my binary tree initiation code is as follows: 例如,我的二叉树启动代码如下:

public class BinaryTree {
    int data;
    BinaryTree left, right;

    public static void main(String[] args) {
        BinaryTree tree = new BinaryTree(1);
        tree= new BinaryTree(1);
        tree.left = new BinaryTree(2);
        tree.right= new BinaryTree(3);
        tree.left.right = new BinaryTree(4);
        System.out.println(tree); // output -> BinaryTree@4554617c
    }

    public BinaryTree(int data) {
      super();
      int val;
      this.left = this.right = null;
  }
}

In node, this binary tree would be displayed as the following: 在节点中,该二叉树将显示如下:

TreeNode {
  val: 1,
  right: TreeNode { val: 3, right: null, left: null },
  left:
   TreeNode {
     val: 2,
     right: TreeNode { val: 4, right: null, left: null },
     left: null } }

However in Java, when I do System.out.println(tree); 但是在Java中,当我执行System.out.println(tree);时,

the output -> BinaryTree@4554617c 输出-> BinaryTree @ 4554617c

What is the proper way to print my BinaryTree and what's a good way to do this? 什么是打印我的BinaryTree的正确方法,什么是执行此操作的好方法? Is there a way to print the tree in a JSON format? 有没有办法以JSON格式打印树?

Printing tree will give you the memory address of the main tree node. 打印tree将为您提供主树节点的内存地址。 If you want to print the content of the tree, you will need to implement a recursive print method and recurse over each node in the tree. 如果要打印树的内容,则需要实现一种递归打印方法,并遍历树中的每个节点。
If the node is a final node (no right or left tree) print the content of that node. 如果该节点是最后一个节点(没有右树或左树),则打印该节点的内容。 Otherwise move down the tree. 否则,请沿树下移。 You can print on the way down the tree or on the way back depending on how you want the tree to look. 您可以在沿树的途中或向后的路上打印,具体取决于您希望树的外观。
I hope I understood the question correctly. 我希望我正确理解了这个问题。

I found this solution on stack overflow and modified it... 我在堆栈溢出时找到了解决方案并对其进行了修改...

public static class TreeNode
{
    int value;
    TreeNode leftChildren;
    TreeNode rightChildren;

    public TreeNode(int value, TreeNode left, TreeNode right)
    {
        this.value = value;
        this.leftChildren = left;
        this.rightChildren = right;
    }

    public addLeftNode(TreeNode node)
    {
        this.leftChildren = node;
    }

    public addRightNode(TreeNode node)
    {
        this.rightChildren = node;
    }

    public void print(String prefix)
    {
        print(prefix, true);
    }

    private void print(String prefix, boolean isTail)
    {
        System.out.println(prefix + (isTail ? "└── " : "├── ") + this.toString());

        if(null != leftChildren)
        {
            leftChildren.print(prefix + (isTail ? "    " : "│   "), (null == rightChildren ? true : false));
        }

        if(null != rightChildren)
        {
            rightChildren.print(prefix + (isTail ?"    " : "│   "), true);
        }

    }

    @Override public String toString()
    {
        return "TreeNode { Value: " + this.value + "}";
    }
}

The first node represents the left and the second node the right node in th binary tree. 在二叉树中,第一个节点代表左节点,第二个节点代表右节点。

EDIT: You can use this like this: 编辑:您可以这样使用:

TreeNode root = new TreeNode(22, null, null);

root.addLeftNode(new TreeNode(15, null, null));
root.addRightNode(new TreeNode(35, new TreeNode(27, null, null), null));

root.print(); // This will print recursively all sub-nodes

Alternate you can write a wrapper class like this: 另外,您可以编写一个包装器类,如下所示:

public class BinaryTree
{
    private TreeNode root;

    // implementation of getters and setters

    public void print()
    {
        this.root.print();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM