可视化视图打印树结构Java版


树是数据结构中非常重要的一部分,所有讲到数据结构和算法的书籍,都会讲到

那么,给定一个根节点,如何能够打印出完整的树结构呢?

这里说的打印,不只是像前序、中序和后序遍历中打印节点内容,我们要打的是整个树的结构。

如下是一段实现打印完整树结构的Java代码:

package tree;

/**
 * 以可视化视图打印树结构
 */
public class PrintTree {
    private static class Trunk {
        Trunk prev;
        String str;

        private Trunk(Trunk prev, String str) {
            this.prev = prev;
            this.str = str;
        }
    }

    // Helper function to print branches of the binary tree
    private static void showTrunks(Trunk p) {
        if (p == null)
            return;

        showTrunks(p.prev);

        System.out.print(p.str);
    }

    // 使用中序遍历方式打印二叉树
    private static void traversalPrint(Node root, Trunk prev, boolean isLeft) {
        if (root == null)
            return;

        String ROOT_PREV                    = "   ";
        String CHILD_PREV                   = "    ";

        String LEFT_CHILD_CURVED_EDGE       = ".---";
        String LEFT_CHILD_STRAIGHT_EDGE     = "   |";

        String RIGHT_CHILD_CURVED_EDGE      = "`---";
        String RIGHT_CHILD_STRAIGHT_EDGE    = "   |";


        String prev_str = CHILD_PREV;
        Trunk trunk = new Trunk(prev, prev_str);

        // 遍历左子树
        traversalPrint(root.left, trunk, true);

        if (prev == null)
            trunk.str = ROOT_PREV;
        else if (isLeft) {
            trunk.str = LEFT_CHILD_CURVED_EDGE;
            prev_str = LEFT_CHILD_STRAIGHT_EDGE;
        } else {
            trunk.str = RIGHT_CHILD_CURVED_EDGE;
            prev.str = prev_str;
        }

        showTrunks(trunk);

        // 打印当前节点
        System.out.println(root.data);

        if (prev != null)
            prev.str = prev_str;

        trunk.str = RIGHT_CHILD_STRAIGHT_EDGE;

        // 遍历右子树
        traversalPrint(root.right, trunk, false);
    }

    public static void print(Node root) {
        traversalPrint(root, null, false);
    }

    public static void main(String[] args) {
        Node root = null;

        // Construct above tree
        root = new Node(1);
        root.left = new Node(2);
        root.right = new Node(3);
        root.left.left = new Node(4);
        root.left.right = new Node(5);
        root.right.left = new Node(6);
        root.right.right = new Node(7);
        root.left.left.left = new Node(8);
        root.left.left.right = new Node(9);
        root.left.right.left = new Node(10);
        root.left.right.right = new Node(11);
        root.right.left.left = new Node(12);
        root.right.left.right = new Node(13);
        root.right.right.left = new Node(14);

        // print constructed binary tree
        PrintTree.print(root);
    }

}

这段代码有一个 main 测试方法,执行后打印:

            .---8
        .---4
       |    `---9
    .---2
   |   |    .---10
   |    `---5
   |        `---11
   1
   |        .---12
   |    .---6
   |   |    `---13
    `---3
       |    .---14
        `---7

其中,最左边对应树的跟节点,上面对应左子树,下面对应右子树。

参考地址:Print Binary Tree Structure with its contents in C++

« »

发表评论

电子邮件地址不会被公开。 必填项已用*标注

昵称 *