题目:

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的更多相关文章

  1. 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 ...

  2. 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, ...

  3. 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 ...

  4. PAT甲级1066. Root of AVL Tree

    PAT甲级1066. Root of AVL Tree 题意: 构造AVL树,返回root点val. 思路: 了解AVL树的基本性质. AVL树 ac代码: C++ // pat1066.cpp : ...

  5. 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 ...

  6. 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 ...

  7. pat1066. Root of AVL Tree (25)

    1066. Root of AVL Tree (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue An A ...

  8. pat 甲级 1066. Root of AVL Tree (25)

    1066. Root of AVL Tree (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue An A ...

  9. 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 ...

随机推荐

  1. Solidworks如何自动打开和关闭特征识别FeatureWorks

    如果直接对已有的零件识别特征,可能会报错   删除多余的特征,先只保留一个输入(注意没有必要连草图也删掉,草图不会影响识别特征,你识别完了之后草图再接着该拉伸拉伸,该切除切除),然后再次执行识别特征, ...

  2. ffmpeg相关资源

    FFPLAY的原理(一) http://blog.csdn.net/shenbin1430/article/details/4291893 ubuntu12.04下命令安装ffplay等: sudo ...

  3. java泛型介绍

    一.泛型初衷 Java集合不会知道我们需要用它来保存什么类型的对象,所以他们把集合设计成能保存任何类型的对象,只要就具有很好的通用性.但这样做也带来两个问题: –集合对元素类型没有任何限制,这样可能引 ...

  4. bitbucket git push 项目503失败

    忙活了一天,要把项目push到远程仓库保存: 一直稳定的bitbucket今天突然push不成功了,出了503错误 谷歌了各种原因,还以为是连接出了问题 打开bitbucket State 页面之后, ...

  5. Linux下好用的命令

    split -l 10000 articles.json  将文件按行分成多个文件

  6. Android实战技巧之三十八:Handler使用中可能引发的内存泄漏

    问题描写叙述 曾几何时,我们用原来的办法使用Handler时会有以下一段温馨的提示: This Handler class should be static or leaks might occur ...

  7. JS判断字符串变量是否含有某个字串的实现方法

    JS判断字符串变量是否含有某个字串的实现方法 varCts = "bblText"; if(Cts.indexOf("Text") > 0 ){ aler ...

  8. 最接近WeChat的全屏自定义相机(Custom Camera)

    代码地址如下:http://www.demodashi.com/demo/13271.html 一.需求 最接近WeChat的全屏自定义相机(Custom Camera),拍照和预览都是全屏尺寸.使用 ...

  9. 阿里云云盘扩容数据盘_Linux

    随着业务的增长,您的数据盘容量可能无法满足数据存储的需要,这时您可以使用 磁盘扩容 功能扩容数据盘.   说明 挂载在实例上的数据盘,只有当实例处于 运行中 (Running) 或 已停止(Stopp ...

  10. 关于数组中加入相同的view的试验

    随便新建一个工程,然后在控制器中粘贴如下代码 - (void)viewDidLoad { [super viewDidLoad]; UIView * view = [[UIView alloc]ini ...