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 the 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 #include<vector>
  3 #include<algorithm>
  4 #include<queue>
  5 #include<stack>
  6 using namespace std;
  7 
  8 class Node{
  9 public:
 10     int value;
 11     bool vis;
 12     Node *left, *right;
 13     Node(){left = NULL; right = NULL; vis = true;}
 14 };
 15 
 16 /*Node* build(Node* root, int value) {
 17     if(root == NULL) {
 18         root = new Node();
 19         root->value = value;
 20     } else if(root->value > value) root->left = build(root->left, value);
 21        else root->right = build(root->right, value);
 22      return root;
 23 }*/
 24 
 25 Node* roateLeft(Node* root) {
 26     Node* t = root->right;
 27     root->right = t->left;
 28     t->left = root;
 29     return t;
 30 }
 31 
 32 Node* roateRight(Node* root){
 33     Node* t = root->left;
 34     root->left = t->right;
 35     t->right = root;
 36     return t;
 37 }
 38 
 39 Node* roateRightLeft(Node* root){
 40     root->right = roateRight(root->right);
 41     return roateLeft(root);
 42 }
 43 
 44 Node* roateLeftRight(Node* root){
 45     root->left = roateLeft(root->left);
 46     return roateRight(root);
 47 }
 48 
 49 int getHeight(Node* root){
 50     if(root == NULL) return 0;
 51     int l = getHeight(root->left) + 1;
 52     int r = getHeight(root->right) + 1;
 53     return l>r ? l : r;
 54 }
 55 
 56 Node* build(Node* root, int value){
 57     if(root == NULL) {
 58         root = new Node();
 59         root->value = value;
 60     } else if(root->value > value){
 61         root->left = build(root->left, value);
 62         if(getHeight(root->left) - getHeight(root->right) == 2) {
 63             if(value < root->left->value) root = roateRight(root);
 64             else root = roateLeftRight(root);
 65         }
 66     }else{
 67         root->right = build(root->right, value);
 68         if(getHeight(root->right) - getHeight(root->left) == 2) {
 69             if(value < root->right->value) root = roateRightLeft(root);
 70             else root = roateLeft(root);
 71         }
 72     }
 73     return root;
 74 }
 75 
 76 
 77 Node* deleteNode(Node* root, int value){
 78     if(value < root->value){
 79         root->left = deleteNode(root->left, value);
 80         if(getHeight(root->right) - getHeight(root->left) == 2) {
 81             if(getHeight(root->right->right) > getHeight(root->right->left)) root = roateLeft(root);
 82             else root = roateRightLeft(root);
 83         }
 84     }
 85     else if(value > root->value){
 86         root->right = deleteNode(root->right, value);
 87         if(getHeight(root->left) - getHeight(root->right) == 2) {
 88             if(getHeight(root->left->left) > getHeight(root->left->right)) root = roateRight(root);
 89             else root = roateLeftRight(root);
 90         }
 91     }
 92     else{
 93         if(root->left == NULL && root->right == NULL) root = NULL;
 94         else if(root->left == NULL && root->right != NULL) {
 95             root->value = root->right->value;
 96             root->right = NULL;
 97         }else if(root->left != NULL && root->right == NULL){
 98             root->value = root->left->value;
 99             root->left = NULL;
100         }else{
101             if(getHeight(root->left) > getHeight(root->right)) {
102                 Node* pre = root->left;
103                 while(pre->right != NULL) pre = pre->right;
104                 root->value = pre->value;
105                 root->left = deleteNode(root->left, pre->value);
106             
107             }else{
108                 Node* post = root->right;
109                 while(post->left != NULL) post = post->left;
110                 root->value = post->value;
111                 root->right = deleteNode(root->right, post->value);
112             } 
113         }
114     }
115     return root;
116 }
117 
118 void levelOrder(Node* root){
119     queue<Node*> q;
120     q.push(root);
121     while(!q.empty()){
122         Node *temp = q.front();
123         q.pop();
124         cout<<temp->value<<" ";
125         if(temp->left != NULL) q.push(temp->left);
126         if(temp->right != NULL) q.push(temp->right);
127     }
128 }
129 
130 /*void preOrder(Node* root){
131     cout<<root->value<<" ";
132     if(root->left != NULL) preOrder(root->left);
133     if(root->right != NULL) preOrder(root->right);
134 }
135 
136 void inOrder(Node* root){
137     if(root->left != NULL) inOrder(root->left); 
138     cout<<root->value<<" ";
139     if(root->right != NULL) inOrder(root->right);
140 }//中序遍历可以检测树的有效性, 遍历出来的是按照大小顺序排列的
141 
142 void postOrder(Node* root){
143     if(root->left != NULL) postOrder(root->left);
144     if(root->right != NULL) postOrder(root->right);
145     cout<<root->value<<" ";
146 }*/
147 
148 void preOrder(Node* root){
149     stack<Node*> s;
150     s.push(root);
151     while(!s.empty()){
152         Node* temp = s.top();
153         cout<<temp->value<<" ";
154         s.pop();
155         if(temp->right != NULL) s.push(temp->right);
156         if(temp->left != NULL) s.push(temp->left);
157     }
158 }
159 
160 void postOrder(Node* root){
161     stack<Node*> s;
162     s.push(root);
163     while(!s.empty()){
164         Node* temp = s.top();
165         if(temp->left != NULL ) {
166             s.push(temp->left);
167             temp->left = NULL;
168         }
169         else if(temp->right != NULL){
170             s.push(temp->right);
171             temp->right = NULL;
172         }else{
173             cout<<s.top()->value<<" ";
174             s.pop();
175         }
176         
177     }
178 }
179 
180 void inOrder(Node* root){
181     stack<Node*> s;
182     s.push(root);
183     while(!s.empty()){
184         Node* temp = s.top();
185         if(temp->left === 1) temp->left = 0;
186         else if(temp->left != NULL) {
187             s.push(temp->left);
188             temp->left = 1;
189         } else if(temp->left == NULL) temp->left = 1;
190         else if(temp->right != NULL) {
191             s.push(temp->right);
192             temp->right = NULL;
193         }else{
194             cout<<s.top()->value()<<" ";
195             s.pop();
196         }
197     }
198 }
199 int main(){
200     Node *root = NULL;
201     int t[] = {10,5,15,3,6,13,17,2,14,16};
202     for(int i = 0; i < 10; i++) root = build(root, t[i]);
203     levelOrder(root);
204     cout<<endl;
205     inOrder(root);
206 
207 return 0;}

猜你喜欢

转载自www.cnblogs.com/mr-stn/p/8946334.html