PAT004 Root of AVL Tree
题目:
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 tell the root of the resulting AVL tree.
Input Specification:
Each input file contains one test case. For each case, the first line contains a positive integer N (<=20) which is the total number of keys to be inserted. 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, print ythe root of the resulting AVL tree in one line.
Sample Input 1:
5
88 70 61 96 120
Sample Output 1:
70
Sample Input 2:
7
88 70 61 96 120 90 65
Sample Output 2:
88
分析:主要是训练平衡树的基本操作,四种旋转方式。
代码:
#include <stdio.h>
typedef struct treeNode {
int data;
struct treeNode *left;
struct treeNode *right;
int height;
} AVLTreeNode; // 在PAT提交时出现MAX宏未定义的编译错误,故添加以下几行代码
#ifndef MAX
#define MAX(A, B) ((A) > (B) ? (A) : (B))
#endif // 获取节点高度
int GetHeight(AVLTreeNode *treeNode)
{
if (!treeNode) {
return ;
} else {
return MAX(GetHeight(treeNode->left), GetHeight(treeNode->right)) + ;
}
} AVLTreeNode *SingleLeftRotation(AVLTreeNode *A)
{
AVLTreeNode *B = A->left;
A->left = B->right;
B->right = A;
A->height = MAX(GetHeight(A->left), GetHeight(A->right)) + ;
B->height = MAX(GetHeight(B->left), GetHeight(B->right)) + ;
return B;
} AVLTreeNode *SingleRightRotation(AVLTreeNode *A)
{
AVLTreeNode *B = A->right;
A->right = B->left;
B->left = A;
A->height = MAX(GetHeight(A->left), GetHeight(A->right)) + ;
B->height = MAX(GetHeight(B->left), GetHeight(B->right)) + ;
return B;
} AVLTreeNode *DoubleLeftRightRotation(AVLTreeNode *A)
{
A->left = SingleRightRotation(A->left);
return SingleLeftRotation(A);
} AVLTreeNode *DoubleRightLeftRotation(AVLTreeNode *A)
{
A->right = SingleLeftRotation(A->right);
return SingleRightRotation(A);
} // 将data插入到AVL树tree中,并返回调整后的AVL树
AVLTreeNode *AVL_insertion(int data, AVLTreeNode *tree)
{
if (!tree) { // 若插入到空树中,新建一个节点
tree = (AVLTreeNode *)malloc(sizeof(AVLTreeNode));
tree->data = data;
tree->height = ;
tree->left = tree->right = NULL;
} else if (data < tree->data) { // 插入到左子树中
tree->left = AVL_insertion(data, tree->left);
if (GetHeight(tree->left) - GetHeight(tree->right) == ) { // 需要左旋
if (data < tree->left->data) { // 左单旋
tree = SingleLeftRotation(tree);
} else { // 左右双旋
tree = DoubleLeftRightRotation(tree);
}
}
} else if (data > tree->data) { // 插入到右子树中
tree->right = AVL_insertion(data, tree->right);
if (GetHeight(tree->right) - GetHeight(tree->left) == ) { // 需要右旋
if (data > tree->right->data) { //右单旋
tree = SingleRightRotation(tree);
} else {
tree = DoubleRightLeftRotation(tree); // 右左旋
}
}
} /* else data == tree->data 无需插入*/ tree->height = MAX(GetHeight(tree->left), GetHeight(tree->right)) + ; return tree;
} int main()
{
// 读取输入
int count = ;
scanf("%d", &count); AVLTreeNode *tree = NULL;
for (int i = ; i < count; i++) {
int data = ;
scanf("%d", &data);
tree = AVL_insertion(data, tree);
}
printf("%d", tree->data);
}
运行结果:
PAT004 Root of AVL Tree的更多相关文章
- 04-树5 Root of AVL Tree + AVL树操作集
平衡二叉树-课程视频 An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the tw ...
- PAT 1066 Root of AVL Tree[AVL树][难]
1066 Root of AVL Tree (25)(25 分) An AVL tree is a self-balancing binary search tree. In an AVL tree, ...
- PTA (Advanced Level) 1066 Root of AVL Tree
Root of AVL Tree An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of ...
- PAT甲级1066. Root of AVL Tree
PAT甲级1066. Root of AVL Tree 题意: 构造AVL树,返回root点val. 思路: 了解AVL树的基本性质. AVL树 ac代码: C++ // pat1066.cpp : ...
- 04-树4. Root of AVL Tree (25)
04-树4. Root of AVL Tree (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue An A ...
- pat04-树4. Root of AVL Tree (25)
04-树4. Root of AVL Tree (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue An A ...
- pat1066. Root of AVL Tree (25)
1066. Root of AVL Tree (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue An A ...
- pat 甲级 1066. Root of AVL Tree (25)
1066. Root of AVL Tree (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue An A ...
- Root of AVL Tree
04-树5 Root of AVL Tree(25 分) An AVL tree is a self-balancing binary search tree. In an AVL tree, the ...
随机推荐
- Android异步载入全解析之大图处理
Android异步载入全解析之大图处理 异步载入中很重要的一部分就是对图像的处理,这也是我们前面用异步载入图像做示例的原因. 一方面是由于图像处理不好的话会很占内存,并且easyOOM,还有一方面,图 ...
- sublime text 2中“ctrl + `”快捷键无效
之前sublime 使用正常,这次在装插件的时候,发现ctrl + `快捷键失效了,无法调出控制台. 然后就一直按这两个键,肯定是被别的占用了,所以就像看看有啥反应,看了半天都没有见到什么神奇的窗口跳 ...
- python3带参数的装饰器 函数参数类型检查
from inspect import signature#python3才有的模块 def typeassert(*args,**kwargs): def decorator(fun): sig=s ...
- UITableView Scroll to top 手动设置tableview 滚动到 顶部
UITableView Scroll to top 手动设置tableview 滚动到 顶部 [mainTableView scrollRectToVisible:CGRectMake(0,0,1,1 ...
- HTTP缓存策略 304
1.图解缓存 示例: 200 (from disk cache): 200 (from memory cache) MemoryCache顾名思义,就是将资源缓存到内存中,等待下次访问时不需要重新下载 ...
- 关于华为x1 7.0无法从eclipse发布的更新as发布的apk
目前只在华为x1 7.0手机上发现这个问题,坑大了. MediaPad 10 FHD 华为这款可以安装. HUAWEI G525-U00 华为这款也可以安装. 目前公司就这几款华为手机了. 原因是 在 ...
- 【VBA编程】08.数组
[数组简介]数组其实就是一组相同类型的数据的有序集合,其形象表示就像线性表.在存储数据的时候,首先在内存中分配一个连续的存储空间,将各个元素按顺序存放在连续的存储单元格中.[定义静态数组]Dim 数据 ...
- 02-spring学习-配置bean
在spring的IOC容器中配置Bean 一,在xml中通过bean节点来配置bean: class:bean的类名,通过反射的方式在IOC容器中创建Bean,所以要求bean中必须有无参的构造器 i ...
- winfrom cahce 问题
.Clear()不能随便用 .Clear()的比较没有什么意思,因为只是把DataTable清空而已,在堆中任然分配内存,一般要比较也是比较Close()方法,不过DataTable没有这个方法 至于 ...
- R语言的if-else
语法问题,类似于for函数使用时必须把"{"放在当前行,而不可以独占一行.R的特色. 具体你的这个问题可以尝试 x <- 5 if (x>0){ print(" ...