1066 Root of AVL Tree (25分)(AVL树的实现)

An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child subtrees of any node differ by at most one; if at any time they differ by more than one, rebalancing is done to restore this property. Figures 1-4 illustrate the rotation rules.

 

 

Now given a sequence of insertions, you are supposed to tell the root of the resulting AVL tree.

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive integer N (≤) which is the total number of keys to be inserted. Then N distinct integer keys are given in the next line. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print the root of the resulting AVL tree in one line.

Sample Input 1:

5
88 70 61 96 120

Sample Output 1:

70

Sample Input 2:

7
88 70 61 96 120 90 65 

Sample Output 2:

88

题目分析:写一个AVL树 背课文是好的 但也要理解清除AVL树的原理 之后还要学习各种平衡树 如红黑树什么的
 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include <climits>
 3 #include<iostream>
 4 #include<vector>
 5 #include<queue>
 6 #include<map>
 7 #include<set>
 8 #include<stack>
 9 #include<algorithm>
10 #include<string>
11 #include<cmath>
12 using namespace std;
13 typedef struct TNode* Tree;
14 struct TNode {
15     int Data;
16     Tree TL;
17     Tree TR;
18     int Height;  //要初始化为-1;
19 };
20 int GetHeight(Tree T) {
21     if (T)
22         return T->Height;
23     else
24         return -1;
25 }
26 Tree singleLeftRotate(Tree T) {
27     Tree TL = T->TL;
28     T->TL = TL->TR;
29     TL->TR = T;
30     T->Height = max(GetHeight(T->TL), GetHeight(T->TR)) + 1;
31     TL->Height = max(GetHeight(TL->TL), GetHeight(TL->TR)) + 1;
32     return TL;
33 }
34 Tree singleRightRotate(Tree T) {
35     Tree TR = T->TR;
36     T->TR = TR->TL;
37     TR->TL = T;
38     T->Height = max(GetHeight(T->TL), GetHeight(T->TR)) + 1;
39     TR->Height = max(GetHeight(TR->TL), GetHeight(TR->TR)) + 1;
40     return TR;
41 }
42 Tree doubleLeftRightRotate(Tree T) {
43     T->TL = singleRightRotate(T->TL);
44     return singleLeftRotate(T);
45 }
46 Tree doubleRightLeftRotate(Tree T) {
47     T->TR = singleLeftRotate(T->TR);
48     return singleRightRotate(T);
49 }
50 Tree Insert(Tree T,int data) {
51     if (!T)
52     {
53         T = new TNode();
54         T->Data = data;
55         T->Height = 0;
56         T->TL = T->TR = NULL;
57     }
58     else if (data > T->Data) {
59         T->TR = Insert(T->TR, data);
60         T->Height = max(GetHeight(T->TL), GetHeight(T->TR)) + 1;
61         if (GetHeight(T->TR) - GetHeight(T->TL) == 2) {
62             if (data > T->TR->Data)
63                 T=singleRightRotate(T);
64             else
65                 T=doubleRightLeftRotate(T);
66         }
67     }
68     else {
69         T->TL = Insert(T->TL, data);
70         T->Height = max(GetHeight(T->TL), GetHeight(T->TR)) + 1;
71         if (GetHeight(T->TL) - GetHeight(T->TR) == 2) {
72             if (data < T->TL->Data)
73                 T=singleLeftRotate(T);
74             else
75                 T=doubleLeftRightRotate(T);
76         }
77     }
78     return T;
79 }
80 
81 
82 int main()
83 {
84     int N;
85     Tree T = NULL;
86     int data;
87     cin >> N;
88     for (int i = 0; i < N; i++)
89     {
90         cin >> data;
91         T = Insert(T, data);
92     }
93     cout << T->Data;
94 }
View Code

猜你喜欢

转载自www.cnblogs.com/57one/p/12068341.html