博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
1123 Is It a Complete AVL Tree(30 分)
阅读量:5351 次
发布时间:2019-06-15

本文共 3560 字,大约阅读时间需要 11 分钟。

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:

588 70 61 63 65

Sample Output 1:

70 63 88 61 65YES

Sample Input 2:

888 70 61 96 120 90 65 68

Sample Output 2:

88 65 96 61 70 90 120 68NO 又是一道avl tree的题,好久不用纯c了,一直用c++,用纯c写一发,不画图写不出来。。。

代码:

#include 
#include
typedef struct tree tree;struct tree { int data; tree *left,*right;}*head = NULL;int n,d,flag = 1;int level[21];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 0; return max_(getheight(t -> left),getheight(t -> right)) + 1;}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 == 2) {
///左边高 if(data > t -> left -> data) {
///插入的结点 在左子树的右子树 t = lr(t); } else t = ll(t);///在左子树的左子树 } else if(b - a == 2) { if(data < t -> right -> data) {
///插入的结点 在右子树的左子树 t = rl(t); } else t = rr(t);///在右子树的右子树 } return t;}void level_order(tree *t) {
///判断是否是完全二叉树 tree *q[31] = {t},*p; int l = 0,r = 1; while(l < r) { p = q[l ++]; level[l - 1] = p -> data; if(p -> left) { if(flag == 2)flag = 0;///之前有空结点 那么肯定不是完全二叉树 q[r ++] = p -> left; } else if(flag == 1)flag = 2;///遇到空结点 if(p -> right) { if(flag == 2)flag = 0; q[r ++] = p -> right; } else if(flag == 1)flag = 2; }}int main() { scanf("%d",&n); for(int i = 0;i < n;i ++) { scanf("%d",&d); head = insert_(head,d); } level_order(head); for(int i = 0;i < n;i ++) { if(i)putchar(' '); printf("%d",level[i]); } puts(flag ? "\nYES" : "\nNO");}

 

 

转载于:https://www.cnblogs.com/8023spz/p/9278659.html

你可能感兴趣的文章
session和xsrf
查看>>
Linux目录结构
查看>>
luoguP3414 SAC#1 - 组合数
查看>>
五一 DAY 4
查看>>
【译】SSH隧道:本地和远程端口转发
查看>>
图片点击轮播(三)-----2017-04-05
查看>>
直播技术细节3
查看>>
《分布式服务架构:原理、设计于实战》总结
查看>>
java中new一个对象和对象=null有什么区别
查看>>
字母和数字键的键码值(keyCode)
查看>>
IE8调用window.open导出EXCEL文件题目
查看>>
Spring mvc初学
查看>>
有意思的代码片段
查看>>
C8051开发环境
查看>>
VTKMY 3.3 VS 2010 Configuration 配置
查看>>
01_1_准备ibatis环境
查看>>
windows中修改catalina.sh上传到linux执行报错This file is needed to run this program解决
查看>>
JavaScript中的BOM和DOM
查看>>
360浏览器兼容模式 不能$.post (不是a 连接 onclick的问题!!)
查看>>
spring注入Properties
查看>>