#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. bzoj2564: 集合的面积(闵可夫斯基和 凸包)

    题面 传送门 题解 花了一个下午的时间调出了一个稍微能看的板子--没办法网上的板子和咱的不太兼容-- 首先有一个叫做闵可夫斯基和的东西,就是给你两个点集\(A,B\),要你求一个点集\(C=\{x+y ...

  2. JDBC_PreparedStatement用法_占位符_参数处理

    import java.sql.Connection; import java.sql.Date;import java.sql.DriverManager;import java.sql.Prepa ...

  3. 一款很好用的页面滚动元素动画插件-AOS.JS

    aos.js是一款效果超赞的页面滚动元素动画jQuery动画库插件.该动画库可以在页面滚动时提供28种不同的元素动画效果,以及多种easing效果.在页面往回滚动时,元素会恢复到原来的状态. 加载方法 ...

  4. app.use和app.get的区别及解析

    转载至:http://blog.csdn.net/wthfeng/article/details/53366169 写在前面:最近研究nodejs及其web框架express,对app.use和app ...

  5. P2057 [SHOI2007]善意的投票 (最大流)

    题目 P2057 [SHOI2007]善意的投票 解析 网络流的建模都如此巧妙. 我们把同意的意见看做源点\(s\),不同意的意见看做汇点\(t\). 那我们\(s\)连向所有同意的人,\(t\)连向 ...

  6. AJAX中success函数的执行顺序

    1,问题,在上图中,数据传输正常,但是一直输出为空的p,再输出66, 2,原因,默认开启了异步加载, 3,解决方法,开启同步,或者在success中添加函数.

  7. Qt 学习之路 2(43):QStringListModel

    Qt 学习之路 2(43):QStringListModel 豆子 2013年2月13日 Qt 学习之路 2 38条评论 上一章我们已经了解到有关 list.table 和 tree 三个最常用的视图 ...

  8. Angular 组件 mat-paginator 自定义详细用法

    Demo: https://stackblitz.com/edit/angular-5mgfxh?file=main.ts 官方文档: https://material.angular.io/comp ...

  9. Sasha and a Bit of Relax(前缀异或和+二维数组+思维)

    Sasha likes programming. Once, during a very long contest, Sasha decided that he was a bit tired and ...

  10. 页面加载时的div动画

    用@keyframes(动画),实现页面加载时的div动画(不要用js控制,因为当页面加载的时候,js还不一定可以使用) 可以在https://daneden.github.io/animate.cs ...