#include <cstdio>
#include <cstdlib> class Node {
public:
Node* L;
Node* R;
int height;
int data;
Node(int val, Node* l = NULL, Node* r = NULL, int h = ): data(val), L(l), R(r), height(h) {}
}; inline int height(Node* node) {
if (node == NULL) return -;
return node->height;
} inline int max(int a, int b) {return a>b?a:b;}
/* K2 is the first node violates the AVL property, K1 is its left node
violation is caused by insert a node into the K1's right sub-tree
(K2) (K1)
/ LL-rotate / \
(K1) --------------> (new) (K2)
/
(new)
*/
Node* rotateLL(Node* root) {
Node* K1 = root->L;
Node* K2 = root; Node* k1_rsub = K1->R;
K1->R = K2;
K2->L = k1_rsub; K1->height = max(height(K1->L), height(K1->R)) + ;
K2->height = max(height(K2->L), height(K2->R)) + ;
return K1;
} /* K1 is the first node violates the AVL property, K2 is its right node
violation is caused by insert a node into the K2's left sub-tree
(K1) (K2)
\ RR-rotate / \
(K2) ----------------> (K1) (new)
\
(new)
*/
Node* rotateRR(Node* root) {
Node* K1 = root;
Node* K2 = root->R;
Node* k2_lsub = K2->L;
K2->L = K1;
K1->R = k2_lsub; K1->height = max(height(K1->L), height(K1->R)) + ;
K2->height = max(height(K2->L), height(K2->R)) + ; return K2;
} /*
first do LL rotate on K3, then do RR rotate on K1
(K1) (K1) (K2)
\ \ / \
(K3) ------> (K2) --------> (K1) (K3)
/ \
(K2) (K3)
*/
Node* rotateRL(Node* root) {
Node* K1 = root;
Node* K2 = root->R->L;
Node* K3 = root->R; K1->R = rotateLL(K3);
return rotateRR(K1);
} /*
first do RR rotate on K1, then do LL rotate on K3
(K3) (K3) (K2)
/ / / \
(K1) ------> (K2) ------> (K1) (K3)
\ /
(K2) (K1)
*/
Node* rotateLR(Node* root) {
Node* K1 = root->L;
Node* K2 = root->L->R;
Node* K3 = root; K3->L = rotateRR(K1);
return rotateLL(K3);
} Node* insert(Node* root, int value) {
if (root == NULL) {
return new Node(value);
}
if (value < root->data) {
root->L = insert(root->L, value);
// do AVL property check
if (height(root->L) - height(root->R) == ) {
if (value < root->L->data) {
// LL case, single rotation
root = rotateLL(root);
} else if (value > root->L->data) {
// LR case, double rotation
root = rotateLR(root);
}
}
} else if (value > root->data ){
root->R = insert(root->R, value);
// do AVL property check
if (height(root->R) - height(root->L) == ) {
if (value > root->R->data) {
// RR case, single rotation
root = rotateRR(root);
} else if (value < root->R->data) {
// RL case, double rotation
root = rotateRL(root);
}
}
} else {
// equal, do nothing
} root->height= max(height(root->L), height(root->R)) + ;
return root;
} int main() {
Node* r = NULL; int N;
scanf("%d", &N);
for (int i=; i<N; i++) {
int d;
scanf("%d", &d);
r = insert(r, d);
}
if (r != NULL) {
printf("%d", r->data);
}
return ;
}

第一次自己写AVL树,参照照Data Structures and Alogrithm Analysis in C第二版中AVL树的代码

PAT 1066 Root of AVL Tree的更多相关文章

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

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

  3. PAT甲级1066. Root of AVL Tree

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

  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. pat 甲级 1066. 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. 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 ...

  8. PAT 甲级 1066 Root of AVL Tree

    https://pintia.cn/problem-sets/994805342720868352/problems/994805404939173888 An AVL tree is a self- ...

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

随机推荐

  1. bzoj 3864: Hero meet devil(dp套dp)

    题面 给你一个只由\(AGCT\)组成的字符串\(S (|S| ≤ 15)\),对于每个\(0 ≤ .. ≤ |S|\),问 有多少个只由\(AGCT\)组成的长度为\(m(1 ≤ m ≤ 1000) ...

  2. 小白如何将代码上传到github上?

    网上已经有很多关于这个的教程,有一步步操作的,但有些感觉已经颇旧了.现在更新一个最新版的github小白教程.尽管以后此教程也会变成旧的,至少在这一段时期,本文还是最新的.就按照github官网上教程 ...

  3. vmware vSphere克隆与快照技术

    通过Web vCenter我们可以很容易的对虚拟机进行管理,通过克隆技术或创建模板,我们可以迅速的创建虚拟机,我们也可以通过快照技术去捕获虚拟机的一些状态,比如说虚拟机的内存.设置或者虚拟磁盘的情况等 ...

  4. 190221协程与IO模型

    一.协程 又称微线程 协程是一种用户态的轻量级的线程 在单线程下实现的并发,例如:yield 优点: 无需线程上下文切换的开销 无需原子操作锁定及同步的开销 方便切换控制流,简化编程模型 高并发,高扩 ...

  5. 数据结构实验之图论七:驴友计划 ( 最短路径 Dijkstra 算法 )

    数据结构实验之图论七:驴友计划 Time Limit: 1000 ms           Memory Limit: 65536 KiB Submit Statistic Discuss Probl ...

  6. ajax beforeSend中无效果

    asnyc:false 与beforesend 同时使用 无效果

  7. js中自己实现bind函数的方式

    最近由于工作比较忙,好久都没时间静下心来研究一些东西了.今天在研究 call 和 apply 的区别的时候,看到 github 上面的一篇文章,看完以后,感觉启发很大. 文章链接为 https://g ...

  8. ubuntu系统下如何切换输入法

    如何切换输入法:ctrl+空格键 输入中文时如何翻页:键盘上的 - +两个键 super表示:美式键盘中的Win键

  9. ABP项目后台初始化数据库

    设置host为启动项,并修改连接字符串 在程序包管理控制台中,默认项目选中EFCore 执行Update-Database命令

  10. 危险系数(枚举点+bfs)--------蓝桥备战系列

    标题:危险系数 抗日战争时期,冀中平原的地道战曾发挥重要作用. 地道的多个站点间有通道连接,形成了庞大的网络.但也有隐患,当敌人发现了某个站点后,其它站点间可能因此会失去联系.        我们来定 ...