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 ...
随机推荐
- 第八届山东ACM省赛F题-quadratic equation
这个题困扰了我长达1年多,终于在今天下午用两个小时理清楚啦 要注意的有以下几点: 1.a=b=c=0时 因为x有无穷种答案,所以不对 2.注意精度问题 3.b^2-4ac<0时也算对 Probl ...
- SpringMVC中web.xml的配置(直接coppy使用就行)
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://w ...
- 基于python-opencv3的图像显示和保存操作
import cv2 as cv import numpy as np #导入库 print("------------------------ ...
- vue 在路由中复用组件
首先需要在app.vue中引入: <template> <div id="app"> <!--<app-header></app-h ...
- 二 ASP.NET MVC 第一个程序 hello world
二 ASP.NET MVC 第一个程序 hello world https://blog.csdn.net/xmroom/article/details/51335917 我使用的Visual s ...
- C# 函数参数object sender, EventArgs e
object sender:表示触发事件的控件对象EventArgs e:表示事件数据的类的基类 Windows程序有一个事件机制.用于处理用户事件. 在WinForm中我们经常需要给控件添加事件.例 ...
- [HNOI/AHOI2018]排列
[Luogu4437] 如果\(a[i]=j\)则序列\(p[]\)中\(j\)必须排在\(i\)前面,如果\(j\)不在范围内则不管,求一个式子\(\sum_{i=1}^n iw_{p[i]}\)的 ...
- P4592 [TJOI2018]异或 (可持久化Trie)
[题目链接] https://www.luogu.org/problemnew/show/P4592 题目描述 现在有一颗以\(1\)为根节点的由\(n\)个节点组成的树,树上每个节点上都有一个权值\ ...
- Bubble Sort Graph CodeForces - 340D || 最长不下降/上升子序列
Bubble Sort Graph CodeForces - 340D 题意: 给出一个n个数的数列,建一个只有n个结点没有边的无向图,对数列进行冒泡排序,每交换一对位置在(i,j)的数在点i和点j间 ...
- pyquery的简单操作
一.初始化 1.html初始化 html = ''' <div> <ul> <li class="item-0">first item</ ...