平衡二叉树

LL RR LR RL 注意画图理解法

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
  1 //平衡二叉树 AVL 
  2 #include <stdio.h>
  3 #include <stdlib.h>
  4 
  5 typedef int ElementType;
  6 
  7 typedef struct AVLNode *Position;
  8 typedef Position AVLTree; /* AVL树类型 */
  9 typedef struct AVLNode{
 10     ElementType data; /* 结点数据 */
 11     AVLTree left;     /* 指向左子树 */
 12     AVLTree right;    /* 指向右子树 */
 13     int height;       /* 树高 */
 14 };
 15  
 16 int Max ( int a, int b )
 17 {
 18     return a > b ? a : b;
 19 }
 20 
 21 int GetHeight( Position p )
 22 {
 23     if(!p)
 24         return -1;
 25     return p->height;
 26 }
 27 
 28 /* 将A与B做左单旋,更新A与B的高度,返回新的根结点B */ 
 29 /* 注意:A必须有一个左子结点B */    
 30 AVLTree SingleLeftRotation ( AVLTree A )
 31 {
 32     AVLTree B = A->left;
 33     A->left = B->right;
 34     B->right = A;
 35     A->height = Max( GetHeight(A->left), GetHeight(A->right) ) + 1;
 36     B->height = Max( GetHeight(B->left), A->height ) + 1;
 37   
 38     return B;
 39 }
 40 /* 将A与B做右单旋,更新A与B的高度,返回新的根结点B */ 
 41 /* 注意:A必须有一个右子结点B */
 42 AVLTree SingleRightRotation ( AVLTree A )
 43 { 
 44     AVLTree B = A->right;
 45     A->right = B->left;
 46     B->left = A;
 47     A->height = Max( GetHeight(A->left), GetHeight(A->right) ) + 1;
 48     B->height = Max( A->height, GetHeight(B->right) ) + 1;
 49   
 50     return B;
 51 }
 52 
 53 /* 注意:A必须有一个左子结点B,且B必须有一个右子结点C */
 54 /* 将A、B与C做两次单旋,返回新的根结点C */ 
 55 AVLTree DoubleLeftRightRotation ( AVLTree A )
 56 { 
 57     /* 将B与C做右单旋,C被返回 */
 58     A->left = SingleRightRotation(A->left);
 59     /* 将A与C做左单旋,C被返回 */
 60     return SingleLeftRotation(A);
 61 }
 62 
 63 /* 将A、B与C做两次单旋,返回新的根结点C */ 
 64 /* 注意:A必须有一个右子结点B,且B必须有一个左子结点C */
 65 AVLTree DoubleRightLeftRotation ( AVLTree A )
 66 { 
 67     /* 将B与C做右单旋,C被返回 */
 68     A->right = SingleLeftRotation(A->right);
 69     /* 将A与C做左单旋,C被返回 */
 70     return SingleRightRotation(A);
 71 }
 72 
 73 /* 将X插入AVL树T中,并且返回调整后的AVL树 */
 74 AVLTree Insert( AVLTree T, ElementType X )
 75 { 
 76     if ( !T ) { /* 若插入空树,则新建包含一个结点的树 */
 77         T = (AVLTree)malloc(sizeof(struct AVLNode));
 78         T->data = X;
 79         T->height = 0;
 80         T->left = T->right = NULL;
 81     } /* if (插入空树) 结束 */
 82  
 83     else if ( X < T->data ) {
 84         T->left = Insert( T->left, X);/* 插入T的左子树 */
 85         if ( GetHeight(T->left)-GetHeight(T->right) == 2 ) /* 如果需要左旋 */
 86             if ( X < T->left->data ) 
 87                T = SingleLeftRotation(T);      //左单旋 LL
 88             else 
 89                T = DoubleLeftRightRotation(T); //左-右双旋LR
 90     } /* else if (插入左子树) 结束 */
 91      
 92     else if ( X > T->data ) {
 93         T->right = Insert( T->right, X );/* 插入T的右子树 */
 94         if ( GetHeight(T->left)-GetHeight(T->right) == -2 )/* 如果需要右旋 */
 95             if ( X > T->right->data ) 
 96                T = SingleRightRotation(T);     //右单旋 RR
 97             else 
 98                T = DoubleRightLeftRotation(T); //右-左双旋 RL
 99     } /* else if (插入右子树) 结束 */
100  
101     /*else X == T->Data,无须插入 */
102     T->height = Max( GetHeight(T->left), GetHeight(T->right) ) + 1;    //更新树高 
103      
104     return T;
105 }
106 
107 int main()
108 {
109     int N, data;
110     AVLTree T;
111     scanf("%d",&N);
112     for(int i = 0; i < N; i++) {
113         scanf("%d",&data);
114         T = Insert(T,data);
115     }
116     printf("%d\n",T->data);
117     return 0;
118 }

 

 

 

 
posted on 2016-03-25 23:56  kuotian  阅读(379)  评论(0编辑  收藏  举报