二叉树

普通的二叉树

节点类:

package ds;

/**
 * Created by clearbug on 2018/3/6.
 *
 * 二叉树节点
 */
public class TreeNode {

    private String key;

    private TreeNode leftChild;

    private TreeNode rightChild;

    public TreeNode(String key) {
        this.key = key;
    }

    public String key() {
        return key;
    }

    public TreeNode getLeftChild() {
        return leftChild;
    }

    public void setLeftChild(TreeNode leftChild) {
        this.leftChild = leftChild;
    }

    public TreeNode getRightChild() {
        return rightChild;
    }

    public void setRightChild(TreeNode rightChild) {
        this.rightChild = rightChild;
    }
}

二叉树类:

package ds;

/**
 * Created by clearbug on 2018/3/6.
 *
 * 二叉树
 */
public class BinaryTree {

    public static void main(String[] args) {
        BinaryTree biTree = new BinaryTree();
        biTree.init();
        biTree.preOrderTraverse();
        biTree.inOrderTraverse();
        biTree.postOrderTraverse();
    }

    private TreeNode root;

    public BinaryTree() {
        root = new TreeNode("root");
    }

    /**
     * 初始化一颗二叉树
     */
    public void init() {
        TreeNode node = root;

        TreeNode rootLeftChild = new TreeNode("left");
        TreeNode rootRightChild = new TreeNode("right");
        root.setLeftChild(rootLeftChild);
        root.setRightChild(rootRightChild);

        TreeNode leftLeftChild = new TreeNode("left-left");
        TreeNode leftRightChild = new TreeNode("left-right");
        rootLeftChild.setLeftChild(leftLeftChild);
        rootLeftChild.setRightChild(leftRightChild);

        TreeNode rightLeftChild = new TreeNode("right-left");
        TreeNode rightRightChild = new TreeNode("right-right");
        rootRightChild.setLeftChild(rightLeftChild);
        rootRightChild.setRightChild(rightRightChild);
    }

    private void traverse(TreeNode node, int order) {
        switch (order) {
            case 0: // 先序
                System.out.println(node.key());
                if (node.getLeftChild() != null) {
                    traverse(node.getLeftChild(), order);
                }
                if (node.getRightChild() != null) {
                    traverse(node.getRightChild(), order);
                }
                break;
            case 1: // 中序
                if (node.getLeftChild() != null) {
                    traverse(node.getLeftChild(), order);
                }
                System.out.println(node.key());
                if (node.getRightChild() != null) {
                    traverse(node.getRightChild(), order);
                }
                break;
            case 2: // 后序
                if (node.getLeftChild() != null) {
                    traverse(node.getLeftChild(), order);
                }
                if (node.getRightChild() != null) {
                    traverse(node.getRightChild(), order);
                }
                System.out.println(node.key());
                break;
            default: // 未知
                System.out.println("未知遍历顺序值:" + order);
        }
    }

    /**
     * 先序遍历
     */
    public void preOrderTraverse() {
        System.out.println("先根序遍历:");
        traverse(root, 0);
    }

    /**
     * 中序遍历
     */
    public void inOrderTraverse() {
        System.out.println("中根序遍历:");
        traverse(root, 1);
    }

    /**
     * 后序遍历
     */
    public void postOrderTraverse() {
        System.out.println("后根序遍历:");
        traverse(root, 2);
    }

}

赫夫曼树

赫夫曼(Huffman)树,又称最优树,是一类带权路径长度最短的树。
结点的带权路径长度为从该结点到树根之间的路径长度与结点上权的乘积;
树的带权路径长度为树中所有叶子结点的带权路径长度之和;

posted @ 2018-03-07 11:07  optor  阅读(209)  评论(0编辑  收藏  举报