题目:

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. Java死锁举例

    死锁: 在多线程竞争使用共享资源的情况下.就有可能出现死锁的情况.比方,当一个线程等待还有一个线程所持有的锁时.那个线程又可能在等待第一个线程所持有的锁.此时.这两个线程会陷入无休止的相互等待状态.这 ...

  2. 内网渗透技巧:判断机器真实外网IP的5种方法总结

    在内网渗透中有时需要在某台WEB服务器中留下后门,该机器可以通过内网IP建立IPC连接,但还需要获知外网IP或域名才能访问Wbshell,在无网关权限的情况下,我总结了有如下方法: 1.通过nsloo ...

  3. [转]bing壁纸天天换 初识shell魅力

    原文链接:http://www.cnblogs.com/atskyline/p/3679522.html 原文的程序跑在window上,curl的使用不太一样,想要获取的图片也不太一样.修改后的代码如 ...

  4. vue - dist

    描述:打包后准备上线的文件(需要服务器环境才能运行!!!)

  5. gensim自然语言处理(续)

    上一篇,已经实现了如何将一条语句在一个语料库中比较相似度, 发现运行的时候每次都要编译语料库,通过查找资料,可以一次性编译成预料库,存人文件 编译语料库代码 11_k.py import sysimp ...

  6. unity3d格式的导出与加载

    http://blog.csdn.net/nateyang/article/details/7567831 1.导出.unity3d格式资源: http://game.ceeger.com/Scrip ...

  7. 运用JMX监控Tomcat

    1.先配Tomcat的启动语句,window下tomcat的bin/catalina.bat(linux为catalina.sh),在头上注释部分(.bat为rem..sh为#)后面加上set JAV ...

  8. c++中的对象复制

    (1)this指针 this是一个隐含于每个类的成员函数的特殊指针,该指针是一个指向正在被某个成员函数操作的对象的指针. 当一个对象调用成员函数时,编译程序先将对象的地址赋给this指针,也就是说,当 ...

  9. Building An Effective Marketing Plan

    “New ideas are a dime a dozen,” observes Arthur R. Kydd, “and so are new products and new technologi ...

  10. Hibernate继承类的实现

    版权声明:本文为博主原创文章,如需转载请标注转载地址. 博客地址:http://www.cnblogs.com/caoyc/p/5603724.html  对于继承关系类的映射.比如在论坛中文章(Ar ...