当前位置:网站首页>1066 Root of AVL Tree // AVL平衡二叉搜索树模板
1066 Root of AVL Tree // AVL平衡二叉搜索树模板
2022-08-03 05:33:00 【Brosto_Cloud】
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 the 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 <iostream>
#include <algorithm>
using namespace std;
int n, x;
struct node {
int val;
node *left, *right;
};
node *rotateLeft(node *root) {
node *t = root->right;
root->right = t->left;
t->left = root;
return t;
}
node *rotateRight(node *root) {
node *t = root->left;
root->left = t->right;
t->right = root;
return t;
}
node *rotateLeftRight(node *root) {
root->left = rotateLeft(root->left);
return rotateRight(root);
}
node *rotateRightLeft(node *root) {
root->right = rotateRight(root->right);
return rotateLeft(root);
}
int height(node *root) {
if (root == NULL) {
return 0;
} else {
return max(height(root->left), height(root->right)) + 1;
}
}
node *insert(node *root, int val) {
if (root == NULL) {
root = new node();
root->val = val;
root->right = root->left = NULL;
} else if (val < root->val) {
root->left = insert(root->left, val);
if (height(root->left) - height(root->right) == 2) {
if (val < root->left->val) {
root = rotateRight(root);
} else {
root = rotateLeftRight(root);
}
}
} else {
root->right = insert(root->right, val);
if (height(root->right) - height(root->left) == 2) {
if (val > root->right->val) {
root = rotateLeft(root);
} else {
root = rotateRightLeft(root);
}
}
}
return root;
}
int main() {
cin >> n;
node *root = NULL;
for (int i = 0; i < n; i++) {
cin >> x;
root = insert(root, x);
}
cout << root->val;
return 0;
}边栏推荐
猜你喜欢
随机推荐
pyspark @udf 循环使用变量问题
【云原生 · Kubernetes】搭建Harbor仓库
El - table column filter functions, control columns show and hide (effect and easy to implement full marks)
UniApp scroll-view 事件不生效(@scroll、@scrolltolower、@scrolltoupper ...)
Getting Started with Chrome Plugin Development
html+css+php+mysql实现注册+登录+修改密码(附完整代码)
【onnx 输入尺寸】修改pytorch生成的onnx模型的输入尺寸
微信小程序 - 监听 TabBar 切换点击事件
Pinned Articles-
MySQL 日期时间类型精确到毫秒
UniApp 自定义条件编译详细使用流程
Embedding two implementations of the torch code
保姆级讲解Transformer
pyspark df 二次排序
配置MSTP功能示例
信息学奥赛一本通T1451:棋盘游戏
一根网线完美解决IPTV+千兆网复用,还不来试试
Redis-记一次docker下使用redis
【干货分享】PCB 板变形原因!不看不知道
一家可靠的HDI板厂,需要具备哪些基本条件?









