04-树4. Root of AVL Tree (25)
04-树4. 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 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 ythe 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 <stdio.h>
struct Node {int val;int height;struct Node *left;struct Node *right;
};
int max(int a, int b) { //返回两者较大者return a > b ?
a : b; } int height(struct Node* root) { //为了兼容空树,树高度不能直接返回根节点的height属性 if (root == NULL) { return -1; } else { return root->height; } } struct Node* RRrotation(struct Node* k1) { //右右旋转 struct Node* k2 = k1->right; //k2为根节点k1的右儿子 k1->right = k2->left; //将k2的左儿子连接到k1的右子节点 k2->left = k1; //将k1连接到k2的左子节点 k1->height = max(height(k1->left), height(k1->right)) + 1; //更新节点高度,仅仅有k1,k2节点高度变化 k2->height = max(height(k2->left), height(k2->right)) + 1; return k2; } struct Node* LLrotation(struct Node* k1) { //左左旋转 struct Node* k2 = k1->left; k1->left = k2->right; k2->right = k1; k1->height = max(height(k1->left), height(k1->right)) + 1; k2->height = max(height(k2->left), height(k2->right)) + 1; return k2; } struct Node* RLrotation(struct Node* k1) { //右左旋转 //分两步:先对根节点的右子树做左左旋转。再对根做右右旋转 k1->right = LLrotation(k1->right); return RRrotation(k1); } struct Node* LRrotation(struct Node* k1) { //左右旋转 k1->left = RRrotation(k1->left); return LLrotation(k1); } struct Node* insertAvlTree(struct Node* node, struct Node* root) { if (root == NULL) { root = node; return root; } if (node->val > root->val) { root->right = insertAvlTree(node, root->right); //插入右子树 if (height(root->right) - height(root->left) == 2) { if (node->val > root->right->val) { //假设插入右子树的右子树,进行右右旋转 root = RRrotation(root); } else if (node->val < root->right->val) { //进行右左旋转 root = RLrotation(root); } } } else if (node->val < root->val) { //插入左子树情况与上面相似 root->left = insertAvlTree(node, root->left); if (height(root->left) - height(root->right) == 2) { if (node->val < root->left->val) { root = LLrotation(root); } else if(node->val > root->left->val) { root = LRrotation(root); } } } //递归中不断更新插入节点到根节点路径上全部节点的高度 root->height = max(height(root->left), height(root->right)) + 1; return root; } int main() { freopen("test.txt", "r", stdin); int n; scanf("%d", &n); struct Node nodes[20]; struct Node *root = NULL; for (int i = 0; i < n; ++i) { //初始化一个节点。并插入AVL树中 scanf("%d", &nodes[i].val); nodes[i].height = 0; //孤立的节点高度为0 nodes[i].left = NULL; nodes[i].right = NULL; root = insertAvlTree(&nodes[i], root); } printf("%d", root->val); return 0; }
题目链接:http://www.patest.cn/contests/mooc-ds/04-%E6%A0%914
04-树4. Root of AVL Tree (25)相关推荐
- PAT甲级1066 Root of AVL Tree (25分):[C++题解]建立平衡树(AVL树)
文章目录 题目分析 题目链接 题目分析 图片来源:acwing 分析 平衡树(AVL树)是平衡二叉搜索树的简称,当然需要满足二叉搜索树的性质,左子树小于根,根小于等于右子树:然后还要满足平衡树的基本特 ...
- pat04-树4. Root of AVL Tree (25)
04-树4. Root of AVL Tree (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue An A ...
- PAT 1066. Root of AVL Tree (25) 回レ!雪月AVL
1066. Root of AVL Tree (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue An A ...
- PAT1066 Root of AVL Tree (25)(AVL树)
题意: 给出一系列要插入平衡搜索二叉树的数,要求输出最后的根节点 思路: 没其他办法,完完全全是AVL树的插入节点模拟,这题就不会写,看别人代码写的. #include<iostream> ...
- 1066 Root of AVL Tree (25 分)【难 / 知识点: 平衡树 未完成】
https://pintia.cn/problem-sets/994805342720868352/problems/994805404939173888 平衡树之前学过,不过有忘完了,有时间补吧
- 1066 Root of AVL Tree——PAT甲级 | 参考mooc实现完整代码
Root of AVL Tree 2013年浙江大学计算机学院免试研究生上机考试真题,是关于AVL树的基本训练. 原题链接:PTA | 程序设计类实验辅助教学平台 题目描述 AVL 树是一种自平衡的 ...
- PAT A1066 Root of AVL Tree ——春水碧于天,画船听雨眠
PAT A1066 Root of AVL Tree AVL这东西记一次忘一次,每次看就像披着初恋外衣的旧情人(or reverse) 以下应该是较为标准的模板方法,只是好久没有用过指针了,所以写了个 ...
- 平衡查找树C语言程序,树4. Root of AVL Tree-平衡查找树AVL树的实现
对于一棵普通的二叉查找树而言,在进行多次的插入或删除后,容易让树失去平衡,导致树的深度不是O(logN),而接近O(N),这样将大大减少对树的查找效率.一种解决办法就是要有一个称为平衡的附加的结构条件 ...
- 【PAT A1066】Root of AVL Tree
#include <cstdio> #include <algorithm> using namespace std;struct node {int v, height; / ...
- 1066 Root of AVL Tree 需再做
1. 这题如果不知道平衡二叉树怎么平衡的(左旋右旋那一套)应该不可能做出吧,那就输出中位数回点血了. 2. 需要具备的基础知识:怎么将结点插入平衡二叉树. 3. 我犯的一个错误:把更新高度的函数直接返 ...
最新文章
- R语言ggplot2可视化:将条形图(bar plot)和线图(line plot)组合在一起并使用双Y轴(double y axis)进行可视化、其中一个Y轴显示为百分比
- 卓越性能代码_「Win」被隐藏起来的卓越性能模式,为何不想让人发现?
- spark和HSQL的连接join方式
- android聚焦时如何给控件加边框,edittext设置获得焦点时的边框颜色
- 利用Javascrip实现web窗体的打开和关闭后的刷新
- cNoteSetColor_命令窗口颜色设置
- JS 数组(遍历 二维数组)
- Java中多个pdf文件合并为一个
- c语言main的作用是什么,c语言main是什么意思-与非网
- 利用python提取视频中的字幕
- 关于nomogram核心函数的time.inc函数的设定
- 花花世界的flowers in December
- mysql tgz 安装_mysql的安装
- 使用CMD更改IP地址
- 信息学奥赛一本通——2068:【例2.6】鸡兔同笼
- 微信红包助手php,php生成微信红包
- C++ 类型转换(static_cast、dynamic_cast、reinterpret_cast、const_cast)
- 为何说买5G手机只能买iPhone,不能买国产5G手机?
- JAVA JSP幼儿园事务管理系统JSP早教中心网站系统 JSP幼儿园网站JSP幼儿园管理系统
- xshell连接服务器提示拒绝密码
热门文章
- linux ssh x11,ssh服务器的x11 forwarding报错的解决
- python3小程序代码_我想使用python写一个小程序作为练习,使用的是python3。
- php valid函数,PHP函数uasort()在类中的使用问题解决
- AC日记——【模板】二分图匹配 洛谷 P3386
- Spring Cloud 与 Dubbo 对比整理(2)
- 浅谈管道模型(Pipeline)
- 图像处理之基础---ffmpeg 中的图像缩放
- delphi xe6 让 ListView 在 Android 可回弹[根据龟山阿卍原创修改为xe6版本]
- Quartus II 的MegaWizard字体过小
- StringJoiner