java 二叉樹

先感慨下:雖爲通信工程畢業,但課程比較傳統,偏向於運營商方向,因爲歷史上來說本院曾經是‘某某郵電學院’,郵電部直屬院校五所之一,所以比較傳統。《數據結構與算法》沒怎麼學過,不想最後大部分同學去了運營商或者郵電規劃建設部門,我卻做了android開發,基礎之薄弱真是令我汗顏。雖然平時開發過程中沒什麼大的感知,但是我知道,如果不補上這一塊,差距將一直存在。


說實話,‘樹’的概念,是我畢業一年以後才知道的,囧。當時被華爲的面試官鄙視的一塌糊塗。當時不以爲意,也沒有進行學習,直到最近希望有些改變,漸漸開始學習這些知識。

public class Tree {
    /*************************************************/
    /*********************** start *******************/
    /*************************************************/
    public static int[] array = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};

    public Tree() {

    }

    /**
     * 把數組變成二叉樹
     */
    public static ArrayList<Node> sortF() {
        ArrayList<Node> list = new ArrayList<Node>();
        for (int i = 0; i < array.length; i++) {
            list.add(new Node(array[i]));
        }

        for (int parentNodeIndex = 0; parentNodeIndex < array.length / 2 - 1; parentNodeIndex++) {
            if (list.get(2 * parentNodeIndex + 1) != null) {
                list.get(parentNodeIndex).leftChildNode = list.get(2 * parentNodeIndex + 1);
            }
            if (list.get(2 * parentNodeIndex + 2) != null) {
                list.get(parentNodeIndex).rightChildNode = list.get(2 * parentNodeIndex + 2);
            }
        }
        //the last parentNode
        int lastParentNodeIndex = array.length / 2 - 1;
        if (list.get(lastParentNodeIndex * 2 + 1) != null) {
            list.get(lastParentNodeIndex).leftChildNode = list.get(lastParentNodeIndex * 2 + 1);
        }
        if (array.length % 2 != 0) {
            if (list.get(lastParentNodeIndex * 2 + 2) != null) {
                list.get(lastParentNodeIndex).rightChildNode = list.get(lastParentNodeIndex * 2 + 2);
            }
        }

        return list;

    }

    public static void printNodeValue(Node node) {
        System.out.println("printNodeValue--node.selfValue=" + node.selfValue);
    }

    public static void test() {
        ArrayList<Node> list = sortF();
        System.out.println("printNodeValue=====left=====");
        leftTraversal(list.get(0));
        System.out.println("printNodeValue======mid====");
        midTraversal(list.get(0));
        System.out.println("printNodeValue======right====");
        rightTraversal(list.get(0));
    }

    /**
     * 遞歸實現前序遍歷
     */
    public static void leftTraversal(Node p) {
        if (p != null) {
            printNodeValue(p);
            leftTraversal(p.getLeftChildNode());
            leftTraversal(p.getRightChildNode());
        }
    }


    public static void midTraversal(Node p) {
        if (p != null) {
            midTraversal(p.getLeftChildNode());
            printNodeValue(p);
            midTraversal(p.getRightChildNode());
        }
    }

    public static void rightTraversal(Node p) {
        if (p != null) {
            rightTraversal(p.getLeftChildNode());
            rightTraversal(p.getRightChildNode());
            printNodeValue(p);
        }
    }

    public static int[] initData() {
        int[] a = new int[10];
        for (int i = 0; i < 10; i++) {
            a[i] = i;
        }
        return a;
    }

    public static class Node {
        int selfValue;
        Node leftChildNode;
        Node rightChildNode;

        public Node(int self) {
            this.selfValue = self;
        }

        public Node getRightChildNode() {
            return rightChildNode;
        }

        public void setRightChildNode(Node rightChildNode) {
            this.rightChildNode = rightChildNode;
        }

        public int getSelfValue() {
            return selfValue;
        }

        public void setSelfValue(int selfValue) {
            this.selfValue = selfValue;
        }

        public Node getLeftChildNode() {
            return leftChildNode;
        }

        public void setLeftChildNode(Node leftChildNode) {
            this.leftChildNode = leftChildNode;
        }
    }
}


12-02 20:06:29.647 11102-11102/com.example.bxh.sayhello I/System.out: printNodeValue=====left=====
12-02 20:06:29.647 11102-11102/com.example.bxh.sayhello I/System.out: printNodeValue--node.selfValue=0
12-02 20:06:29.647 11102-11102/com.example.bxh.sayhello I/System.out: printNodeValue--node.selfValue=1
12-02 20:06:29.647 11102-11102/com.example.bxh.sayhello I/System.out: printNodeValue--node.selfValue=3
12-02 20:06:29.647 11102-11102/com.example.bxh.sayhello I/System.out: printNodeValue--node.selfValue=7
12-02 20:06:29.647 11102-11102/com.example.bxh.sayhello I/System.out: printNodeValue--node.selfValue=8
12-02 20:06:29.647 11102-11102/com.example.bxh.sayhello I/System.out: printNodeValue--node.selfValue=4
12-02 20:06:29.647 11102-11102/com.example.bxh.sayhello I/System.out: printNodeValue--node.selfValue=9
12-02 20:06:29.647 11102-11102/com.example.bxh.sayhello I/System.out: printNodeValue--node.selfValue=2
12-02 20:06:29.647 11102-11102/com.example.bxh.sayhello I/System.out: printNodeValue--node.selfValue=5
12-02 20:06:29.647 11102-11102/com.example.bxh.sayhello I/System.out: printNodeValue--node.selfValue=6
12-02 20:06:29.647 11102-11102/com.example.bxh.sayhello I/System.out: printNodeValue======mid====
12-02 20:06:29.647 11102-11102/com.example.bxh.sayhello I/System.out: printNodeValue--node.selfValue=7
12-02 20:06:29.647 11102-11102/com.example.bxh.sayhello I/System.out: printNodeValue--node.selfValue=3
12-02 20:06:29.647 11102-11102/com.example.bxh.sayhello I/System.out: printNodeValue--node.selfValue=8
12-02 20:06:29.647 11102-11102/com.example.bxh.sayhello I/System.out: printNodeValue--node.selfValue=1
12-02 20:06:29.647 11102-11102/com.example.bxh.sayhello I/System.out: printNodeValue--node.selfValue=9
12-02 20:06:29.647 11102-11102/com.example.bxh.sayhello I/System.out: printNodeValue--node.selfValue=4
12-02 20:06:29.647 11102-11102/com.example.bxh.sayhello I/System.out: printNodeValue--node.selfValue=0
12-02 20:06:29.647 11102-11102/com.example.bxh.sayhello I/System.out: printNodeValue--node.selfValue=5
12-02 20:06:29.647 11102-11102/com.example.bxh.sayhello I/System.out: printNodeValue--node.selfValue=2
12-02 20:06:29.647 11102-11102/com.example.bxh.sayhello I/System.out: printNodeValue--node.selfValue=6
12-02 20:06:29.647 11102-11102/com.example.bxh.sayhello I/System.out: printNodeValue======right====
12-02 20:06:29.647 11102-11102/com.example.bxh.sayhello I/System.out: printNodeValue--node.selfValue=7
12-02 20:06:29.648 11102-11102/com.example.bxh.sayhello I/System.out: printNodeValue--node.selfValue=8
12-02 20:06:29.648 11102-11102/com.example.bxh.sayhello I/System.out: printNodeValue--node.selfValue=3
12-02 20:06:29.648 11102-11102/com.example.bxh.sayhello I/System.out: printNodeValue--node.selfValue=9
12-02 20:06:29.648 11102-11102/com.example.bxh.sayhello I/System.out: printNodeValue--node.selfValue=4
12-02 20:06:29.648 11102-11102/com.example.bxh.sayhello I/System.out: printNodeValue--node.selfValue=1
12-02 20:06:29.648 11102-11102/com.example.bxh.sayhello I/System.out: printNodeValue--node.selfValue=5
12-02 20:06:29.648 11102-11102/com.example.bxh.sayhello I/System.out: printNodeValue--node.selfValue=6
12-02 20:06:29.648 11102-11102/com.example.bxh.sayhello I/System.out: printNodeValue--node.selfValue=2
12-02 20:06:29.648 11102-11102/com.example.bxh.sayhello I/System.out: printNodeValue--node.selfValue=0


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