Topic URL: http://pat.zju.edu.cn/contests/pat-a-practise/1066
An AVL tree is a self-balancing binary search tree. In a AVL tree, the heights of the "two child subtrees of" any node differ by at most one; If at any time they differ by the more than one, the rebalancing is do to restore the property. Figures 1-4 illustrate the rotation rules.
Now given a sequence of insertions, your are supposed to tell the root of the resulting AVL tree.
Input Specification:
Each input file contains one test case. For each case, the contains a positive integer N (<=20) which are the total number of keys to be inserted. Then N distinct integer keys are given in the next line. All of the numbers in a line are separated by a.
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 120
Sample Output 1:
70
Sample Input 2:
7
88 70 61 96 120 90 65
Sample Output 2:
88
To investigate the general operation of AVL tree (self-balanced binary lookup tree), refer to http://www.cnblogs.com/baochuan/archive/2012/10/16/2716641.html Web site.
The code is as follows:
/* http://pat.zju.edu.cn/contests/pat-a-practise/1066 AVL tree for self-balanced binary lookup tree.
The maximum difference in height of the two subtrees of any node in the AVL tree is one, so it is also known as a highly balanced tree.
* * #include <iostream> #include <fstream> using namespace std;
Ifstream fin ("in.txt");
#define CIN fin struct Node {int value;
Node* Leftchild;
node* Rightchild; int height;
Zuozi Height-Right sub-tree height Node (int v): value (v), leftchild (null), Rightchild (null), height (0) {}};
int GetHeight (node* node) {if (node = NULL) return-1;
Return node->height;
BOOL Isbalanced (node* parent) {return abs (GetHeight (Parent->leftchild)-getheight (Parent->rightchild)) < 2;
} node* Rotate_ll (node* parent) {node* child = parent->leftchild;
Parent->leftchild = child->rightchild;
Child->rightchild = parent;
Parent->height = Max (GetHeight (Parent->leftchild), GetHeight (parent->rightchild)) + 1;
Child->height = Max (GetHeight (Child->leftchild), GetHeight (child->rightchild)) + 1;
return to child; } node* ROTATE_RR (node* parent) {node* Child = parent->rightchild;
Parent->rightchild = child->leftchild;
Child->leftchild = parent;
Parent->height = Max (GetHeight (Parent->leftchild), GetHeight (parent->rightchild)) + 1;
Child->height = Max (GetHeight (Child->leftchild), GetHeight (child->rightchild)) + 1;
return to child;
} node* ROTATE_LR (node* parent) {node* child = parent->leftchild;
Parent->leftchild = ROTATE_RR (child);
return Rotate_ll (parent);
} node* Rotate_rl (node* parent) {node* child = parent->rightchild;
Parent->rightchild = Rotate_ll (child);
return ROTATE_RR (parent); } node* Insertnode (node* root,int newvalue) {if (root!=null) {if (NewValue > Root->value)//r {root->
Rightchild = Insertnode (Root->rightchild,newvalue);
if (!isbalanced (root)) {if (NewValue > Root->rightchild->value)//r-r {root = Rotate_rr (root);
}else//r-l {root = Rotate_rl (root); }}}else//l {root->Leftchild = Insertnode (Root->leftchild,newvalue);
if (!isbalanced (root)) {if (NewValue > Root->leftchild->value)//l-r {root = ROTATE_LR (root);
}else//l-l {root = Rotate_ll (root);
}}}else {root = new Node (newvalue);
Root->height = Max (GetHeight (Root->leftchild), GetHeight (root->rightchild)) + 1;
return root;
} void Printtree (node* root) {if (root!= NULL) {cout<<root->value<< "-";
Printtree (Root->leftchild);
Printtree (Root->rightchild);
int main () {int n;
cin>>n;
Node *root = NULL;
int x;
int i;
for (i=0;i<n;i++) {cin>>x;
Root = Insertnode (root,x);
} cout<<root->value<<endl;
System ("PAUSE");
return 0; }