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 output the level-order traversal sequence of the resulting AVL tree, and to tell if it is a complete binary tree.

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive integer N (<= 20). 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, insert the keys one by one into an initially empty AVL tree. Then first print in a line the level-order traversal sequence of the resulting AVL tree. All the numbers in a line must be separated by a space, and there must be no extra space at the end of the line. Then in the next line, print "YES" if the tree is complete, or "NO" if not.

Sample Input 1:

5
88 70 61 63 65

Sample Output 1:

70 63 88 61 65
YES

Sample Input 2:

8
88 70 61 96 120 90 65 68

Sample Output 2:

88 65 96 61 70 90 120 68
NO

  1 #include <iostream>
  2 #include <queue>
  3 #include <algorithm>
  4 using namespace std;
  5 struct Node{
  6     int data, h;
  7     Node *left, *right;
  8 };
  9 bool completeAVL = true;
 10 int printFlag;
 11 
 12 Node *newNode(int x){
 13     Node *p = new Node();
 14     p->data = x;
 15     p->left = p->right = NULL;
 16     p->h = 1;
 17     return p;
 18 }
 19 int getHeight(Node *root){//获得高度 
 20     if(root == NULL)    return 0;
 21     return root->h;
 22 }
 23 
 24 int getBanance(Node *root){//获得平衡因子 
 25     if(root == NULL)    return 0;
 26     return getHeight(root->left)-getHeight(root->right);
 27 }
 28 
 29 void updateHeight(Node *root){//更新高度 
 30     if(root){
 31         root->h = max(getHeight(root->left), getHeight(root->right))+1;
 32     }
 33 }
 34 
 35 void R(Node *&root){//右旋 
 36     Node *p = root->left;
 37     root->left = p->right;
 38     p->right = root;
 39     updateHeight(root);
 40     updateHeight(p);
 41     root = p;
 42 }
 43 
 44 void L(Node *&root){//左旋 
 45     Node *p = root->right;
 46     root->right = p->left;
 47     p->left = root;
 48     updateHeight(root);
 49     updateHeight(p);
 50     root = p;
 51 }
 52 
 53 void insert(Node *&root, int x){
 54     if(root == NULL){
 55         root = newNode(x);
 56         return;
 57     }
 58     if(x < root->data){
 59         insert(root->left, x);
 60         updateHeight(root);
 61         if(getBanance(root) == 2){
 62             if(getBanance(root->left) == 1){
 63                 R(root);
 64             }else{//-1 
 65                 L(root->left);
 66                 R(root);
 67             }
 68         }
 69     }else{
 70         insert(root->right, x);
 71         updateHeight(root);
 72         if(getBanance(root) == -2){
 73             if(getBanance(root->right) == -1){
 74                 L(root);
 75             }else{//1
 76                 R(root->right);
 77                 L(root);
 78             }
 79         } 
 80     }
 81 }
 82 
 83 void levelOrder(Node *root){
 84     queue<Node *> q;
 85     q.push(root);
 86     int hasMeetLeaf = false; 
 87     while(!q.empty()){
 88         Node* t = q.front();    q.pop();
 89         if(t->left){
 90             if(hasMeetLeaf)    completeAVL = false;
 91             q.push(t->left);
 92         }else{
 93             hasMeetLeaf = true;
 94         }
 95         if(t->right){
 96             if(hasMeetLeaf)    completeAVL = false;
 97             q.push(t->right);
 98         }else{
 99             hasMeetLeaf = true;
100         }
101         if(-- printFlag)    printf("%d ", t->data);
102         else    printf("%d\n", t->data);
103     }
104 }
105 
106 int main()
107 {
108     int N;
109     Node *root = NULL;
110     scanf("%d", &N);
111     for(int i = 0; i < N; i ++){
112         int x;
113         scanf("%d", &x);
114         insert(root, x);
115     }
116     printFlag = N; levelOrder(root);
117     if(completeAVL)    printf("YES\n");
118     else    printf("NO\n");
119     return 0;
120 }

 

Posted on 2017-02-28 16:01  小小旅行商  阅读(126)  评论(0编辑  收藏  举报