利用笛卡尔坐标系在控制台画出二叉树

思想

二叉树的概念就不说了,如何把二叉树画出来,像课本上那样的。
在控制如画的话,就是在一个纯文本的环境下了。
整个控制如可以看作是一个笛卡尔坐标系。


通过给树的结点设置x y 坐标
- 计算x时即依照每行有 2^x 个元素,从root開始,下一行的左右结点的位置位于父结点的左右两边。
计算y时须要使用一開始创建树的时候设置好的树的层级 level 属性。

从而可以在控制台画出二叉树。

树的结点定义

/**
 * 
 * @auther wuwang
 * @createTime 2015-6-17 下午11:17:04
 */
package tree.binarytree;

import lombok.Data;

/**
 * 
 * 
 * @author peaches
 */
@Data
public class BinaryTree {
    private Node root;

    /* 二叉树的构造方法 */
    public BinaryTree() {
        root = null;
    }

    /* insert方法 */
    public void insert(int data) {

        root = insert(root, data, null);
    }

    public Node insert(Node node, int data, Node nodeParent) {
        if (node == null) {
            node = new Node(data);
            node.parent = nodeParent;
            node.level = countLevel(node);
        } else {
            if (data <= node.data) {
                node.left = insert(node.left, data, node);// 递归这里的insert,才形成
                // 的树,事实上是二叉排序树。

} else { node.right = insert(node.right, data, node); } } return node; } /* 构建二叉树 */ public BinaryTree buildTree(int... a) { for (int data : a) { insert(data); } return this; } /* 以下就是中序遍历啊 */ public void printTree() { printTree(root); /* System.out.println(); */ } public void printTree(Node node) { if (node == null) { return; } printTree(node.left); System.out.println(node.data + ""); printTree(node.right); } public int countLevel(Node node) { return node == null ? 0 : (1 + countLevel(node.parent)); } /* 定义一个静态内部类,Node节点 */ @Data public static class Node { Node parent;// 父结点 Node left;// 左结点 Node right;// 右结点 int x;// 用于绘图。图形中的位置。横坐标 int y;// 用于绘图,图形中的位置,纵坐标 Integer data;// 结点的值 int level;// 从root到此结点的层级 public Node getParent() { return null;// toString()时忽略父结点,避免lombok生成的代码会递归溢出 } Node(Integer newData) { left = null; right = null; data = newData; } } }

定义一个工具类,用于输出二叉树结点

/**
 * 
 * @auther wuwang
 * @createTime 2015-6-17 下午11:31:52
 */
package tree.binarytree;

import java.util.Arrays;

/**
 * 
 * 
 * @author peaches
 */
public class BinaryTreeUtils {

    private static int nodeNum;
    private static int lineLength;
    private static int maxLevel;
    private static String[][] strings;

    /**
     * 绘制二叉树
     * 
     * @param binaryTree
     * @createTime 2015-6-19 上午7:55:57
     */
    public static void drawBinaryTree(BinaryTree binaryTree) {

        nodeNum = countNode(binaryTree.getRoot());

        lineLength = nodeNum * 2;

        beforeDrawBinaryTree(binaryTree.getRoot(), NodePostion.ROOT);

        strings = new String[maxLevel + 1][lineLength * 2];

        for (int i = 0; i < strings.length; i++) {
            for (int j = 0; j < strings[i].length; j++) {
                strings[i][j] = " ";
            }
        }

        System.out.println("nodeNum: " + nodeNum);

        drawBinaryTree(binaryTree.getRoot());

        System.out.println("------------------------------------");

        for (String[] s : strings) {
            System.out.println(Arrays.toString(s).replaceAll("[\\]\\[\\,]{1}", " "));
        }
    }

    /**
     * 绘制二叉树前,先计算二叉树中全部结点在xy坐标系中的位置
     * 
     * @param node
     * @param nodePostion
     * @createTime 2015-6-19 上午7:53:59
     */
    private static void beforeDrawBinaryTree(BinaryTree.Node node, NodePostion nodePostion) {
        if (node == null) {
            return;
        }
        countXY(node, nodePostion);

        if (node.left == null) {
            node.left = new BinaryTree.Node(null);
            node.left.parent = node;
            node.left.level = node.level + 1;
            countXY(node.left, NodePostion.LEFT);
        } else {
            beforeDrawBinaryTree(node.left, NodePostion.LEFT);
        }

        if (node.right == null) {
            node.right = new BinaryTree.Node(null);
            node.right.parent = node;
            node.right.level = node.level + 1;
            countXY(node.right, NodePostion.RIGHT);
        } else {
            beforeDrawBinaryTree(node.right, NodePostion.RIGHT);
        }
    }

    /**
     * 绘制二叉树,把二叉树的全部结点输出到一个二维数组中。

二维数组即代表终端。用于按行输出 * * @param node * @createTime 2015-6-19 上午7:55:01 */ private static void drawBinaryTree(BinaryTree.Node node) { if (node == null) { return; } strings[node.y][node.x] = String.valueOf(node.data); drawBinaryTree(node.left); drawBinaryTree(node.right); } /** * 计算二叉树结点个数 * * @param node * @return * @createTime 2015-6-17 下午11:45:33 */ private static int countNode(BinaryTree.Node node) { if (node == null) { return 0; } return 1 + countNode(node.left) + countNode(node.right); } /** * 计算二叉树结点在xy坐标系中的坐标 * * @param node * @param nodePostion * @createTime 2015-6-19 上午7:53:30 */ private static void countXY(BinaryTree.Node node, NodePostion nodePostion) { int x = 0; int y = 0; switch (nodePostion) { case LEFT: x = (int) (node.parent.x - lineLength / (Math.pow(2, node.level) + 2)); y = node.level; break; case RIGHT: x = (int) (node.parent.x + lineLength / (Math.pow(2, node.level) + 2)); y = node.level; break; case ROOT: x = lineLength / 2; y = node.level; break; } node.x = x; node.y = y; if (maxLevel < node.level) { maxLevel = node.level; } } /** * 结点位置。左。右,根 * * @author peaches */ public static enum NodePostion { LEFT, RIGHT, ROOT } }

调用

/**
 * 
 * @auther wuwang
 * @createTime 2015-6-17 下午11:17:45
 */
package tree.binarytree;

/**
 * 
 * 
 * @author peaches
 */
public class TreeMain {
    public static void main(String[] args) {
        BinaryTree binaryTree = new BinaryTree().buildTree(0, 8, 23, 10, 48, 12, 13, 1, 2, 3, 42, 675, 826, 7, 0, 8, 23, 10, 48, 12, 13, 1, 2, 3, 42, 675, 826, 7);
        System.out.println(binaryTree);

        BinaryTreeUtils.drawBinaryTree(binaryTree);
    }
}
posted @ 2016-03-05 11:24  mfrbuaa  阅读(621)  评论(0)    收藏  举报