04-树5 Root of AVL Tree

题目来自:http://t.cn/R4hw22D

04-树5 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.

04-树5 Root of AVL Tree

04-树5 Root of AVL Tree

04-树5 Root of AVL Tree 

04-树5 Root of AVL Tree

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

题目大意是先输入一个整数N,然后依次输入N个节点的值,以此建立AVL树,最后输出AVL树的根节点的值。

代码如下:

 
  1 #include <cstdio>
  2 #include <cstdlib>
  3 
  4 typedef struct TreeNode *AvlTree;
  5 typedef struct TreeNode *Position;
  6 struct TreeNode
  7 {
  8     int Data;
  9     AvlTree Left;
 10     AvlTree Right;
 11     int Height;
 12 };
 13 
 14 AvlTree Insert(int x, AvlTree T);   //插入新节点,必要时调整
 15 Position SingleRotateWithLeft(Position a);    //左单旋
 16 Position SingleRotateWithRight(Position b);   //右单旋
 17 Position DoubleRotateWithLeft(Position a);    //左右旋
 18 Position DoubleRotateWithRight(Position b);   //右左旋
 19 
 20 int Max(int x1, int x2);      //返回两个int中较大的
 21 int Height(Position P);     //返回一个节点的高度
 22 
 23 int main()
 24 {
 25     int n, x;
 26     AvlTree T = NULL;
 27 
 28     scanf("%d", &n);
 29     for (int i = 0; i < n; i++)
 30     {
 31         scanf("%d", &x);
 32         T = Insert(x, T);
 33     }
 34     printf("%d
", T->Data);    //打印根节点的值
 35 
 36     return 0;
 37 }
 38 
 39 AvlTree Insert(int x, AvlTree T)
 40 {
 41     if (T == NULL)
 42     {
 43         T = (AvlTree)malloc(sizeof(struct TreeNode));
 44         T->Data = x;
 45         T->Left = T->Right = NULL;
 46         T->Height = 0;
 47     }
 48     else if (x < T->Data)   //向左子树插入
 49     {
 50         T->Left = Insert(x, T->Left);
 51         if (Height(T->Left) - Height(T->Right) == 2)    //需调整
 52         {
 53             if (x < T->Left->Data)
 54                 T = SingleRotateWithLeft(T);
 55             else
 56                 T = DoubleRotateWithLeft(T);
 57         }
 58     }
 59     else if (x > T->Data)   //向右子树插入
 60     {
 61         T->Right = Insert(x, T->Right);
 62         if (Height(T->Right) - Height(T->Left) == 2)    //需调整
 63         {
 64             if (x > T->Right->Data)
 65                 T = SingleRotateWithRight(T);
 66             else
 67                 T = DoubleRotateWithRight(T);
 68         }
 69     }
 70     /*else值为x的节点已经存在树中,无需插入*/
 71 
 72     /*更新节点高度*/
 73     T->Height = Max(Height(T->Left), Height(T->Right)) + 1;
 74     return T;
 75 }
 76 
 77 Position SingleRotateWithLeft(Position a)
 78 {
 79     Position b = a->Left;
 80     a->Left = b->Right;
 81     b->Right = a;
 82     //更新a, b节点高度
 83     a->Height = Max(Height(a->Left), Height(a->Right)) + 1;
 84     b->Height = Max(Height(b->Left), Height(b->Right)) + 1;
 85 
 86     return b;      /*新的根节点*/
 87 }
 88 
 89 Position SingleRotateWithRight(Position b)
 90 {
 91     Position a = b->Right;
 92     b->Right = a->Left;
 93     a->Left = b;
 94     //更新a,b节点高度
 95     a->Height = Max(Height(a->Left), Height(a->Right)) + 1;
 96     b->Height = Max(Height(b->Left), Height(b->Right)) + 1;
 97     return a;       /*新的根节点*/
 98 }
 99 
100 Position DoubleRotateWithLeft(Position a)
101 {
102     a->Left = SingleRotateWithRight(a->Left);
103     return SingleRotateWithLeft(a);
104 }
105 
106 Position DoubleRotateWithRight(Position b)
107 {
108     b->Right = SingleRotateWithLeft(b->Right);
109     return SingleRotateWithRight(b);
110 }
111 
112 int Max(int x1, int x2)
113 {
114     return (x1 > x2) ? x1 : x2;
115 }
116 
117 int Height(Position P)
118 {
119     if (P == NULL)  //空节点高度为-1
120         return -1;
121     return P->Height;
122 }
参考资料:
04-树4. Root of AVL Tree-平衡查找树AVL树的实现