A1066. Root of AVL Tree (25)

Source: Internet
Author: User

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
1#include <stdio.h>2#include <stdlib.h>3#include <iostream>4#include <string.h>5#include <math.h>6#include <algorithm>7#include <string>8#include <stack>9#include <queue>Ten using namespacestd; One Const intmaxn= the;  A intn,m,s; - structnode{ -Node *Left ; theNode *Right ; -  intV,height;//height can be understood as the number of layers of a tree rooted in that node -}*root,*NULL;  - voidinit () { +     NULL=Newnode; -     NULL->height=0; +root=NULL;//Null is a height of 0 A } atnode* NewNode (intV//set up a new node - { -node* t=Newnode (); -t->v=v; -t->height=1; -t->left=t->right=NULL; in      returnT; - }  to  + voidGetnewheight (Node *root) - { theRoot->height=max (root->left->height,root->right->height) +1; * } $ voidL (Node *&root)Panax Notoginseng { -Node *tmp=root->Right ; theRoot->right=tmp->Left ; +tmp->left=Root; A getnewheight (root); the getnewheight (TMP); +root=tmp; -      $ } $  - voidR (Node *&root) - { thenode* tmp=root->Left ; -Root->left=tmp->Right ;Wuyitmp->right=Root; the getnewheight (root); - getnewheight (TMP); Wuroot=tmp; - } About voidInsert (node *&root,intv) $ { -     if(root==NULL) -     { -root=NewNode (v); A      return;  +     }  the     if(v<root->v) -     { $Insert (root->left,v); the getnewheight (root); the         if((Root->left->height)-(root->right->height) = =2) the         { the             //ll Type -             if((Root->left->left->height)-(root->left->right->height) = =1) in             { the R (root); the}Else if((Root->left->left->height)-(root->left->right->height) ==-1) About             { the                 //LR theL (root->Left ); the R (root); +             } -         } the}ElseBayi     { theInsert (root->right,v); the getnewheight (root); -         if((Root->left->height)-(root->right->height) ==-2) -         { the             if((Root->right->left->height)-(root->right->right->height) = =1) the             { theR (root->Right ); the L (root); -}Else if((Root->right->left->height)-(root->right->right->height) ==-1) the             { the L (root); the             }94         } the     } the } the intMain () {98     intn,v; About init (); -scanf"%d",&n);101      for(intI=0; i<n;i++)102     {103scanf"%d",&v);104 Insert (root,v); the     }106printf"%d\n",root->v);107     return 0;108}

A1066. Root of AVL Tree (25)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.