Source:

PAT A1123 Is It a Complete AVL Tree (30 分)

Description:

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

Keys:

Attention:

  • 判断完全二叉树,while的固定写法
  • Rotation中,先Update root,再Update temp,否则会影响结果,注意

Code:

 /*
Data: 2019-06-24 15:36:45
Problem: PAT_A1123#Is It a Complete AVL Tree
AC: 35:46 题目大意:
由插入序列构造一棵AVL树,输出层次遍历并判断是否为一棵完全二叉树 基本思路:
构造平衡二叉树,
中序遍历并判断是否为完全二叉树
*/
#include<cstdio>
#include<queue>
#include<algorithm>
using namespace std;
struct node
{
int data;
int height;
node *lchild, *rchild;
}; int GetHeight(node *root)
{
if(root == NULL)
return ;
else
return root->height;
} int GetBalanceFactor(node *root)
{
return GetHeight(root->lchild) - GetHeight(root->rchild);
} void UpdataHeight(node *&root)
{
root->height = max(GetHeight(root->lchild),GetHeight(root->rchild))+;
} void LeftRotation(node *&root)
{
node *temp = root->rchild;
root->rchild = temp->lchild;
temp->lchild = root;
UpdataHeight(root);
UpdataHeight(temp);
root = temp;
} void RightRotation(node *&root)
{
node *temp = root->lchild;
root->lchild = temp->rchild;
temp->rchild = root;
UpdataHeight(root);
UpdataHeight(temp);
root = temp;
} void Insert(node *&root, int x)
{
if(root == NULL)
{
root = new node;
root->data = x;
root->height=;
root->lchild = root->rchild = NULL;
}
else if(x < root->data)
{
Insert(root->lchild, x);
UpdataHeight(root);
if(GetBalanceFactor(root) == )
{
if(GetBalanceFactor(root->lchild) == )
RightRotation(root);
else
{
LeftRotation(root->lchild);
RightRotation(root);
}
}
}
else
{
Insert(root->rchild, x);
UpdataHeight(root);
if(GetBalanceFactor(root) == -)
{
if(GetBalanceFactor(root->rchild) == -)
LeftRotation(root);
else
{
RightRotation(root->rchild);
LeftRotation(root);
}
}
}
} int IsComplete(node *root, int n)
{
queue<node*> q;
q.push(root);
int cnt=, ans=;
while(!q.empty())
{
root = q.front();
q.pop();
if(root)
{
printf("%d%c", root->data,++cnt==n?'\n':' ');
q.push(root->lchild);
q.push(root->rchild);
}
else
{
if(cnt==n)
break;
else
{
ans=;
while(!q.empty())
{
root = q.front();
if(root) break;
else q.pop();
}
}
}
}
return ans;
} int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("Test.txt", "r", stdin);
#endif // ONLINE_JUDGE int n,x;
node *root = NULL;
scanf("%d", &n);
for(int i=; i<n; i++)
{
scanf("%d", &x);
Insert(root, x);
}
if(IsComplete(root, n))
printf("YES");
else
printf("NO"); return ;
}

PAT_A1123#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. 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 ...

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

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

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

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

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

  9. PAT甲级——A1123 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 ...

随机推荐

  1. M - Substrings

    You are given a number of case-sensitive strings of alphabetic characters, find the largest string X ...

  2. Swift: 一句话获取虚拟机上APP所在的目录

    在XCode6上,虚拟机的App的存放位置发生了变化,通过下面语句,我们可以立即获得其所在的位置: println(NSTemporaryDirectory())

  3. JEval使用实例

    jeval是为为你的Java应用程序提供可增加的.高性能.数学.  布尔和函数表达式的解析和运算的高级资源包. 以下这个样例包括了JEval经常使用功能: package demo0; import ...

  4. 跨平台C++开源码的两种经常使用编译方式

    作者:朱金灿 来源:http://blog.csdn.net/clever101 跨平台C++开源代码为适应各种编译器的编译,採用了两种方式方面来适配.一种是makefile方式.以著名的空间数据格式 ...

  5. 菜鸟nginx源代码剖析 配置与部署篇(一) 手把手实现nginx &quot;I love you&quot;

    菜鸟nginx源代码剖析 配置与部署篇(一) 手把手配置nginx "I love you" Author:Echo Chen(陈斌) Email:chenb19870707@gm ...

  6. bazel编译tensorflow 生成libtensorflow_inference.so 和 libandroid_tensorflow_inference_java.jar

    bazel build -c opt //tensorflow/contrib/android:libtensorflow_inference.so --crosstool_top=//externa ...

  7. C语言实现字符串拼接

    #include <stdio.h>#include <stdlib.h>#include <string.h> char* str_contact(const c ...

  8. 七牛php上传下载类,集成官方文档的方法

    <?phpuse Qiniu\Auth;use Qiniu\Storage\UploadManager;class qiniu{ public $_accesskey = null; publi ...

  9. GS运维常用工具及文档

    规范部分   GS产品线性能问题处理流程:http://gsk.inspur.com/File/t-4244 XXX项目性能问题信息收集单-模板:http://gsk.inspur.com/File/ ...

  10. 5.13Junit单元测试-反射-注解

    一.Junit单元测试 * 测试分类: 1.黑盒测试:不需要写代码,给输入值,看程序是否能够输出期望的值 2.白盒测试:需要些代码的.关注程序具体的执行流程 Junit使用:白盒测试 步骤: 1.定义 ...