An AVL tree is a self-balancing binary search tree. In a AVL tree, the heights of the subtrees of any node differ by at the most one; If at any time they differ by more than one, the rebalancing is the done to restore this property. Figures 1-4 illustrate the rotation rules.
Now given a sequence of insertions, you is supposed to the root of the resulting AVL tree.
Input Specification:
Each input file contains the one test case. For each case, the first line contains a positive integer N (<=20) which are the total number of keys to being inserted. Then N distinct integer keys is given in the next line. All the numbers in a line is separated by a space.
Output Specification:
For each test case, print ythe root of the resulting AVL tree on one line.
Sample Input 1:
588 70 61) 96 120
Sample Output 1:
70
Sample Input 2:
788 70 61 96 120 90 65
Sample Output 2:
88
Ideas for solving problems: creating AVL trees
Comprehension: 1, the Lrrevolve function can call the contents of the Llrevolve function directly, and then pay attention to the relation between the functions.
2, Insert_avltree function START I use if always error changed to else can be originally because the tree is if the previous sentence after processing may meet if conditions
1#include <iostream>2#include <cmath>3 using namespacestd;4typedefstructAvltreenode *Avltree;5typedefstructAvltreenode6 {7 intdata;8avltreenode*Left ;9avltreenode*Right ;Ten intheight; One }; A intgethight (Avltree T) { - if(!T) - { the return-1; - } - Else - returnT->height; + } - intMax (intAintb) { + if(a>b) A { at returnA; - } - Else returnb; - } - avltree llrevolve (Avltree T) { -Avltree A =T; inAvltree B = t->Left ; -A->left=b->Right ; toB->right =A; +A->height = Max (Gethight (A->left), Gethight (a->right)) +1; -B->height = Max (Gethight (B->left), Gethight (b->right)) +1; the returnB; * } $ avltree rrrevolve (Avltree T) {Panax NotoginsengAvltree A =T; -Avltree B = t->Right ; theA->right = b->Left ; +B->left =A; AA->height = Max (Gethight (A->left), Gethight (a->right)) +1; theB->height = Max (Gethight (B->left), Gethight (b->right)) +1; + returnB; - } $ //avltree lrrevolve (Avltree T) { $ //Avltree A = T; - //avltree B = t->left; - //avltree C = b->right; the //b->right = c->left; - //a->left = c->right;Wuyi //c->left = B; the //c->right = A; - //a->height = Max (Gethight (A->left), Gethight (a->right)) + 1; Wu //b->height = Max (Gethight (B->left), Gethight (b->right)) + 1; - //c->height = Max (Gethight (C->left), Gethight (c->right)) + 1; About //return C; $ //} - avltree lrrevolve (Avltree T) { -Avltree A =T; -A->left = Rrrevolve (a->Left ); A returnllrevolve (A); + } the avltree rlrevolve (Avltree T) { -Avltree A =T; $Avltree B = t->Right ; theA->right = Llrevolve (a->Right ); the returnrrrevolve (A); the } theAvltree Insert_avltree (intElemdata, Avltree T) { - if(!T) in { theT = (avltreenode*)malloc(sizeof(Avltreenode)); theT->data =Elemdata; AboutT->left =NULL; theT->right =NULL; theT->height =0; the } + Else if(Elemdata < t->data) - { theT->left = Insert_avltree (Elemdata, t->Left );Bayi if(Gethight (T->left)-gethight (t->right) = =2){ the if(Elemdata < t->left->data) theT =llrevolve (T); - //if (Elemdata > T->left->data) - Else theT =lrrevolve (T); the } the } the Else if(Elemdata > t->data) { -T->right = Insert_avltree (Elemdata, t->Right ); the if(Gethight (T->right)-gethight (t->left) = =2){ the if(Elemdata > t->right->data) theT =rrrevolve (T);94 //if (Elemdata < T->right->data) the Else theT =rlrevolve (T); the }98 } AboutT->height = Max (Gethight (T->left), Gethight (t->right)) +1; - returnT;101 }102 103 intMain () {104 intNumber ; theCIN >>Number ;106Avltree T =NULL;107 for(inti =0; I < number; i++)108 {109 intElemdata; theCIN >>Elemdata;111t=Insert_avltree (elemdata,t); the }113cout << t->data<<Endl; the return 0; the}
Root of AVL Tree