PAT 1066 Root of AVL Tree
#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的更多相关文章
- 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, ...
- 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 ...
- PAT甲级1066. Root of AVL Tree
PAT甲级1066. Root of AVL Tree 题意: 构造AVL树,返回root点val. 思路: 了解AVL树的基本性质. AVL树 ac代码: C++ // pat1066.cpp : ...
- 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 ...
- pat 甲级 1066. Root of AVL Tree (25)
1066. Root of AVL Tree (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue An A ...
- 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 ...
- 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 ...
- PAT 甲级 1066 Root of AVL Tree
https://pintia.cn/problem-sets/994805342720868352/problems/994805404939173888 An AVL tree is a self- ...
- 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 ...
随机推荐
- P2561 [AHOI2002]黑白瓷砖
$ \color{#0066ff}{ 题目描述 }$ \(\color{#0066ff}{输入格式}\) 文件中以一行的形式存放一个正整数 n , n ≤ 20 . \(\color{#0066ff} ...
- Python数组(二)
一.函数list 可将任何序列(如字符串)作为list的参数.list实际上是一个类,而不是函数. test=['java','C#','C','C++'] print(list(test)) ——& ...
- AJAX发送json,SpringMVC 接收JSON,@RequestBody
需求:JQuery ajax前台,采用 POST请求 发送json,后台使用SpringMVC接收json并处理 前台: $.ajax({ url:"请求地址", type:&qu ...
- Android蓝牙连接自动测试工具
蓝牙连接自动测试工具 1.需求产生 开发不按着需求走都是耍流氓且浪费时间.此工具的需求产生是研发人员在开发产品时涉及到蓝牙驱动和安卓蓝牙两个东西.但是呢,蓝牙不太稳定,那么工作来了.就需要研发人员一边 ...
- docker 安装 redis
docker拉去镜像以及配置生成容器的步骤几乎和之前的nginx安装一样,直接写下面的命令了 1. docker pull redis 2. docker run -p 6379:6379 -v /U ...
- 通过文件路径读取CSV表格内的数据
ReadDataFromCSV.h UCLASS() class MYPROJECT_API UReadDataFromCSV : public UBlueprintFunctionLibrary { ...
- LOJ6502. 「雅礼集训 2018 Day4」Divide(构造+dp)
题目链接 https://loj.ac/problem/6502 题解 中间一档部分分提示我们将所有的 \(w_i\) 排序. 考虑如果我们能构造出这样一个 \(w_i\) 的序列,使得该序列满足:对 ...
- Just a Hook(线段树区间修改值)-------------蓝桥备战系列
In the game of DotA, Pudge's meat hook is actually the most horrible thing for most of the heroes. T ...
- CodeChef - NWAYS 组合数 朱世杰恒等式
这道题目数据有坑,白浪费一个小时! 题意:求\(\sum_{i=1}^n\sum_{j=1}^n{|i-j|+k \choose k}\) 知识点: 朱世杰恒等式,\(\sum_{i=r}^n{i \ ...
- IOS下去掉input submit圆角和背景色错误
在iOS系统下input submit会有圆角,如果添加有背景色,背景色错误,在安卓系统是没有这些问题,可以在input样式加上这段样式 input{ -webkit-appearance: none ...