Root of AVL Tree

Root of AVL Tree

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 (≤ 20) 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树相关调整操作的模板题。

  AC代码:

 1 #include <cstdio>
 2 #include <algorithm>
 3 
 4 struct TNode {
 5     int data;
 6     TNode *left, *right;
 7 };
 8 
 9 TNode *insert(TNode *T, int data);
10 int getHeight(TNode *T);
11 TNode *LLRot(TNode *root);
12 TNode *RRRot(TNode *root);
13 TNode *LRRot(TNode *root);
14 TNode *RLRot(TNode *root);
15 
16 int main() {
17     int n;
18     scanf("%d", &n);
19     TNode *T = NULL;
20     for (int i = 0 ; i < n; i++) {
21         int data;
22         scanf("%d", &data);
23         T = insert(T, data);
24     }
25     printf("%d", T->data);
26     
27     return 0;
28 }
29 
30 TNode *insert(TNode *T, int data) {
31     if (T == NULL) {
32         T = new TNode;
33         T->data = data;
34         T->left = T->right = NULL;
35         return T;
36     }
37     else if (data < T->data) {
38         T->left = insert(T->left, data);
39         if (getHeight(T->left) - getHeight(T->right) == 2) {
40             if (data < T->left->data) T = LLRot(T);
41             else T = LRRot(T);
42         }
43     }
44     else if (data > T->data) {
45         T->right = insert(T->right, data);
46         if (getHeight(T->left) - getHeight(T->right) == -2) {
47             if (data > T->right->data) T = RRRot(T);
48             else T = RLRot(T);
49         }
50     }
51     
52     return T;
53 }
54 
55 int getHeight(TNode *T) {
56     if (T == NULL) return 0;
57     return std::max(getHeight(T->left), getHeight(T->right)) + 1;
58 }
59 
60 TNode *LLRot(TNode *root) {
61     TNode *t = root->left;
62     root->left = t->right;
63     t->right = root;
64     return t;
65 }
66 
67 TNode *RRRot(TNode *root) {
68     TNode *t = root->right;
69     root->right = t->left;
70     t->left = root;
71     return t;
72 }
73 
74 TNode *LRRot(TNode *root) {
75     root->left = RRRot(root->left);
76     return LLRot(root);
77 }
78 
79 TNode *RLRot(TNode *root) {
80     root->right = LLRot(root->right);
81     return RRRot(root);
82 }

  其中树节点可以存放高度,这样就不需要通过调用递归函数来求树的高度了。

 1 #include <cstdio>
 2 #include <algorithm>
 3 
 4 struct TNode {
 5     int data, height;
 6     TNode *left, *right;
 7 };
 8 
 9 TNode *insert(TNode *T, int data);
10 int getHeight(TNode *T);
11 TNode *LLRot(TNode *root);
12 TNode *RRRot(TNode *root);
13 TNode *LRRot(TNode *root);
14 TNode *RLRot(TNode *root);
15 
16 int main() {
17     int n;
18     scanf("%d", &n);
19     TNode *T = NULL;
20     for (int i = 0 ; i < n; i++) {
21         int data;
22         scanf("%d", &data);
23         T = insert(T, data);
24     }
25     printf("%d", T->data);
26     
27     return 0;
28 }
29 
30 TNode *insert(TNode *T, int data) {
31     if (T == NULL) {
32         T = new TNode;
33         T->data = data;
34         T->left = T->right = NULL;
35     }
36     else if (data < T->data) {
37         T->left = insert(T->left, data);
38         if (getHeight(T->left) - getHeight(T->right) == 2) {
39             if (data < T->left->data) T = LLRot(T);
40             else T = LRRot(T);
41         }
42     }
43     else if (data > T->data) {
44         T->right = insert(T->right, data);
45         if (getHeight(T->left) - getHeight(T->right) == -2) {
46             if (data > T->right->data) T = RRRot(T);
47             else T = RLRot(T);
48         }
49     }
50     
51     T->height = getHeight(T);
52     
53     return T;
54 }
55 
56 int getHeight(TNode *T) {
57     if (T == NULL) return 0;
58     else if (T->left && T->right) return std::max(T->left->height, T->right->height) + 1;
59     else if (T->left) return T->left->height + 1;
60     else if (T->right) return T->right->height + 1;
61     else return 1;
62 }
63 
64 TNode *LLRot(TNode *root) {
65     TNode *t = root->left;
66     root->left = t->right;
67     t->right = root;
68     root->height = getHeight(root);
69     t->height = getHeight(t);
70     return t;
71 }
72 
73 TNode *RRRot(TNode *root) {
74     TNode *t = root->right;
75     root->right = t->left;
76     t->left = root;
77     root->height = getHeight(root);
78     t->height = getHeight(t);
79     return t;
80 }
81 
82 TNode *LRRot(TNode *root) {
83     root->left = RRRot(root->left);
84     return LLRot(root);
85 }
86 
87 TNode *RLRot(TNode *root) {
88     root->right = LLRot(root->right);
89     return RRRot(root);
90 }

posted @ 2021-05-27 22:48  onlyblues  阅读(133)  评论(0编辑  收藏  举报
Web Analytics