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

判断是否完全二叉树:

层序遍历时,若前面已经出现过空子树,而后面又发现了非空子树,则不是完全二叉树。

平衡二叉树的旋转:

1、右单旋

2、左单旋

3、左右双旋

4、右左双旋

 #include <iostream>
#include <vector>
#include <queue>
using namespace std; typedef struct Node *Tree;
struct Node
{
int data;
Tree left;
Tree right;
}; Tree insert(Tree T, int data);
int getHeight(Tree T);
Tree LL(Tree T);
Tree LR(Tree T);
Tree RR(Tree T);
Tree RL(Tree T);
vector<int> levelOrder(Tree T, bool& isComplete); int main()
{
int N, i, t;
cin >> N;
Tree root = NULL;
for (i = ; i < N; i++)
{
cin >> t;
root = insert(root, t);
}
bool isComplete;
vector<int> v = levelOrder(root, isComplete);
cout << v[];
for (i = ; i < v.size(); i++)
cout << " " << v[i];
printf("\n%s", isComplete ? "YES" : "NO");
return ;
} Tree insert(Tree T, int data)
{
if (T == NULL)
{
T = new Node;
T->data = data;
T->left = T->right = NULL;
}
else if (data < T->data)
{
T->left = insert(T->left, data);
if (getHeight(T->left) - getHeight(T->right) == )
{
if (data < T->left->data)
T = LL(T);
else
T = LR(T);
}
}
else
{
T->right = insert(T->right, data);
if (getHeight(T->right) - getHeight(T->left) == )
{
if (data > T->right->data)
T = RR(T);
else
T = RL(T);
}
}
return T;
} int getHeight(Tree T)
{
if (T == NULL) return ;
int a = getHeight(T->left);
int b = getHeight(T->right);
return (a > b) ? (a + ) : (b + );
} Tree LL(Tree T)
{
Tree tmp = T->left;
T->left = tmp->right;
tmp->right = T;
return tmp;
} Tree LR(Tree T)
{
T->left = RR(T->left);
return LL(T);
} Tree RR(Tree T)
{
Tree tmp = T->right;
T->right = tmp->left;
tmp->left = T;
return tmp;
} Tree RL(Tree T)
{
T->right = LL(T->right);
return RR(T);
} vector<int> levelOrder(Tree T, bool& isComplete)
{
vector<int> v;
isComplete = true;
queue<Tree> q;
q.push(T);
bool hasEmpty = false;
while (!q.empty())
{
Tree T = q.front();
q.pop();
v.push_back(T->data);
if (T->left != NULL)
{
q.push(T->left);
//已经出现过空子树但是现在子树又不空,那么不是完全二叉树
if (hasEmpty) isComplete = false;
}
else
hasEmpty = true;
if (T->right != NULL)
{
q.push(T->right);
if (hasEmpty) isComplete = false;
}
else
hasEmpty = true;
}
return v;
}

最新文章

  1. C#中override和new修饰符的区别
  2. nginx服务器在IE下载时,apk,ipa文件变成zip的解决方法
  3. ipa上传到APP store
  4. -高级Javascript编程学习笔记----Javascript编程及架构设计最应该注意的基本点
  5. intent传递参数
  6. 【转载】va_list 可变参数 简介 va_copy vprintf
  7. 惠普 hpacucli工具使用
  8. arm get_vector_swi_address
  9. Unix/Linux环境C编程入门教程(15) BT5开发环境搭建
  10. poj2987 Firing
  11. 纯CSS3打造七巧板
  12. python web -- flask
  13. k2datas 基础编程题,判断字符串是否有重复串
  14. CVE-2012-0158 分析
  15. linux文件或目录权限修改后如何恢复(备份了权限就能恢复)
  16. [Web 前端] mobx教程(三)-在React中使用Mobx
  17. 02Spark的左连接
  18. linux --nginx篇
  19. javascript中scrollTop和offsetTop的区别
  20. Spring4自动装配(default-autowire)

热门文章

  1. webconfig配置详解--转
  2. Hadoop 对MapReduce的理解
  3. Centos7 安装MySQL 5.7 (通用二进制包)
  4. Solr 6.7学习笔记(02)-- 配置文件 managed-schema (schema.xml)(1)
  5. bzoj4435: [Cerc2015]Juice Junctions(最小割树+hash)
  6. oracle例程
  7. Python Day24
  8. Jmeter report优化
  9. LeetCode初级算法(数组)解答
  10. Angular学习笔记 ——input 标签上的【name属性】和【ngModelOptions属性】