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 ...
随机推荐
- C# Meta Programming - Let Your Code Generate Code - 利用反射重写自动的ToString()
我们在写一些Model的时候,经常会重写ToString,为了在控制台中进行打印或者更好的单元测试. 但是,如果Model的字段非常多的时候,如此简单的重复劳动经常会变成一件令人头痛的事情,因为大家 ...
- vue - 实例事件
1.$on(在构造器外部添加事件) 2.$once(执行一次的事件) 3.$off(关闭事件) 4.$emit(事件调用) <!DOCTYPE html> <html lang=&q ...
- 算法笔记_084:蓝桥杯练习 11-1实现strcmp函数(Java)
目录 1 问题描述 2 解决方案 1 问题描述 问题描述 自己实现一个比较字符串大小的函数,也即实现strcmp函数.函数:int myStrcmp(char *s1,char *s2) 按照AS ...
- SS配置出错解决方法
{ "server":"0.0.0.0", "local_address":"127.0.0.1", "l ...
- HDU-1090-A+B for Input-Output Practice (II)(骗訪问量的)
A+B for Input-Output Practice (II) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/327 ...
- Twelves Monkeys (multiset解法 141 - ZOJ Monthly, July 2015 - H)
Twelves Monkeys Time Limit: 5 Seconds Memory Limit: 32768 KB James Cole is a convicted criminal ...
- An error occured while handling a json request
修复方法: sudo pip install werkzeug==0.8.3
- SQL中intersect、union、minus和except 运算符
1.intersect运算符intersect运算符通过只包括 TABLE1 和 TABLE2 中都有的行并消除所有重复行而派生出一个结果表.当 ALL 随 INTERSECT 一起使用时 (inte ...
- android获取系统应用大小的方法
<span style="font-family: Arial, Helvetica, sans-serif;"><span style="font-s ...
- sklearn基本回归方法
https://blog.csdn.net/u010900574/article/details/52666291 博主总结和很好,方法很实用. python一些依赖库: https://www.lf ...