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. Git 项目上传至github入门实战并解决常见错误

    1.Git GUI 首先,在push到github的项目必须先建立版本(即creat  repository的名字一样),一般是先pull下来,再push(为了防止有其他人提交了代码,而你却不知道,造 ...

  2. 个人博客搭建----基于solo

    个人站地址是:http://www.iwillhaveacatoneday.cn 博客是基于开源的Java 博客系统--solo搭建的,这里记录下部署过程中遇到的一些主要问题 后台 solo后台采的是 ...

  3. POJ 3653 &amp; ZOJ 2935 &amp; HDU 2722 Here We Go(relians) Again(最短路dijstra)

    题目链接: PKU:http://poj.org/problem? id=3653 ZJU:problemId=1934" target="_blank">http ...

  4. pascal+sublime搭建Pascal学习环境

    一.fpc安装 1. 下载:http://www.freepascal.org/down/i386/win32.var(或者:http://download.csdn.net/detail/wenph ...

  5. .m文件导入C++头文件带来的错误

    这几天的工作挖了不少的坑.遇到了各种千奇百怪的错误,如今好好总结一下. 新建一个project,然后新建HelloCPP.h,HelloCPP.cpp文件.HelloCPP.h文件内容例如以下: #i ...

  6. C++第11周(春)项目3 - 点类派生直线类

    课程首页在:http://blog.csdn.net/sxhelijian/article/details/11890759.内有完整教学方案及资源链接 [项目3 - 点类派生直线类]定义点类Poin ...

  7. 把一个文件夹下的多个excel文件合并到同一个excel的一个sheet里

    #!/usr/bin/python # -*- coding: UTF-8 -*- import pandas as pd import os if __name__ == '__main__': F ...

  8. Java获取NTP网络时间

    最近项目中涉及到一个时间验证的问题,需要根据当前时间来验证业务数据是否过期.所以直接写代码如下: new java.util.Date().getTime();          结果测试的时候出现了 ...

  9. 有关于dict(字典)的特性与操作方法

    有关于dict(字典)的特性与操作方法 1.字典的特性 语法: dic = {key1 : value1,key2 : value2,key3 : value3............} 注:字典中k ...

  10. android 提纲挈领

    之后的android学习将侧重三方面: 1.基础内容例如xml属性.sharedpreference.数据库必须能够熟记于心. 2.开源library熟练应用,能够了解如何更好地使用各种开源libra ...