题意:给定结点个数n和插入序列,判断构造的AVL树是否是完全二叉树?

思路:AVL树的建立很简单。而如何判断是不是完全二叉树呢?通过层序遍历进行判断:当一个结点的孩子结点为空时,则此后就不能有新的结点入队。若没有,则是完全二叉树,否则不是。

代码:

  1. #include <cstdio>
  2. #include <algorithm>
  3. #include <iostream>
  4. #include <vector>
  5. #include <queue>
  6. using namespace std;
  7.  
  8. vector<int> layer;
  9.  
  10. struct Node {
  11. int v, height;
  12. Node *lchild, *rchild;
  13. };
  14.  
  15. Node* newNode(int v) {
  16. Node* pNode = new Node;
  17. pNode->v = v;
  18. pNode->height = ;
  19. pNode->lchild = pNode->rchild = NULL;
  20. return pNode;
  21. }
  22.  
  23. int getHeight(Node* root){
  24.  
  25. if(root==NULL) return ;
  26. return root->height;
  27. }
  28. void updateHeight(Node* root) {
  29. root->height = max(getHeight(root->lchild), getHeight(root->rchild))+;
  30. }
  31.  
  32. int getBalanceFactor(Node* root) {
  33. return getHeight(root->lchild)- getHeight(root->rchild);
  34. }
  35.  
  36. void L(Node* &root) {
  37.  
  38. Node* temp = root->rchild;
  39. root->rchild = temp->lchild;
  40. temp->lchild = root;
  41. updateHeight(root);
  42. updateHeight(temp);
  43. root = temp;
  44. }
  45. void R(Node* &root) {
  46. Node* temp = root->lchild;
  47. root->lchild = temp->rchild;
  48. temp->rchild = root;
  49. updateHeight(root);
  50. updateHeight(temp);
  51. root = temp;
  52. }
  53.  
  54. void insert(Node* &root, int v) {
  55. if (root == NULL) {
  56. root = newNode(v);
  57. return;
  58. }
  59.  
  60. if (v < root->v) {
  61. insert(root->lchild,v);
  62. updateHeight(root);
  63. if (getBalanceFactor(root) == ) {
  64. if(getBalanceFactor(root->lchild)==){
  65. R(root);
  66. }else if(getBalanceFactor(root->lchild)==-){
  67. L(root->lchild);
  68. R(root);
  69. }
  70.  
  71. }
  72. }
  73. else {
  74. insert(root->rchild,v);
  75. updateHeight(root);
  76. if (getBalanceFactor(root) == -) {
  77. if(getBalanceFactor(root->rchild)==-){
  78. L(root);
  79. }
  80. else if(getBalanceFactor(root->rchild)==){
  81. R(root->rchild);
  82. L(root);
  83. }
  84. }
  85. }
  86. }
  87. bool isComplete =true;
  88. int after=;
  89. void layerOrder(Node* root){
  90. queue<Node*> Q;
  91. Q.push(root);
  92. while(!Q.empty()){
  93. Node* front=Q.front();
  94. Q.pop();
  95. layer.push_back(front->v);
  96.  
  97. if(front->lchild!=NULL){
  98. if(after==) isComplete=false;
  99. Q.push(front->lchild);
  100. }else{
  101. after=;
  102. }
  103.  
  104. if(front->rchild!=NULL){
  105. if(after==) isComplete=false;
  106. Q.push(front->rchild);
  107. }else{
  108. after=;
  109. }
  110. }
  111.  
  112. }
  113.  
  114. //vector<int> insertOrder;
  115.  
  116. int main()
  117. {
  118. int n,data;
  119. scanf("%d",&n);
  120. Node* root=NULL;
  121. for(int i=;i<n;i++){
  122. scanf("%d",&data);
  123. insert(root,data);
  124. }
  125. layerOrder(root);
  126.  
  127. for(int i=;i<layer.size()-;i++){
  128. printf("%d ",layer[i]);
  129. }
  130. printf("%d\n",layer[n-]);
  131. printf("%s\n",isComplete==true?"YES":"NO");
  132.  
  133. return ;
  134. }

1123.(重、错)Is It a Complete AVL Tree的更多相关文章

  1. PAT甲级1123. Is It a Complete AVL Tree

    PAT甲级1123. Is It a Complete AVL Tree 题意: 在AVL树中,任何节点的两个子树的高度最多有一个;如果在任何时候它们不同于一个,则重新平衡来恢复此属性.图1-4说明了 ...

  2. 1123 Is It a Complete AVL Tree

    1123 Is It a Complete AVL Tree(30 分) An AVL tree is a self-balancing binary search tree. In an AVL t ...

  3. PAT_A1123#Is It a Complete AVL Tree

    Source: PAT A1123 Is It a Complete AVL Tree (30 分) Description: An AVL tree is a self-balancing bina ...

  4. 1123. Is It a Complete AVL Tree (30)

    An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child sub ...

  5. 1123 Is It a Complete AVL Tree(30 分)

    An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child sub ...

  6. PAT甲级——1123 Is It a Complete AVL Tree (完全AVL树的判断)

    嫌排版乱的话可以移步我的CSDN:https://blog.csdn.net/weixin_44385565/article/details/89390802 An AVL tree is a sel ...

  7. 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 sub ...

  8. PAT Advanced 1123 Is It a Complete AVL Tree (30) [AVL树]

    题目 An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child ...

  9. A1123. 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 sub ...

  10. PAT A1123 Is It a Complete AVL Tree (30 分)——AVL平衡二叉树,完全二叉树

    An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child sub ...

随机推荐

  1. s21day05 python笔记

    s21day05 python笔记 一.昨日内容回顾及补充 回顾 补充 列表独有功能 extend:循环添加到一个列表中 1.users = ['张三',66],people = ['王五',99] ...

  2. HDU 2036 叉乘求三角形面积

    Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s) ...

  3. 回收机制GC

    .NET 之 垃圾回收机制GC 一.GC的必要性 1.应用程序对资源操作,通常简单分为以下几个步骤:为对应的资源分配内存 → 初始化内存 → 使用资源 → 清理资源 → 释放内存. 2.应用程序对资源 ...

  4. 02 JDBC相关

    ====================================================================================JDBC JAVA Databa ...

  5. 20165313Java实验四 Android程序设计

    实验报告封面 课程:Java程序设计 班级:1653班 姓名:张晨晖 学号:20165313 指导教师:娄嘉鹏 实验日期:2018年5月14日 实验时间:13:45 - 15:25 实验序号:实验四 ...

  6. 滚动加载图片(懒加载)实现原理(这是旧实现,仅做为获取元素宽高api的参考)

    https://www.cnblogs.com/flyromance/p/5042187.html 本文主要通过以下几方面来说明懒加载技术的原理,个人前端小菜,有错误请多多指出 一.什么是图片滚动加载 ...

  7. csvn使用入门

    在前面我们已经配置好了csvn服务器,直达链接http://blog.csdn.net/qq_34829953/article/details/78285647 现在我们在win10环境下使用我们搭建 ...

  8. Singer 学习八 运行&&开发taps、targets (三 开发tap)

    如何没有找到适合的tap,那么我们可以自己开发一个 hello world tap 仅仅是一个程序,我们可以使用任何语言进行编写,根据singer 指南,输出数据到stdout 即可,实际上一个简单的 ...

  9. UWA 转载

    性能优化,进无止境-内存篇 https://blog.uwa4d.com/archives/optimzation_memory_1.html https://blog.uwa4d.com/archi ...

  10. 应用端连接MySQL数据库报Communications link failure

    事情的起因: 某项目的开发同学突然Q我们组的某同学,要求我们调整MySQL的连接等待超时参数wait_timeout.要求我们从28800s调整到31536000s(也就是一年) 应用端测试环境的to ...