• 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.jpg

    F2.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 Tree,然后按照层次遍历输出。

    思路:

      1.构建AVL Tree时插入结点会遇到四种情况。

    F1.jpg

    右旋:

    node rightRotate(node root) {

      node temp = root->left;

      root->left = temp->right;

      temp->right = root;

      return temp;

    }

    F2.jpg

    左旋:

    node leftRotate(node root) {

      node temp = root->right;

      root->right = temp->left;

      temp->left = root;

      return temp;

    }

    F3.jpg

    先右旋后左旋:

    node rightLeftRotate(node root) {

      root->right = rightRotate(root->right);

      node temp = leftRotate(root);

      return temp;

    }

    先左旋后右旋:

    node leftRigthRotate(node root) {

      root->left = leftRotate(root->left);

      node temp = rightRotate(root);

      return temp;

    }

     求某一个结点的左右孩子深度时,可以用递归函数求解。最后判断是不是完全二叉树,可以先记录第一个缺失孩子的节点,如果该节点后又出现了有孩子的节点则,不是完全二叉树。

    Code:

      1 #include <bits/stdc++.h>
      2 
      3 using namespace std;
      4 
      5 typedef struct Node* node;
      6 
      7 struct Node {
      8     int val;
      9     node left;
     10     node right;
     11     Node(int v) {
     12         val = v;
     13         left = NULL;
     14         right = NULL;
     15     }
     16 };
     17 
     18 node rightRotate(node root) {
     19     node temp = root->left;
     20     root->left = temp->right;
     21     temp->right = root;
     22     return temp;
     23 }
     24 
     25 node leftRotate(node root) {
     26     node temp = root->right;
     27     root->right = temp->left;
     28     temp->left = root;
     29     return temp;
     30 }
     31 
     32 node leftRightRotate(node root) {
     33     root->left = leftRotate(root->left);
     34     return rightRotate(root);
     35 }
     36 
     37 node rightLeftRotate(node root) {
     38     root->right = rightRotate(root->right);
     39     return leftRotate(root);
     40 }
     41 
     42 int findHeight(node root) {
     43     if (root == NULL) return 0;
     44     int l = findHeight(root->left);
     45     int r = findHeight(root->right);
     46     return max(l, r) + 1;
     47 }
     48 
     49 void insertNode(int v, node& root) {
     50     if (root == NULL) {
     51         root = new Node(v);
     52     } else if (v < root->val) {
     53         insertNode(v, root->left);
     54         int l = findHeight(root->left);
     55         int r = findHeight(root->right);
     56         if (abs(r - l) >= 2) {
     57             if (v < root->left->val) {
     58                 root = rightRotate(root);
     59             } else {
     60                 root = leftRightRotate(root);
     61             }
     62         }
     63     } else {
     64         insertNode(v, root->right);
     65         int l = findHeight(root->left);
     66         int r = findHeight(root->right);
     67         if (abs(r - l) >= 2) {
     68             if (v > root->right->val) {
     69                 root = leftRotate(root);
     70             } else {
     71                 root = rightLeftRotate(root);
     72             }
     73         }
     74     }
     75 }
     76 
     77 bool levelTravel(node root) {
     78     queue<node> que;
     79     que.push(root);
     80     bool isCompleteBinaryTree = true;
     81     bool isFirst = true;
     82     bool isStart = false;
     83     cout << root->val;
     84     while (!que.empty()) {
     85         node temp = que.front();
     86         if (isStart)
     87             cout << " " << temp->val;
     88         else
     89             isStart = true;
     90         que.pop();
     91         if (temp->left) {
     92             que.push(temp->left);
     93             if (!isFirst) isCompleteBinaryTree = false;
     94         } else if (isFirst) {
     95             isFirst = false;
     96         }
     97         if (temp->right) {
     98             que.push(temp->right);
     99             if (!isFirst) isCompleteBinaryTree = false;
    100         } else if (isFirst) {
    101             isFirst = false;
    102         }
    103     }
    104     cout << endl;
    105     return isCompleteBinaryTree;
    106 }
    107 
    108 int main() {
    109     int n, t;
    110     cin >> n;
    111     node root = NULL;
    112     while (n--) {
    113         cin >> t;
    114         insertNode(t, root);
    115     }
    116     if (levelTravel(root))
    117         cout << "YES" << endl;
    118     else
    119         cout << "NO" << endl;
    120     return 0;
    121 }

    参考:https://www.liuchuo.net/archives/2732

    永远渴望,大智若愚(stay hungry, stay foolish)
  • 相关阅读:
    【转】sql server编写通用脚本自动检查两个不同服务器的新旧数据库的表结构差异
    Pytest 2
    【转】python通过SMTP协议发送邮件失败,报错505或535
    【转】环境搭建之allure的安装配置,及简单使用
    Pytest 1
    替换姓名为隐式
    docker 用户组权限
    安装go环境
    Win10配置WSL2安装Ubuntu,并支持Nvidia CUDA 环境
    miniconda源配置
  • 原文地址:https://www.cnblogs.com/h-hkai/p/12759911.html
Copyright © 2020-2023  润新知