鏈表實現對二叉樹的操作

import java.util.ArrayList;
import java.util.List;

/**
 * Description:鏈表實現對二叉樹的操作
 */
public class BiTreeByLinked {
    private Node       root;
    private List<Node> list = new ArrayList<>();

    private BiTreeByLinked() {
        Node x = new Node("X", null, null);
        Node y = new Node("Y", null, null);
        Node d = new Node("d", x, y);
        Node e = new Node("e", null, null);
        Node f = new Node("f", null, null);
        Node c = new Node("c", e, f);
        Node b = new Node("b", d, null);
        Node a = new Node("a", b, c);
        root = a;
    }


    private class Node {
        private String data;
        private Node   lchid;
        private Node   rchild;

        Node(String data, Node lchild, Node rchild) {
            this.data = data;
            this.lchid = lchild;
            this.rchild = rchild;
        }
    }

    /**
     * 對該二叉樹進行前序遍歷 結果存儲到list中
     *
     * @param node
     */
    private void preOrder(Node node) {

        list.add(node);
        if (node.lchid != null) {
            preOrder(node.lchid);
        }
        if (node.rchild != null) {
            preOrder(node.rchild);
        }
    }

    /**
     * 對該二叉樹進行中序遍歷 結果存儲到list中
     *
     * @param node
     */
    private void inOrder(Node node) {
        if (node.lchid != null) {
            inOrder(node.lchid);
        }
        list.add(node);
        if (node.rchild != null) {
            inOrder(node.rchild);
        }
    }

    /**
     * 對該二叉樹進行後序遍歷 結果存儲到list中
     *
     * @param node
     */
    private void postOrder(Node node) {
        if (node.lchid != null) {
            postOrder(node.lchid);
        }
        if (node.rchild != null) {
            postOrder(node.rchild);
        }
        list.add(node);
    }

    /**
     * 返回樹的深度
     * 說明:
     * 1、如果一棵樹只有一個結點,它的深度爲1。
     * 2、如果根結點只有左子樹而沒有右子樹,那麼樹的深度是其左子樹的深度加1;
     * 3、如果根結點只有右子樹而沒有左子樹,那麼樹的深度應該是其右子樹的深度加1;
     * 4、如果既有右子樹又有左子樹,那該樹的深度就是其左、右子樹深度的較大值再加1。
     *
     * @return TreeDepth
     */
    private int getTreeDepth(Node node) {
        if (node.lchid == null && node.rchild == null) {
            return 1;
        }

        int left = 0, right = 0;

        if (node.lchid != null) {
            left = getTreeDepth(node.lchid);
        }
        if (node.rchild != null) {
            right = getTreeDepth(node.rchild);
        }

        return left > right ? left + 1 : right + 1;
    }

    /**
     * 得到遍歷結果集
     *
     * @return
     */
    private List<Node> getResultList() {
        return list;
    }

    public static void main(String[] args) {
        BiTreeByLinked tree = new BiTreeByLinked();
        System.out.println("根節點是: " + tree.root.data);

        System.out.print("後序遍歷: ");
        tree.postOrder(tree.root);
        for (Node node : tree.getResultList()) {
            System.out.print(node.data + " ");
        }
        System.out.println();
        System.out.println("樹的深度: " + tree.getTreeDepth(tree.root));
    }

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章