Original question:
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
#include <iostream> #include <sstream> #include <string>using namespace Std;template<typename T >class avltreenode;template<typename t>avltreenode<t>* singleleftrotation (AVLTreeNode<T>* A); Template<typename t>avltreenode<t>* singlerightrotation (avltreenode<t>* A); template<typename t>avltreenode<t>* doubleleftrightrotation (avltreenode<t>* A); Template<typename T>AVLTreeNode <t>* doublerightleftrotation (avltreenode<t>* A); Template<typename T>class Avltreenode{public:t Data; Avltreenode<t>* left; avltreenode<t>* right; int Height;}; inline int Max (int a, int b) {return a > b a:b;} Template<typename t>int getheight (avltreenode<t>* A) {if (! A) return 0; Return Max (GetHeight (A->left), GetHeight (a->right)) + 1;} Template<typename t>avltreenode<t>* avl_insertion (t X, avltreenode<t>* T) {if (!t) {t = new Avltreenode<t>; T->data = x; t->height = 0; T->left = t->right = 0; } else if (x < t->data) {T->left = Avl_insertion (x, T->left); if (GetHeight (t->left)-getheight (t->right) = = 2) {if (X < t->left->data) t = singleleftrotation (t); else T = doubleleftrightrotation (t); }} else if (x > T->data) {t->right = Avl_insertion (x, t->right); if (GetHeight (t->left)-getheight (t->right) = =-2) {if (x > T->right->data) t = singlerightrotation (t); else T = doublerightleftrotation (t); }} t->height = Max (GetHeight (T->left), GetHeight (t->right)) + 1; return t;} Template<typename t>avltreenode<t>* singleleftrotation (avltreenode<t>* A) {AVLTreeNode<T>* B = a->left; A-88 A->left = B-> right; B->right = A; B-70 a->height = Max (GetHeight (A->left), GetHeight (a->right)) + 1; B->height = Max (GetHeight (b->left), a->height) + 1; return B;} Template<typename t>avltreenode<t>* singlerightrotation (avltreenode<t>* A) {AVLTreeNode<T> * B = a->right; A->right = b->left; B->left = A; A->height = Max (GetHeight (A->left), GetHeight (a->right)) + 1; B->height = Max (A->height, GetHeight (b->right)) + 1; return B;} Template<typename t>avltreenode<t>* doubleleftrightrotation (avltreenode<t>* A) {A->Left = Singlerightrotation (A->left); Return singleleftrotation (A);} Template<typename t>avltreenode<t>* doublerightleftrotation (avltreenode<t>* A) {A->Right = Singleleftrotation (A->right); Return singlerightrotation (A);} int main (void) {int n, tmp; CIN >> N; String line; Cin.ignore (' \ n '); Getline (CIN, line, '\ n '); Istringstream stream (line); Avltreenode<int>*t = NULL; for (int i = 0; i < n; ++i) {stream >> tmp; t = avl_insertion (tmp, T); } cout << t->data << Endl; return 0;}
Root of AVL Tree