PAT 1066. Root of AVL Tree (25)

1066. Root of AVL Tree (25)

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 illustrate 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 (<=20) which is the total number of keys to be inserted. Then N distinct 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 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
  1 #include <iostream>
  2 
  3 using namespace std;
  4 
  5 struct Node
  6 {
  7     int value;
  8     Node* left;
  9     Node* right;
 10     Node(){ left = right = nullptr; }
 11 };
 12 
 13 int height(Node* root)
 14 {
 15     if (root == nullptr)
 16         return -1;
 17     else
 18     {
 19         int l = height(root->left);
 20         int r = height(root->right);
 21         return l > r ? l + 1 : r + 1;
 22     }
 23 }
 24 
 25 Node* LL(Node* k1)
 26 {
 27     Node* k2;
 28 
 29     k2 = k1->left;
 30     k1->left = k2->right;
 31     k2->right = k1;
 32 
 33     return k2;
 34 }
 35 
 36 Node* RR(Node* k1)
 37 {
 38     Node* k2;
 39 
 40     k2 = k1->right;
 41     k1->right = k2->left;
 42     k2->left = k1;
 43 
 44     return k2;
 45 }
 46 
 47 Node* LR(Node* k3)
 48 {
 49     k3->left = RR(k3->left);
 50 
 51     return LL(k3);
 52 }
 53 
 54 Node* RL(Node* k3)
 55 {
 56     k3->right = LL(k3->right);
 57 
 58     return RR(k3);
 59 }
 60 
 61 Node* Insert(Node* root, int value)
 62 {
 63     if (root == nullptr)
 64     {
 65         Node* node = new Node;
 66         node->value = value;
 67         return node;
 68     }
 69     else if (value < root->value)
 70     {
 71         root->left = Insert(root->left, value);
 72         if (height(root->left) - height(root->right) == 2)
 73         {
 74             if (value < root->left->value)
 75                 root = LL(root);
 76             else
 77                 root = LR(root);
 78         }
 79         return root;
 80     }
 81     else
 82     {
 83         root->right = Insert(root->right, value);
 84         if (height(root->right) - height(root->left) == 2)
 85         {
 86             if (value > root->right->value)
 87                 root = RR(root);
 88             else
 89                 root = RL(root);
 90         }
 91         return root;
 92     }
 93 }
 94 
 95 
 96 int main()
 97 {
 98     int n;
 99     cin >> n;
100 
101     Node* root = nullptr;
102     for (int i = 0; i < n; i++)
103     {
104         int value;
105         cin >> value;
106         root = Insert(root, value);
107     }
108 
109     cout << root->value;
110 }

 

posted @ 2015-08-24 17:46  JackWang822  阅读(813)  评论(0编辑  收藏  举报