PTA (Advanced Level) 1066 root of AVL Tree

Source: Internet
Author: User
Root of AVL Tree

An AVL Tree is a self-balancing Binary Search Tree. in an AVL Tree, the heights of the two child Subtrees of any node differ by at most one; if at any time they differ by more than one, rebalancing is done to restore this property. figures 1-4 using strate the rotation rules.

 

 

 

Now given a sequence of insertions, you are supposed to tell the root of the resulting AVL Tree.

 

Input specification:

Each input file contains one test case. for each case, the first line contains a positive integer N (≤) which is the total number of keys to be inserted. then ndistinct integer keys are given in the next line. all the numbers in a line are separated by a space.

Output specification:

For each test case, print the root of the resulting AVL Tree in 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

Solution:
The AVL Tree template requires that the AVL tree be created based on the input. That is, when the maximum height difference of the leaf node of the Binary Search Tree is greater than or equal to two, the left-or right-hand structure is optimized to keep the node depth at O (logn) output AVL root node.
1 # include <bits/stdc ++. h> 2 using namespace STD; 3 typedef int datatype; 4 vector <datatype> data; 5 struct node {6 datatype data; 7 int height; // AVL Tree nodes need to record height 8 node * leftchild; 9 node * rightchild; 10 node () {11 Height = 1; 12 leftchild = NULL; 13 rightchild = NULL; 14} 15}; 16 int getheight (node * root) {// get height 17 if (root = NULL) 18 return 0; 19 else 20 return root-> height; 21} 2 2 int getbalancefactor (node * root) {// obtain the height difference between the leaf node of the tree. The left height is positive, the right height is negative, and 23 return getheight (root-> leftchild) -getheight (root-> rightchild); 24} 25 int updateheight (node * root) {// update height 26 root-> Height = max (getheight (root-> leftchild ), getheight (root-> rightchild) + 1; 27} 28 void leftrotation (node * & root) {// left-side 29 node * temp = root-> rightchild; // root points to the previous root node temp points to the right child root node 30 root-> rightchild = temp-> leftchild ;// Temp points to the right subtree of the root node, so all its nodes are greater than the root node 31 // due to the left-hand need to make temp a new root node, so point the right subtree of root to the left subtree of temp, and then point the left subtree of temp to root 32 temp-> leftchild = root; 33 // update the tree height of root and temp 34 updateheight (Root ); 35 updateheight (temp); 36 root = temp; // temp becomes the new root node 37} 38 void rightrotation (node * & root) {// The right-hand train of thought is the same as the left-hand 39 node * temp = root-> leftchild; 40 root-> leftchild = temp-> rightchild; 41 temp-> rightchild = root; 42 updateheight (Root); 43 updateheig HT (temp); 44 root = temp; 45} 46 void insertavltree (node * & root, int X) {// insert node 47 If (root = NULL) {// locate the insert position 48 root = new node (); 49 root-> DATA = x; 50 return; 51} 52 If (root-> DATA = x) {// The Node already has 53 return; 54} else if (root-> DATA> X) {// The data to be inserted is smaller than the root node weight 55 insertavltree (root-> leftchild, x); // insert 56 updateheight (Root) on the left subtree ); 57 if (getbalancefactor (Root) = 2) {58 If (getbalancefactor (root-> leftchild) = 1) {59 rightrotation (Root); 60} else if (getbalancefactor (root-> leftchild) =-1) {61 leftrotation (root-> leftchild ); 62 rightrotation (Root); 63} 64} 65} else if (root-> data <X) {// The data to be inserted is 66 higher than the root node weight. insertavltree (root-> rightchild, x); // Insert the right subtree 67 updateheight (Root ); 68 if (getbalancefactor (Root) =-2) {69 If (getbalancefactor (root-> rightchild) =-1) {70 leftrotation (Root ); 71} else if (getbalance Factor (root-> rightchild) = 1) {72 rightrotation (root-> rightchild); 73 leftrotation (Root ); 74} 75} 76} 77} 78 node * createavltree () {79 node * root = NULL; 80 for (vector <datatype >:: iterator it = data. begin (); it! = Data. end (); It ++) {81 insertavltree (root, * It); 82} 83 return root; 84} 85/* void preorder (node * root) {86 If (root = NULL) 87 return; 88 cout <root-> data <""; 89 preorder (root-> leftchild ); 90 preorder (root-> rightchild); 91} */92 int main () 93 {94 int N; 95 while (scanf ("% d", & N )! = EOF) {96 data. clear (); 97 for (INT I = 0; I <n; I ++) {98 datatype temp; 99 scanf ("% d", & temp); 100 data. push_back (temp); 101} 102 node * root = createavltree (); 103 // preorder (Root); 104 // cout <Endl; 105 printf ("% d \ n", root-> data); 106} 107 return 0; 108}

 



PTA (Advanced Level) 1066 root of AVL Tree

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.