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 ythe 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树

解题感悟:1、 LRrevolve函数可以直接调用LLrevolve函数的内容,以后要注意各个函数之间的联系

              2、insert_AVLTree函数开始我使用if 总是报错 改为else就可以原来是因为 树被if前一句处理之后可能满足if的条件

  1 #include<iostream>
  2 #include <cmath>
  3 using namespace  std;
  4 typedef struct  AVLTreeNode * AVLTree;
  5 typedef struct AVLTreeNode
  6 {
  7     int data;
  8     AVLTreeNode* left;
  9     AVLTreeNode* right;
 10     int height;
 11 };
 12 int  getHight(AVLTree T){
 13     if (!T)
 14     {
 15         return -1;
 16     }
 17     else
 18         return T->height;
 19 }
 20 int Max(int a, int b){
 21     if (a>b)
 22     {
 23         return a;
 24     }
 25     else return b;
 26 }
 27 AVLTree LLrevolve(AVLTree T){
 28     AVLTree A = T;
 29     AVLTree B = T->left;
 30     A->left=B->right ;
 31     B->right = A;
 32     A->height = Max(getHight(A->left), getHight(A->right)) + 1;
 33     B->height = Max(getHight(B->left), getHight(B->right)) + 1;
 34     return B;
 35 }
 36 AVLTree RRrevolve(AVLTree T){
 37     AVLTree A = T;
 38     AVLTree B = T->right;
 39     A->right = B->left;
 40     B->left = A;
 41     A->height = Max(getHight(A->left), getHight(A->right)) + 1;
 42     B->height = Max(getHight(B->left), getHight(B->right)) + 1;
 43     return B;
 44 }
 45 //AVLTree LRrevolve(AVLTree T){
 46 //    AVLTree A = T;
 47 //    AVLTree B = T->left;
 48 //    AVLTree C = B->right;
 49 //    B->right = C->left;
 50 //    A->left = C->right;
 51 //    C->left = B;
 52 //    C->right = A;
 53 //    A->height = Max(getHight(A->left), getHight(A->right)) + 1;
 54 //    B->height = Max(getHight(B->left), getHight(B->right)) + 1;
 55 //    C->height = Max(getHight(C->left), getHight(C->right)) + 1;
 56 //    return C;
 57 //}
 58 AVLTree LRrevolve(AVLTree T){
 59     AVLTree A = T;
 60     A->left = RRrevolve(A->left);
 61     return LLrevolve(A);
 62 }
 63 AVLTree RLrevolve(AVLTree T){
 64     AVLTree A = T;
 65     AVLTree B = T->right;
 66     A->right = LLrevolve(A->right);
 67     return RRrevolve(A);
 68 }
 69 AVLTree insert_AVLTree(int elemData, AVLTree T){
 70     if (!T)
 71     {
 72         T = (AVLTreeNode*)malloc(sizeof(AVLTreeNode));
 73         T->data = elemData;
 74         T->left = NULL;
 75         T->right = NULL;
 76         T->height = 0;
 77     }
 78     else if (elemData < T->data)
 79     {
 80         T->left = insert_AVLTree(elemData, T->left);
 81         if (getHight(T->left) - getHight(T->right) == 2){
 82             if (elemData < T->left->data)
 83                 T = LLrevolve(T);    
 84             //if (elemData > T->left->data) 
 85             else
 86                 T = LRrevolve(T);
 87         }
 88     }
 89     else if (elemData > T->data){
 90         T->right = insert_AVLTree(elemData, T->right);
 91         if (getHight(T->right) - getHight(T->left) == 2){
 92             if (elemData > T->right->data)
 93                 T = RRrevolve(T);
 94             //if (elemData < T->right->data)
 95             else
 96                 T = RLrevolve(T);
 97         }
 98     }
 99     T->height = Max(getHight(T->left), getHight(T->right)) + 1;
100     return T;
101 }
102 
103 int main(){
104     int number;
105     cin >> number;
106     AVLTree T = NULL;
107     for (int i = 0; i < number; i++)
108     {
109         int elemData;
110         cin >> elemData;
111         T=insert_AVLTree(elemData,T);
112     }
113     cout << T->data<<endl;
114     return 0;
115 }

 

posted @ 2015-04-13 17:07  lkyssd  阅读(135)  评论(0编辑  收藏  举报