pat04-树4. Root of AVL Tree (25)
04-树4. Root of AVL Tree (25)
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 120Sample Output 1:
70Sample Input 2:
7 88 70 61 96 120 90 65Sample Output 2:
88
平衡二叉树的建立、插入、更新。
1 #include<cstdio> 2 #include<algorithm> 3 #include<iostream> 4 #include<cstring> 5 #include<queue> 6 #include<vector> 7 using namespace std; 8 int num; 9 struct node{ 10 int v,h; 11 node *l,*r; 12 node(){ 13 l=r=NULL; 14 h=1; 15 } 16 }; 17 int max(int a,int b){ 18 if(a>b){ 19 return a; 20 } 21 else{ 22 return b; 23 } 24 } 25 int GetHigh(node *h){ 26 if(!h){ 27 return 0; 28 } 29 return h->h; 30 } 31 void LeftRotation(node *&h){ 32 node *p=h->l; 33 h->l=p->r; 34 p->r=h; 35 h=p; 36 h->r->h=max(GetHigh(h->r->l),GetHigh(h->r->r))+1; 37 h->h=max(GetHigh(h->l),GetHigh(h->r))+1; 38 } 39 void RightRotation(node *&h){ 40 node *p=h->r; 41 h->r=p->l; 42 p->l=h; 43 h=p; 44 h->l->h=max(GetHigh(h->l->l),GetHigh(h->l->r))+1; 45 h->h=max(GetHigh(h->l),GetHigh(h->r))+1; 46 } 47 void RightLeftRotation(node *&h){ 48 LeftRotation(h->r); 49 //h->h=max(GetHigh(h->l),GetHigh(h->r))+1; 50 RightRotation(h); 51 } 52 void LeftRightRotation(node *&h){ 53 RightRotation(h->l); 54 //h->h=max(GetHigh(h->l),GetHigh(h->r))+1; 55 LeftRotation(h); 56 } 57 void AVLInsert(int v,node *&h){ 58 if(!h){//已经到了最底层 59 h=new node(); 60 h->v=v; 61 return; 62 } 63 //bool can=false; 64 if(v<h->v){ 65 AVLInsert(v,h->l); 66 if(GetHigh(h->l)-GetHigh(h->r)==2){ 67 68 //can=true; 69 70 if(v<h->l->v){//左单旋 71 LeftRotation(h); 72 } 73 else{//左右双旋 74 LeftRightRotation(h); 75 } 76 } 77 } 78 else{ 79 AVLInsert(v,h->r); 80 if(GetHigh(h->l)-GetHigh(h->r)==-2){ 81 82 //can=true; 83 84 if(v>h->r->v){//左单旋 85 RightRotation(h); 86 } 87 else{//左右双旋 88 RightLeftRotation(h); 89 } 90 } 91 } 92 //if(!can) 93 h->h=max(GetHigh(h->l),GetHigh(h->r))+1; //更新树高为1或2的树的树高 94 } 95 /*void prefind(node *h){ 96 if(h){ 97 //cout<<11<<endl; 98 cout<<h->v<<endl; 99 prefind(h->l); 100 prefind(h->r); 101 } 102 }*/ 103 int main(){ 104 //freopen("D:\\INPUT.txt","r",stdin); 105 int n; 106 while(scanf("%d",&n)!=EOF){ 107 node *h=NULL; 108 int i; 109 for(i=0;i<n;i++){ 110 scanf("%d",&num); 111 AVLInsert(num,h); 112 } 113 114 //prefind(h); //检测 115 116 printf("%d\n",h->v); 117 } 118 return 0; 119 }
模板:
1 typedef struct AVLTreeNode *AVLTree; 2 typedef struct AVLTreeNode{ 3 ElementType Data; 4 AVLTree Left; 5 AVLTree Right; 6 int Height; 7 }; 8 AVLTree AVL_Insertion ( ElementType X, AVLTree T ) 9 { /* 将 X 插入 AVL 树 T 中,并且返回调整后的 AVL 树 */ 10 if ( !T ) { /* 若插入空树,则新建包含一个结点的树 */ 11 T = (AVLTree)malloc(sizeof(struct AVLTreeNode)); 12 T->Data = X; 13 T->Height = 0; 14 T->Left = T->Right = NULL; 15 } /* if (插入空树) 结束 */ 16 else if (X < T->Data) { /* 插入 T 的左子树 */ 17 T->Left = AVL_Insertion(X, T->Left); 18 if (GetHeight(T->Left) - GetHeight(T->Right) == 2 ) 19 /* 需要左旋 */ 20 if (X < T->Left->Data) 21 T = SingleLeftRotation(T); /* 左单旋 */ 22 else 23 T = DoubleLeftRightRotation(T); /* 左-右双旋 */ 24 } /* else if (插入左子树) 结束 */ 25 else if (X > T->Data) { /* 插入 T 的右子树 */ 26 T->Right = AVL_Insertion(X, T->Right); 27 if (GetHeight(T->Left) - GetHeight(T->Right) == -2 ) 28 /* 需要右旋 */ 29 if (X > T->Right->Data) 30 T = SingleRightRotation(T); /* 右单旋 */ 31 else 32 T = DoubleRightLeftRotation(T); /* 右-左双旋 */ 33 } /* else if (插入右子树) 结束 */ 34 /* else X == T->Data,无须插入 */ 35 T->Height = Max(GetHeight(T->Left),GetHeight(T->Right))+1; 36 /*更新树高*/ 37 return T; 38 } 39 AVLTree SingleLeftRotation ( AVLTree A ) 40 { /* 注意: A 必须有一个左子结点 B */ 41 /* 将 A 与 B 做如图 4.35 所示的左单旋,更新 A 与 B 的高度,返回新的根结点 B */ 42 AVLTree B = A->Left; 43 A->Left = B->Right; 44 B->Right = A; 45 A->Height = Max(GetHeight(A->Left), GetHeight(A->Right))+1; 46 B->Height = Max(GetHeight(B->Left), A->Height)+1; 47 return B; 48 } 49 AVLTree DoubleLeftRightRotation ( AVLTree A ) 50 { /* 注意: A 必须有一个左子结点 B,且 B 必须有一个右子结点 C */ 51 /* 将 A、 B 与 C 做如图 4.38 所示的两次单旋,返回新的根结点 C */ 52 A->Left = SingleRightRotation(A->Left); /*将 B 与 C 做右单旋, C 被返回*/ 53 return SingleLeftRotation(A); /*将 A 与 C 做左单旋, C 被返回*/ 54 }