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 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
- 又是一道avl tree的题,好久不用纯c了,一直用c++,用纯c写一发,不画图写不出来。。。
代码:
- #include <stdio.h>
- #include <stdlib.h>
- typedef struct tree tree;
- struct tree {
- int data;
- tree *left,*right;
- }*head = NULL;
- int n,d,flag = ;
- int level[];
- tree *create(int data) {///创建节点
- tree *t = (tree *)malloc(sizeof(tree));
- t -> left = t -> right = NULL;
- t -> data = data;
- return t;
- }
- int max_(int a,int b) {///最大值
- return a > b ? a : b;
- }
- int getheight(tree *t) {///求高度
- if(t == NULL)return ;
- return max_(getheight(t -> left),getheight(t -> right)) + ;
- }
- tree *ll(tree *t) {///左左旋
- tree *temp = t -> left;
- t -> left = temp -> right;
- temp -> right = t;
- return temp;
- }
- tree *rr(tree *t) {///右右旋
- tree *temp = t -> right;
- t -> right = temp -> left;
- temp -> left = t;
- return temp;
- }
- tree *lr(tree *t) {///左右旋
- t -> left = rr(t -> left);
- return ll(t);
- }
- tree *rl(tree *t) {///右左旋
- t -> right = ll(t -> right);
- return rr(t);
- }
- tree *insert_(tree *t,int data) {
- if(t == NULL)return create(data);
- else if(data > t -> data)t -> right = insert_(t -> right,data);
- else t -> left = insert_(t -> left,data);
- int a = getheight(t -> left),b = getheight(t -> right);
- if(a - b == ) {///左边高
- if(data > t -> left -> data) {///插入的结点 在左子树的右子树
- t = lr(t);
- }
- else t = ll(t);///在左子树的左子树
- }
- else if(b - a == ) {
- if(data < t -> right -> data) {///插入的结点 在右子树的左子树
- t = rl(t);
- }
- else t = rr(t);///在右子树的右子树
- }
- return t;
- }
- void level_order(tree *t) {///判断是否是完全二叉树
- tree *q[] = {t},*p;
- int l = ,r = ;
- while(l < r) {
- p = q[l ++];
- level[l - ] = p -> data;
- if(p -> left) {
- if(flag == )flag = ;///之前有空结点 那么肯定不是完全二叉树
- q[r ++] = p -> left;
- }
- else if(flag == )flag = ;///遇到空结点
- if(p -> right) {
- if(flag == )flag = ;
- q[r ++] = p -> right;
- }
- else if(flag == )flag = ;
- }
- }
- int main() {
- scanf("%d",&n);
- for(int i = ;i < n;i ++) {
- scanf("%d",&d);
- head = insert_(head,d);
- }
- level_order(head);
- for(int i = ;i < n;i ++) {
- if(i)putchar(' ');
- printf("%d",level[i]);
- }
- puts(flag ? "\nYES" : "\nNO");
- }
1123 Is It a Complete AVL Tree(30 分)的更多相关文章
- 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 ...
- 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 ...
- PAT甲级题解-1123. Is It a Complete AVL Tree (30)-AVL树+满二叉树
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6806292.html特别不喜欢那些随便转载别人的原创文章又不给 ...
- PAT 1123. Is It a Complete AVL Tree (30)
AVL树的插入,旋转. #include<map> #include<set> #include<ctime> #include<cmath> #inc ...
- 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 ...
- PAT甲级1123. Is It a Complete AVL Tree
PAT甲级1123. Is It a Complete AVL Tree 题意: 在AVL树中,任何节点的两个子树的高度最多有一个;如果在任何时候它们不同于一个,则重新平衡来恢复此属性.图1-4说明了 ...
- 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 ...
- 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 ...
- PAT甲级1123 Is It a Complete AVL Tree【AVL树】
题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805351302414336 题意: 给定n个树,依次插入一棵AVL ...
随机推荐
- 两个栈来实现一个队列的C++代码
利用两个栈来实现一个队列, 这个问题非经常见. 最关键的是要有好的思路, 至于实现, 那是非常easy的事情了. 在本文中, 也想说说自己的思路, 可是. 我认为用代码来表述思路更符合我的习惯. 也 ...
- HDOJ2084数塔问题
数塔问题 题目要求从顶层走究竟层.若每一步仅仅能走到相邻的结点,求经过的结点的数字之和最大值. 非常经典的DP,能够这样考虑,要求从塔顶到塔底最大路径之和.计算时能够考虑自底向上,走最后一步所选的数一 ...
- XMPP资源绑定(Resource Binding)
一个XMPP的账号由三部分组成: 用户名(user/node),域名(domain)和资源(resource) .例如 alice@xmpp.irusher.com/mobile ,user部分(或n ...
- linux查看磁盘挂载的三种方法
第一种方法:使用df命令,例如: orientalson:/home # df Filesystem 1K-blocks Used Available Use% Mounted on /dev/sda ...
- leetCode 90.Subsets II(子集II) 解题思路和方法
Given a collection of integers that might contain duplicates, nums, return all possible subsets. Not ...
- Layout布局位置
- - GUILayout 这个本身就是用于自动布局的. 不用给定位置的,这也是它与GUI的区别所在.通常默认开始的位置是屏幕的左上角. 当然你也可以限定开始自动布局的位置.用 GUILayout.B ...
- Android-解决Fail to post notification on channel "null"的方法
原文:https://blog.csdn.net/weixin_40604111/article/details/78674563 在sdk版本为25或25之前想在notification中添加一个点 ...
- Asp.Net Mvc: 浅析TempData机制
一. Asp.Net Mvc中的TempData 在Asp.Net Mvc框架的ControllerBase中存在一个叫做TempData的Property,它的类型为TempDataDictiona ...
- 限制线程数 Limit the number of threads started by colly 随机延迟
random delay | Colly http://go-colly.org/docs/examples/random_delay/
- php计算数组的维数
function array_dim($arr){ if(!is_array($arr)) return 0; else{ $max1 = 0; foreach($arr as $item1){ $t ...