04-树5 Root of AVL Tree
Posted shin0324
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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.
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 #include<stdio.h> 2 #include<stdlib.h> 3 4 typedef struct AVLNode *PtrToAVLNode; 5 struct AVLNode { 6 int Data; 7 PtrToAVLNode Left, Right; 8 int Height; 9 }; 10 typedef PtrToAVLNode AVL; 11 12 int Max(int a, int b) { 13 if (a > b) return a; 14 else return b; 15 } 16 17 int GetHeight(AVL BT) { 18 int HL, HR, MAXH; 19 if(!BT) return 0; //空树高度为0 20 else { 21 HL = GetHeight(BT->Left); 22 HR = GetHeight(BT->Right); 23 MAXH = Max(HL, HR); //递归求出左子树和右子树的高度,选其中较大的那个 24 return (MAXH + 1); //树的高度等于较大一边的字数高度+根结点 25 } 26 } 27 28 //左单旋 29 AVL SingleLeftRotation(AVL A) { 30 AVL B = A->Left; 31 A->Left = B->Right; 32 B->Right = A; 33 A->Height = Max(GetHeight(A->Left), GetHeight(A->Right)) + 1; 34 B->Height = Max(GetHeight(B->Left), GetHeight(B->Right)) + 1; 35 return B; 36 } 37 38 //右单旋 39 AVL SingleRightRotation(AVL A) { 40 AVL B = A->Right; 41 A->Right = B->Left; 42 B->Left = A; 43 A->Height = Max(GetHeight(A->Left), GetHeight(A->Right)) + 1; 44 B->Height = Max(GetHeight(B->Left), GetHeight(B->Right)) + 1; 45 return B; 46 } 47 48 //左-右双旋 49 AVL DoubleLeftRightRotation(AVL A) { 50 A->Left = SingleRightRotation(A->Left) ; 51 return SingleLeftRotation(A); 52 } 53 54 //右-左双旋 55 AVL DoubleRightLeftRotation(AVL A) { 56 A->Right = SingleLeftRotation(A->Right); 57 return SingleRightRotation(A); 58 } 59 60 //插入新结点函数,在高度差大于1时做出调整 61 AVL Insert(AVL T1, int data) { 62 if (!T1) { 63 T1 = (AVL)malloc(sizeof(struct AVLNode)); 64 T1->Data = data; 65 T1->Left = T1->Right = NULL; 66 } 67 else if (T1->Data > data) { 68 T1->Left = Insert(T1->Left, data); 69 if (GetHeight(T1->Left) - GetHeight(T1->Right) == 2) { 70 if (data < T1->Left->Data) { 71 T1 = SingleLeftRotation(T1); 72 } 73 else T1 = DoubleLeftRightRotation(T1); 74 } 75 } 76 else if (T1->Data < data) { 77 T1->Right = Insert(T1->Right, data); 78 if (GetHeight(T1->Right) - GetHeight(T1->Left) == 2) { 79 if (data > T1->Right->Data) { 80 T1 = SingleRightRotation(T1); 81 } 82 else T1 = DoubleRightLeftRotation(T1); 83 } 84 } 85 T1->Height = GetHeight(T1); //更新树高度 86 return T1; 87 } 88 89 int main() { 90 int N, data; 91 scanf("%d", &N); 92 AVL T = NULL; 93 while (N--) { 94 scanf("%d", &data); 95 T = Insert(T, data); 96 } 97 printf("%d", T->Data); //打印头结点data 98 return 0; 99 }
以上是关于04-树5 Root of AVL Tree的主要内容,如果未能解决你的问题,请参考以下文章