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

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

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
 
题意:
本题是1066题和1110题的综合,通过给定的插入序列,首先需要正确构建AVL树;其次判断该树是否为完全二叉树。
 
思路:
略。看1066题和1110题的题解即可。
 
代码:
#include 
#include
#include
using namespace std;struct Node{ int val; int height;//以该结点为根结点的子树高度 Node *lchild,*rchild; Node(int v):val(v),height(1),lchild(nullptr),rchild(nullptr){}};int n;//结点个数int getHeight(Node* pNode){ if(pNode==nullptr) return 0; else return pNode->height;}int getBalancedFactor(Node* pNode){ return getHeight(pNode->lchild)-getHeight(pNode->rchild);}void updateHeight(Node* pNode){ pNode->height=max(getHeight(pNode->lchild),getHeight(pNode->rchild))+1;}void leftRotation(Node* &pNode){ Node* temp=pNode->rchild; pNode->rchild=temp->lchild; temp->lchild=pNode; updateHeight(pNode); updateHeight(temp); pNode=temp;}void rightRotation(Node* &pNode){ Node* temp=pNode->lchild; pNode->lchild=temp->rchild; temp->rchild=pNode; updateHeight(pNode); updateHeight(temp); pNode=temp;}void insert(Node* &root,int val){ if(root==nullptr){ root=new Node(val); return; } if(val < root->val){ insert(root->lchild,val); updateHeight(root); if(getBalancedFactor(root)==2){ if(getBalancedFactor(root->lchild)==1){
//LL型 rightRotation(root); }else if(getBalancedFactor(root->lchild)==-1){
//LR型 leftRotation(root->lchild); rightRotation(root); } } }else{ insert(root->rchild,val); updateHeight(root); if(getBalancedFactor(root)==-2){ if(getBalancedFactor(root->rchild)==-1){
//RR型 leftRotation(root); }else if(getBalancedFactor(root->rchild)==1){
//RL型 rightRotation(root->rchild); leftRotation(root); } } }}//层序遍历,并判断是否为完全二叉树bool levelOrderTraversal(Node* root){ int cnt=0; bool flag=true; queue
q; q.push(root); while(!q.empty()){ Node* temp=q.front(); q.pop(); if(temp){ printf("%d",temp->val); cnt++; if(cnt
lchild); q.push(temp->rchild); }else{ if(cnt

 

转载于:https://www.cnblogs.com/kkmjy/p/9528326.html

你可能感兴趣的文章
十四、curator recipes之DistributedAtomicLong
查看>>
模板中的名字查找问题
查看>>
StringUtils工具类用法
查看>>
QListView的子项的ViewMode
查看>>
erlang的erl文件的编码方式
查看>>
Kosaraju算法解决强连通问题
查看>>
做一个完整的Java Web项目需要掌握的技能
查看>>
win7下安装docker的一些坑
查看>>
Java:正则表达式
查看>>
tensorflow
查看>>
mysql分表的3种方法
查看>>
TableControl大小变化
查看>>
返回脚本所在目录
查看>>
[LeetCode] No. 9 Palindrome Number
查看>>
(转)GCT之逻辑经验总结(拿来主义)
查看>>
虚拟继承中子类和父类的构造函数顺序1
查看>>
js错误: Unexpected number in JSON at position 2792 value里面有双引号怎么解决
查看>>
(实践篇)剖析最近项目使用的一个框架
查看>>
tigerVNC的简单使用教程(CentOS的远程桌面连接)
查看>>
组合数据类型综合练习:英文词频统计
查看>>