二叉排序樹的高度分析


一,介紹

對於二叉排序樹而言,其相關操作與樹的高度息息相關。設樹中有N個節點,

盡管各個操作的平均時間復雜度為O(logN),但當輸入的序列有序時,構造出來的樹是一個單分支的樹,其高度為O(N)

故對二叉排序樹的各個操作(如,findMax、contains、findMin...)的時間復雜度也退化成O(N)

 

 二:實現思路

現在分析給定一個輸入序列,根據該輸入序列構造一棵二叉排序樹,統計二叉排序樹的平均高度。

 

有一個隨機生成一組范圍為 [1,N] 的隨機數生成算法,該算法參考:

然后,以上面生成的一組序列作為樹的結點,插入到樹中。

插入完成后,計算該樹的樹高。

二叉排序樹高的平均值 = 各棵樹的高度之和 / tree_numbers。比如,30棵二叉排序樹,每棵有31個節點,樹高度的平均值為 8.666666666666666

 

三,統計結果

樹的數目,               每棵樹中節點的數目,      二叉排序樹高的平均值                   完全二叉樹的高度(根的高度為0)

tree_numbers         node_numbers             averHeight

30                         31                               8.666666666666666                   4 

30                         63                               10.833333333333334                 5

30                        127                              13.366666666666667                 6

 

45                        31                                8.422222222222222                   4          

45                        63                                10.822222222222223                 5

45                       127                               13.466666666666667                 6

 

15                       31                                 8.2                                            4

15                       63                                 11.266666666666667                 5

15                       127                               12.666666666666666                 6

 

15                       99                                 11.8                                          6

30                       99                                 12.3                                          6  

45                       99                                 12.488888888888889                 6   

(11.8+12.3+12.49)/3 = 12.19666666

 

從上面可以看出,對於相同數目節點的二叉排序樹和完成二叉樹,前者的高度要比后者高一倍。

 

四,具體實現代碼

 1 import c2.C2_2_8;
 2 
 3 public class BinarySearchTree<T extends Comparable<? super T>> {
 4 
 5     private static class BinaryNode<T> {
 6         T element;
 7         BinaryNode<T> left;
 8         BinaryNode<T> right;
 9 
10         public BinaryNode(T element) {
11             this(element, null, null);
12         }
13 
14         public BinaryNode(T element, BinaryNode<T> left, BinaryNode<T> right) {
15             this.element = element;
16             this.left = left;
17             this.right = right;
18         }
19 
20         public String toString() {
21             return element.toString();
22         }
23     }
24 
25     private BinaryNode<T> root;
26 
27     public BinarySearchTree() {
28         root = null;
29     }
30 
31     public void insert(T ele) {
32         root = insert(ele, root);// 每次插入操作都會'更新'根節點.
33     }
34 
35     private BinaryNode<T> insert(T ele, BinaryNode<T> root) {
36         if (root == null)
37             return new BinaryNode<T>(ele);
38         int compareResult = ele.compareTo(root.element);
39         if (compareResult > 0)
40             root.right = insert(ele, root.right);
41         else if (compareResult < 0)
42             root.left = insert(ele, root.left);
43         else
44             ;
45         return root;
46     }
47 
48     public int height() {
49         return height(root);
50     }
51 
52     private int height(BinaryNode<T> root) {
53         if (root == null)
54             return -1;// 葉子節點的高度為0,空樹的高度為-1
55 
56         return 1 + (int) Math.max(height(root.left), height(root.right));
57     }
58 
59     public static void main(String[] args) {
60         BinarySearchTree<Integer> intTree = new BinarySearchTree<>();
61                 //統計每棵樹有63個節點,共30棵樹的平均高度
62         double averHeight = intTree.averageHeigth(30, 63, intTree);
63         System.out.println("averageheight = " + averHeight);
64     }
65 
66     public double averageHeigth(int tree_numbers, int node_numbers, BinarySearchTree<Integer> tree) {
67         int tree_height, totalHeight = 0;
68         for(int i = 1; i <= tree_numbers; i++){
69             int[] randomNumbers = C2_2_8.algorithm3(node_numbers);//每棵樹隨機生成一組序列,該序列作為節點的值
70             //build tree
71             for(int j = 0; j < node_numbers; j++)//將一組節點插入到樹中
72             {
73                 tree.insert(randomNumbers[j]);
74                 System.out.print(randomNumbers[j] + " ");
75             }
76             System.out.println();
77             tree_height = tree.height();
78             System.out.println("height:" + tree_height);
79     
80             totalHeight += tree_height;//統計樹高之和
81             tree.root = null;//for building next tree, 參考insert()實現
82         }
83     return (double)totalHeight / tree_numbers;
84     }
85 }
86     

 


免責聲明!

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



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