二叉樹的實現

二叉樹: html

參考連接:http://blog.csdn.net/luckyxiaoqiang/article/details/7518888 java

http://www.cnblogs.com/vamei/archive/2013/03/17/2962290.html node

 

實現: 數組

 

 

import java.util.LinkedList; 數據結構

import java.util.List; post

 

/** .net

* 功能:把一個數組的值存入二叉樹中,而後進行3種方式的遍歷 htm

* blog

* 參考資料0:數據結構(C語言版)嚴蔚敏 索引

*

* 參考資料1:http://zhidao.baidu.com/question/81938912.html

*

* 參考資料2:http://cslibrary.stanford.edu/110/BinaryTrees.html#java

*

*

*/

public class BinTreeTraverse2 {

 

 

    /**

     * 內部類:節點

     *

     *

     */

    private static class Node {

        Node leftChild;

        Node rightChild;

        int data;

 

        Node(int newData) {

            leftChild = null;

            rightChild = null;

            data = newData;

        }

    }

 

    public void createBinTree(List<Node> nodeList,int[] array) {

        // 將一個數組的值依次轉換爲Node節點

        for (int nodeIndex = 0; nodeIndex < array.length; nodeIndex++) {

            nodeList.add(new Node(array[nodeIndex]));

        }

        // 對前lastParentIndex-1個父節點按照父節點與孩子節點的數字關係創建二叉樹

        for (int parentIndex = 0; parentIndex < array.length / 2 - 1; parentIndex++) {

            // 左孩子

            nodeList.get(parentIndex).leftChild = nodeList

                    .get(parentIndex * 2 + 1);

            // 右孩子

            nodeList.get(parentIndex).rightChild = nodeList

                    .get(parentIndex * 2 + 2);

        }

        // 最後一個父節點:由於最後一個父節點可能沒有右孩子,因此單獨拿出來處理

        int lastParentIndex = array.length / 2 - 1;

        // 左孩子

        nodeList.get(lastParentIndex).leftChild = nodeList

                .get(lastParentIndex * 2 + 1);

        // 右孩子,若是數組的長度爲奇數才創建右孩子

        if (array.length % 2 == 1) {

            nodeList.get(lastParentIndex).rightChild = nodeList

                    .get(lastParentIndex * 2 + 2);

        }

    }

 

    /**

     * 先序遍歷

     *

     * 這三種不一樣的遍歷結構都是同樣的,只是前後順序不同而已

     *

     * @param node

     * 遍歷的節點

     */

    public static void preOrderTraverse(Node node) {

        if (node == null)

            return;

        System.out.print(node.data + " ");

        preOrderTraverse(node.leftChild);

        preOrderTraverse(node.rightChild);

    }

 

    /**

     * 中序遍歷

     *

     * 這三種不一樣的遍歷結構都是同樣的,只是前後順序不同而已

     *

     * @param node

     * 遍歷的節點

     */

    public static void inOrderTraverse(Node node) {

        if (node == null)

            return;

        inOrderTraverse(node.leftChild);

        System.out.print(node.data + " ");

        inOrderTraverse(node.rightChild);

    }

 

    /**

     * 後序遍歷

     *

     * 這三種不一樣的遍歷結構都是同樣的,只是前後順序不同而已

     *

     * @param node

     * 遍歷的節點

     */

    public static void postOrderTraverse(Node node) {

        if (node == null)

            return;

        postOrderTraverse(node.leftChild);

        postOrderTraverse(node.rightChild);

        System.out.print(node.data + " ");

    }

 

    public static void main(String[] args) {

        

         int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

         List<Node> nodeList = new LinkedList<Node>();

        

        BinTreeTraverse2 binTree = new BinTreeTraverse2();

        binTree.createBinTree(nodeList,array);

        

        // nodeList中第0個索引處的值即爲根節點

        Node root = nodeList.get(0);

 

        System.out.println("先序遍歷:");

        preOrderTraverse(root);

        System.out.println();

 

        System.out.println("中序遍歷:");

        inOrderTraverse(root);

        System.out.println();

 

        System.out.println("後序遍歷:");

        postOrderTraverse(root);

    }

 

}

相關文章
相關標籤/搜索