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

目錄: 

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

2.具體代碼 


1.樹的構建方法 



2.具體代碼 

Java代碼  收藏代碼
  1. package tree;  
  2.   
  3. import java.util.LinkedList;  
  4. import java.util.List;  
  5.   
  6. /** 
  7.  * 功能:把一個數組的值存入二叉樹中,然後進行3種方式的遍歷 
  8.  *  
  9.  * 參考資料0:數據結構(C語言版)嚴蔚敏 
  10.  *  
  11.  * 參考資料1:http://zhidao.baidu.com/question/81938912.html 
  12.  *  
  13.  * 參考資料2:http://cslibrary.stanford.edu/110/BinaryTrees.html#java 
  14.  *  
  15.  * @author [email protected] @date: 2011-5-17 
  16.  *  
  17.  */  
  18. public class BinTreeTraverse2 {  
  19.   
  20.     private int[] array = { 123456789 };  
  21.     private static List<Node> nodeList = null;  
  22.   
  23.     /** 
  24.      * 內部類:節點 
  25.      *  
  26.      * @author [email protected] @date: 2011-5-17 
  27.      *  
  28.      */  
  29.     private static class Node {  
  30.         Node leftChild;  
  31.         Node rightChild;  
  32.         int data;  
  33.   
  34.         Node(int newData) {  
  35.             leftChild = null;  
  36.             rightChild = null;  
  37.             data = newData;  
  38.         }  
  39.     }  
  40.   
  41.     public void createBinTree() {  
  42.         nodeList = new LinkedList<Node>();  
  43.         // 將一個數組的值依次轉換爲Node節點  
  44.         for (int nodeIndex = 0; nodeIndex < array.length; nodeIndex++) {  
  45.             nodeList.add(new Node(array[nodeIndex]));  
  46.         }  
  47.         // 對前lastParentIndex-1個父節點按照父節點與孩子節點的數字關係建立二叉樹  
  48.         for (int parentIndex = 0; parentIndex < array.length / 2 - 1; parentIndex++) {  
  49.             // 左孩子  
  50.             nodeList.get(parentIndex).leftChild = nodeList  
  51.                     .get(parentIndex * 2 + 1);  
  52.             // 右孩子  
  53.             nodeList.get(parentIndex).rightChild = nodeList  
  54.                     .get(parentIndex * 2 + 2);  
  55.         }  
  56.         // 最後一個父節點:因爲最後一個父節點可能沒有右孩子,所以單獨拿出來處理  
  57.         int lastParentIndex = array.length / 2 - 1;  
  58.         // 左孩子  
  59.         nodeList.get(lastParentIndex).leftChild = nodeList  
  60.                 .get(lastParentIndex * 2 + 1);  
  61.         // 右孩子,如果數組的長度爲奇數才建立右孩子  
  62.         if (array.length % 2 == 1) {  
  63.             nodeList.get(lastParentIndex).rightChild = nodeList  
  64.                     .get(lastParentIndex * 2 + 2);  
  65.         }  
  66.     }  
  67.   
  68.     /** 
  69.      * 先序遍歷 
  70.      *  
  71.      * 這三種不同的遍歷結構都是一樣的,只是先後順序不一樣而已 
  72.      *  
  73.      * @param node 
  74.      *            遍歷的節點 
  75.      */  
  76.     public static void preOrderTraverse(Node node) {  
  77.         if (node == null)  
  78.             return;  
  79.         System.out.print(node.data + " ");  
  80.         preOrderTraverse(node.leftChild);  
  81.         preOrderTraverse(node.rightChild);  
  82.     }  
  83.   
  84.     /** 
  85.      * 中序遍歷 
  86.      *  
  87.      * 這三種不同的遍歷結構都是一樣的,只是先後順序不一樣而已 
  88.      *  
  89.      * @param node 
  90.      *            遍歷的節點 
  91.      */  
  92.     public static void inOrderTraverse(Node node) {  
  93.         if (node == null)  
  94.             return;  
  95.         inOrderTraverse(node.leftChild);  
  96.         System.out.print(node.data + " ");  
  97.         inOrderTraverse(node.rightChild);  
  98.     }  
  99.   
  100.     /** 
  101.      * 後序遍歷 
  102.      *  
  103.      * 這三種不同的遍歷結構都是一樣的,只是先後順序不一樣而已 
  104.      *  
  105.      * @param node 
  106.      *            遍歷的節點 
  107.      */  
  108.     public static void postOrderTraverse(Node node) {  
  109.         if (node == null)  
  110.             return;  
  111.         postOrderTraverse(node.leftChild);  
  112.         postOrderTraverse(node.rightChild);  
  113.         System.out.print(node.data + " ");  
  114.     }  
  115.   
  116.     public static void main(String[] args) {  
  117.         BinTreeTraverse2 binTree = new BinTreeTraverse2();  
  118.         binTree.createBinTree();  
  119.         // nodeList中第0個索引處的值即爲根節點  
  120.         Node root = nodeList.get(0);  
  121.   
  122.         System.out.println("先序遍歷:");  
  123.         preOrderTraverse(root);  
  124.         System.out.println();  
  125.   
  126.         System.out.println("中序遍歷:");  
  127.         inOrderTraverse(root);  
  128.         System.out.println();  
  129.   
  130.         System.out.println("後序遍歷:");  
  131.         postOrderTraverse(root);  
  132.     }  
  133.   
  134. }  


輸出結果: 

Java代碼  收藏代碼
  1. 先序遍歷:  
  2. 1 2 4 8 9 5 3 6 7   
  3. 中序遍歷:  
  4. 8 4 9 2 5 1 6 3 7   
  5. 後序遍歷:  
  6. 8 9 4 5 2 6 7 3 1   
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章