zoukankan      html  css  js  c++  java
  • PAT 1123 Is It a Complete 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 illustrate the rotation rules.

    F1.jpgF2.jpg
    F3.jpg F4.jpg

    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

    题目大意:构建一颗avl,并且判断是否是完全二叉树, 输出层序遍历;
    尝试用了算法笔记上面的方法, 代码的思路更加清晰一些, 构造的时间比以前的方法短一些;
    区别在于,在节点中添加了记录节点高度的变量, 在求节点高度的时候,不用递归的去求高度
    引用传值,也让代码简洁很多;
    在左旋右旋的过程中跟新节点高度;
    如何证明二叉树是完全二叉树的方法见:https://www.cnblogs.com/mr-stn/p/9232618.html
    注意点:在c++中0被认作是false,其他的值认为是true;
      1 #include<iostream>
      2 #include<vector>
      3 #include<queue>
      4 using namespace std;
      5 struct Node{
      6   int val, height;
      7   Node *left, *right;
      8   Node(int v){
      9     val = v;
     10     height = 1;
     11     left = right = NULL;
     12   }
     13 };
     14 
     15 int getHeight(Node *root){
     16   return root==NULL ? 0 : root->height;
     17 }
     18 
     19 int getBalanceFactor(Node *root){
     20   int l=getHeight(root->left), r=getHeight(root->right);
     21   return l-r;
     22 }
     23 
     24 void updateHeight(Node *root){
     25   int l=getHeight(root->left), r=getHeight(root->right);
     26   root->height =  l>r ? l+1 : r+1;
     27 }
     28 
     29 void leftRoate(Node* &root){
     30   Node *temp = root->right;
     31   root->right = temp->left;
     32   temp->left = root;
     33   updateHeight(root);
     34   updateHeight(temp);
     35   root = temp;
     36 }
     37 
     38 void rightRoate(Node* &root){
     39   Node *temp=root->left;
     40   root->left = temp->right;
     41   temp->right = root;
     42   updateHeight(root);
     43   updateHeight(temp);
     44   root = temp;
     45 }
     46 
     47 void insertNode(Node* &root, int val){
     48   if(root==NULL){
     49     root = new Node(val);
     50     return;
     51   }
     52   if(val<root->val){
     53     insertNode(root->left, val);
     54     updateHeight(root);
     55     if(getBalanceFactor(root)==2){
     56       if(getBalanceFactor(root->left)==1) rightRoate(root);
     57       else{
     58         leftRoate(root->left);
     59         rightRoate(root);
     60       }
     61     }
     62   }else{
     63     insertNode(root->right, val);
     64     updateHeight(root);
     65     if(getBalanceFactor(root)==-2){
     66       if(getBalanceFactor(root->right)==-1) leftRoate(root);
     67       else{
     68         rightRoate(root->right);
     69         leftRoate(root);
     70       }
     71     }
     72   }
     73 }
     74 int main(){
     75   int n, i;
     76   scanf("%d", &n);
     77   Node *root=NULL;
     78   for(i=0; i<n; i++){
     79     int node;
     80     scanf("%d", &node);
     81     insertNode(root, node);
     82   }
     83   vector<int> level;
     84   queue<Node*> q;
     85   q.push(root);
     86   bool existNullNode=false, isAVL=true;
     87   while(q.size()){
     88     Node *temp = q.front();
     89     q.pop();
     90     level.push_back(temp->val);
     91     if(temp->left){
     92       q.push(temp->left);
     93       if(existNullNode) isAVL=false;
     94     }else existNullNode=true;
     95     if(temp->right){
     96       q.push(temp->right);
     97       if(existNullNode) isAVL=false;
     98     }else existNullNode=true;
     99   }
    100   printf("%d", level[0]);
    101   for(i=1; i<n; i++) printf(" %d", level[i]);
    102   printf("
    %s", isAVL ? "YES" : "NO");
    103   return 0;
    104 }
    有疑惑或者更好的解决方法的朋友,可以联系我,大家一起探讨。qq:1546431565
  • 相关阅读:
    2014年7顶级编程语言一个月
    Swift学习 --- 2.3和字符串
    自己写CPU第五级(4)——逻辑、实现移动和空指令
    读书笔记-互联网思维阅读10其中一本书《自由》
    Linux学习笔记——如何使用共享库交叉编译
    Vim识别编码
    linux RWT
    GConf 错误:联系配置服务器失败;某些可能原因是需要为 ORBit 启用 TCP/IP 联网
    Mysql 表忽略大小写~~
    andorid studio
  • 原文地址:https://www.cnblogs.com/mr-stn/p/9233399.html
Copyright © 2011-2022 走看看