04-1. Root of AVL Tree (25) time limit
Memory Limit 65536 KB
Code length limit 8000 B
Procedures for the award of questions StandardAuthor Chen, Yue
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
Submit Code
Test instructions: give you n number of outputs to find the root node of the tree with their established balance
Reference: http://www.cppblog.com/cxiaojia/archive/2013/07/22/187776.html
http://blog.csdn.net/realxuejin/article/details/12872035 Two blog post
#include <iostream> #include <algorithm> #include <string> #include <cstdio> #include < cstring> #include <cstdlib> #include <cmath> #include <vector> #include <queue> #include < stack> #include <map> #include <set>using namespace std;//#define Lson rt<<1,l,mid//#define Rson RT <<1|1,mid+1,r//#define Lson root<<1//#define Rson root<<1|1//#define MID ((l+r) >>1) typedef Long long ll;typedef pair<int,int> p;const int maxn=50005;const int base=1000;const int Inf=999999;const double eps= 1e-5;struct node//tree Node {int data;//data field int high;///tree height node *lson,*rson;//about son node () {lson=rson=null;high=0;} Constructor initialization function};int Gethigh (node *t)//Take height function {if (t!=null) return t->high; return-1;} Node *ll (node *k2)//left-left single-spin {node *k1; k1=k2->lson; k2->lson=k1->rson; k1->rson=k2; K2->high=max (Gethigh (K2->lson), Gethigh (K2->rson)) +1; K1->high=max (Gethigh (k1->lson), Gethigh (K1->rson)) +1; return K1;} Node *RR (node *k2)//right-right single-spin {node *k1; k1=k2->rson; k2->rson=k1->lson; k1->lson=k2; K2->high=max (Gethigh (K2->lson), Gethigh (K2->rson)) +1; K1->high=max (Gethigh (K1->lson), Gethigh (K1->rson)) +1; return K1;} node* LR (node *k3)//left/right double-spin {K3->LSON=RR (K3->lson); Return LL (K3);} node* RL (node * K3)//right-left double-spin {k3->rson=ll (K3->rson); Return RR (K3);} BOOL isbalanced (node *a,node *b)//Determine the left and right sub-tree height difference {return abs (Gethigh (a)-gethigh (b)) <2;} node* Insert (node *root,int v)//The process of establishing the tree {if (root==null) {root=new node (); root->data=v; return root; } if (V>root->data) {Root->rson=insert (root->rson,v); if (!isbalanced (Root->lson,root->rson)) {if (v>root->rson->data) root=rr (RO OT); else Root=rl (root); }} else {Root->lson=insert (root->lson,v); if (!isbalanced (Root->lson,root->rson)) {if (v>root->lson->data) Roo T=LR (root); else Root=ll (root); }} Root->high=max (Gethigh (Root->lson), Gethigh (Root->rson)) +1; return root;} int main () {int n,m,j,i,k,t; cin>>n; node* T=null; while (n--) {cin>>t; T=insert (t,t); } printf ("%d\n", t->data); return 0;}
04-1. Root of AVL Tree (25)