java實現二叉樹的構建以及3種遍歷方法


大二下學期學習數據結構的時候用C介紹過二叉樹,但是當時熱衷於java就沒有怎么鳥二叉樹,但是對二叉樹的構建及遍歷一直耿耿於懷,今天又遇見這個問題了,所以花了一下午的時間來編寫代碼以及介紹思路的文檔生成!


目錄:

1.把一個數組的值賦值給一顆二叉樹

2.具體代碼


1.樹的構建方法



2.具體代碼

package test;

import java.util.LinkedList;  
import java.util.List;  
  
/** 
 * 功能:把一個數組的值存入二叉樹中,然后進行3種方式的遍歷 
 *  
 * 參考資料0:數據結構(C語言版)嚴蔚敏 
 *  
 * 參考資料1:http://zhidao.baidu.com/question/81938912.html 
 *  
 * 參考資料2:http://cslibrary.stanford.edu/110/BinaryTrees.html#java 
 *  
 * @author ocaicai@yeah.net @date: 2011-5-17 
 *  
 */  
public class BinTreeTraverse2 {  
  
    private int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };  
    private static List<Node> nodeList = null;  
  
    /** 
     * 內部類:節點 
     *  
     * @author ocaicai@yeah.net @date: 2011-5-17 
     *  
     */  
    private static class Node {  
        Node leftChild;  
        Node rightChild;  
        int data;  
  
        Node(int newData) {  
            leftChild = null;  
            rightChild = null;  
            data = newData;  
        }  
    }  
  
    public void createBinTree() {  
        nodeList = new LinkedList<Node>();  
        // 將一個數組的值依次轉換為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) {  
        BinTreeTraverse2 binTree = new BinTreeTraverse2();  
        binTree.createBinTree();  
        // 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);  
    }  
  
}  

 

輸出

先序遍歷:
1 2 4 8 9 5 3 6 7 
中序遍歷:
8 4 9 2 5 1 6 3 7 
后序遍歷:
8 9 4 5 2 6 7 3 1 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM