04-树4. Root of AVL Tree (25)

时间限制
100 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard
作者
CHEN, Yue

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>
struct Node {
int val;
int height;
struct Node *left;
struct Node *right;
};
int max(int a, int b) { //返回两者较大者
return a > b ? a : b;
}
int height(struct Node* root) { //为了兼容空树,树高度不能直接返回根节点的height属性
if (root == NULL) {
return -1;
}
else {
return root->height;
}
}
struct Node* RRrotation(struct Node* k1) { //右右旋转
struct Node* k2 = k1->right; //k2为根节点k1的右儿子
k1->right = k2->left; //将k2的左儿子连接到k1的右子节点
k2->left = k1; //将k1连接到k2的左子节点
k1->height = max(height(k1->left), height(k1->right)) + 1; //更新节点高度,仅仅有k1,k2节点高度变化
k2->height = max(height(k2->left), height(k2->right)) + 1;
return k2;
}
struct Node* LLrotation(struct Node* k1) { //左左旋转
struct Node* k2 = k1->left;
k1->left = k2->right;
k2->right = k1;
k1->height = max(height(k1->left), height(k1->right)) + 1;
k2->height = max(height(k2->left), height(k2->right)) + 1;
return k2;
}
struct Node* RLrotation(struct Node* k1) { //右左旋转
//分两步:先对根节点的右子树做左左旋转。再对根做右右旋转
k1->right = LLrotation(k1->right);
return RRrotation(k1);
}
struct Node* LRrotation(struct Node* k1) { //左右旋转
k1->left = RRrotation(k1->left);
return LLrotation(k1);
}
struct Node* insertAvlTree(struct Node* node, struct Node* root) {
if (root == NULL) {
root = node;
return root;
}
if (node->val > root->val) {
root->right = insertAvlTree(node, root->right); //插入右子树
if (height(root->right) - height(root->left) == 2) {
if (node->val > root->right->val) { //假设插入右子树的右子树,进行右右旋转
root = RRrotation(root);
}
else if (node->val < root->right->val) { //进行右左旋转
root = RLrotation(root);
}
}
}
else if (node->val < root->val) { //插入左子树情况与上面相似
root->left = insertAvlTree(node, root->left);
if (height(root->left) - height(root->right) == 2) {
if (node->val < root->left->val) {
root = LLrotation(root);
}
else if(node->val > root->left->val) {
root = LRrotation(root);
}
}
}
//递归中不断更新插入节点到根节点路径上全部节点的高度
root->height = max(height(root->left), height(root->right)) + 1;
return root;
}
int main() {
freopen("test.txt", "r", stdin);
int n;
scanf("%d", &n);
struct Node nodes[20];
struct Node *root = NULL;
for (int i = 0; i < n; ++i) { //初始化一个节点。并插入AVL树中
scanf("%d", &nodes[i].val);
nodes[i].height = 0; //孤立的节点高度为0
nodes[i].left = NULL;
nodes[i].right = NULL;
root = insertAvlTree(&nodes[i], root);
}
printf("%d", root->val);
return 0;
}

题目链接:http://www.patest.cn/contests/mooc-ds/04-%E6%A0%914

04-树4. Root of AVL Tree (25)的更多相关文章

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

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

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

  3. PTA 04-树5 Root of AVL Tree (25分)

    题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/668 5-6 Root of AVL Tree   (25分) An AVL tree ...

  4. PAT甲级:1066 Root of AVL Tree (25分)

    PAT甲级:1066 Root of AVL Tree (25分) 题干 An AVL tree is a self-balancing binary search tree. In an AVL t ...

  5. pat1066. Root of AVL Tree (25)

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

  6. PAT 甲级 1066 Root of AVL Tree (25 分)(快速掌握平衡二叉树的旋转,内含代码和注解)***

    1066 Root of AVL Tree (25 分)   An AVL tree is a self-balancing binary search tree. In an AVL tree, t ...

  7. 1066 Root of AVL Tree (25分)(AVL树的实现)

    An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child sub ...

  8. PAT Advanced 1066 Root of AVL Tree (25) [平衡⼆叉树(AVL树)]

    题目 An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child ...

  9. PAT 1066. Root of AVL Tree (25)

    An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child sub ...

随机推荐

  1. 洛谷P4139 上帝与集合的正确用法 [扩展欧拉定理]

    题目传送门 上帝与集合的正确用法 题目描述 根据一些书上的记载,上帝的一次失败的创世经历是这样的: 第一天, 上帝创造了一个世界的基本元素,称做“元”. 第二天, 上帝创造了一个新的元素,称作“α”. ...

  2. 【BZOJ 3566】 3566: [SHOI2014]概率充电器 (概率树形DP)

    3566: [SHOI2014]概率充电器 Description 著名的电子产品品牌 SHOI 刚刚发布了引领世界潮流的下一代电子产品——概率充电器:“采用全新纳米级加工技术,实现元件与导线能否通电 ...

  3. bzoj1116 [POI2008]CLO 边双联通分量

    只需对每个联通块的$dfs$树检查有没有返租边即可 复杂度$O(n + m)$ #include <cstdio> #include <cstring> using names ...

  4. luoguP4206 [NOI2005]聪聪与可可 期望概率DP

    首先,分析一下这个猫和鼠 猫每局都可以追老鼠一步或者两步,但是除了最后的一步,肯定走两步快些.... 既然猫走的步数总是比老鼠多,那么它们的距离在逐渐缩小(如果这题只能走一步反而不能做了...) 猫不 ...

  5. 压测工具Siege

    一.下载 http://www.joedog.org/ http://www.joedog.org/pub/siege/siege-2.70.tar.gz 二.测试 siege -c200 -r10 ...

  6. CCTableView使用及其ViewSize动态调整

    cocos2dx的CCTableView使用及其ViewSize动态调整,直接上代码参考如下: // // summary : 水平滑动样式的TableView用法 void createGlobal ...

  7. JDK源码(1.7) -- java.util.AbstractCollection<E>

    java.util.AbstractCollection<E> 源码分析(JDK1.7) ------------------------------------------------- ...

  8. HDU 5640 King's Cake GCD

    King's Cake 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5640 Description It is the king's birthd ...

  9. MySQL单表恢复方法

    正休息的时候一个电话将我的睡意完全打散,“开发童鞋写update SQL的时候忘了加where条件了”,相信每一个DBA同学听到这个消息的时候都有骂街的冲动吧.万幸只是单表写花了,而不是哪位大神在DB ...

  10. 树莓派(Debian)系统开启iptables的raw表实现日志输出

    说明:可能Debian默认不开启iptables的raw表,所以无法通过其实现日志跟踪. 日志跟踪:http://www.cnblogs.com/EasonJim/p/8413563.html 解决方 ...