1. 程式人生 > >如何直觀形象地樹狀打印一棵二叉樹?

如何直觀形象地樹狀打印一棵二叉樹?

str bst over binary 二叉樹 效果 sin code dog

網上絕大部分的二叉樹打印效果都十分潦草,也不夠直觀形象,最近自己用Java寫了個打印器,可以實現如下效果

技術分享圖片

BinarySearchTree<Integer> bst1 = bst(new Integer[]{
    7, 4, 9, 2, 5, 8, 11, 1, 3, 6, 10, 12
});
printer.treeln(bst1);
/*
        7
      /       4       9
   / \     /   2   5   8   11
 / \   \     /  1   3   6   10  12
*/

BinarySearchTree<Integer> bst2 = bst(new Integer[]{
    381, 12, 410, 9, 40, 394, 540, 
    35, 190, 476, 760, 146, 445,
    600, 800
});
printer.treeln(bst2);
/*
        381
      /       12           410
 /  \         /   9    40     394   540
    /  \         /    35    190    476     760
      /       /      /       146     445    600   800
*/

printer.treeln(bst(new Integer[]{
    30, 10, 60, 5, 20, 40, 80,
    15, 50, 70, 90
}));
/*
        30
     /        10          60
 /  \       /    5    20   40      80
    /       \    /    15        50  70  90
*/

printer.treeln(new NodeOperation() {
    @Override
    public Object root() {
        return 8;
    }
    
    @Override
    public Object left(Object node) {
        if (node.equals(8)) return 3;
        if (node.equals(3)) return 1;
        if (node.equals(6)) return 4;
        if (node.equals(14)) return 13;
        return null;
    }
    
    @Override
    public Object right(Object node) {
        if (node.equals(8)) return 10;
        if (node.equals(10)) return 14;
        if (node.equals(3)) return 6;
        if (node.equals(6)) return 7;
        return null;
    }

    @Override
    public Object string(Object node) {
        return node;
    }
});
/*
          8
        /         3       10
     / \            1   6       14
       / \      /
      4   7   13
 */

printer.treeln(new NodeOperation() {
    @Override
    public Object root() {
        return "Life";
    }
    
    @Override
    public Object left(Object node) {
        if (node.equals("Life")) return "Animal";
        if (node.equals("Person")) return "Man";
        if (node.equals("Animal")) return "Cat";
        if (node.equals("Dog")) return "Teddy";
        return null;
    }
    
    @Override
    public Object right(Object node) {
        if (node.equals("Life")) return "Person";
        if (node.equals("Person")) return "Woman";
        if (node.equals("Animal")) return "Dog";
        if (node.equals("Dog")) return "SingleDog";
        return null;
    }

    @Override
    public Object string(Object node) {
        return node;
    }
});
/*
          Life
        /        Animal        Person
   /  \         /    Cat    Dog    Man    Woman
     /      Teddy   SingleDog
 */

具體實現請看github

如何直觀形象地樹狀打印一棵二叉樹?