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 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>
#include<vector>
#include<algorithm>
using namespace std; struct node
{
node(int v):left(NULL),right(NULL),high(),val(v){}
node* left,* right;
int val,high;
}; int gethigh(node* root)
{
int a = , b = ;
if(root->left!= NULL)
a = root->left->high;
if(root->right!= NULL)
b = root->right->high;
return a > b ? a+:b+;
} void R(node* & root)
{
node* tem = root->left;
root->left = tem->right;
tem->right = root;
root->high = gethigh(root);
tem->high = gethigh(tem);
root = tem;
} void L(node* & root)
{
node* tem = root->right;
root->right = tem->left;
tem->left = root;
root->high = gethigh(root);
tem->high = gethigh(tem);
root = tem;
} void insert(node*& root,int val)
{
if(root == NULL)
{
root = new node(val);
return;
} if(val < root->val)
{
insert(root->left,val);
root->high = gethigh(root);
int a = root->left == NULL ? : root->left->high;
int b = root->right == NULL ? : root->right->high;
if(a - b == )
{
int c = root->left->left == NULL ? :root->left->left->high;
int d = root->left->right == NULL ? :root->left->right->high;
if(c - d == )
{
R(root);
}
else if(c - d == -)
{
L(root->left);
R(root);
}
}
}
else
{
insert(root->right,val);
root->high = gethigh(root);
int a = root->left == NULL ? : root->left->high;
int b = root->right == NULL ? : root->right->high;
if(a - b == -)
{
int c = root->right->right == NULL ? :root->right->right->high;
int d = root->right->left == NULL ? :root->right->left->high;
if(c - d == )
{
L(root);
}
else if(c - d == -)
{
R(root->right);
L(root);
}
}
}
} int main()
{
int n,tem;
scanf("%d",&n);
node* Tree = NULL;
for(int i = ;i < n;++i)
{
scanf("%d",&tem);
insert(Tree,tem);
}
printf("%d\n",Tree->val);
return ;
}
1066. Root of AVL Tree (25)的更多相关文章
- 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 ...
- 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)
An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child sub ...
- 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 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 ...
- 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 sub ...
- PAT (Advanced Level) 1066. Root of AVL Tree (25)
AVL树的旋转.居然1A了.... 了解旋转方式之后,数据较小可以当做模拟写. #include<cstdio> #include<cstring> #include<c ...
- PAT甲级题解-1066. Root of AVL Tree (25)-AVL树模板题
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6803291.html特别不喜欢那些随便转载别人的原创文章又不给 ...
随机推荐
- setcookie 之 我见
$default_currency=get_default_currency(); $_COOKIE['currency'] = $default_currency['currency']; $_CO ...
- NSURLConnection & NSRULSession
NSURLConnection & NSRULSession NSURLSession是NSURLConnection 的替代者,在2013年苹果全球开发者大会(WWDC2013)随ios7一 ...
- Oracle基础(九) Oracle的体系结构
一.Oracle体系结构概述: Oracle的体系结构是指数据库的组成.工作过程与原理,以及数据在数据库中的组织与管理机制.要了解Oracle数据库的体系结构,必须理解Oracle系统的重要概念和主要 ...
- 怒刷DP之 HDU 1160
FatMouse's Speed Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Su ...
- 怒刷DP之 HDU 1260
Tickets Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Stat ...
- tmux使用
1.配置文件的使用 在~/.tmux.conf中添加: setw -g mouse-resize-pane on setw -g mouse-select-pane on setw -g mouse- ...
- 使用ambari搭建Hadoop平台
1.操作系统 CentoOS Server with GUI(有GUI,有浏览器*ambari基于浏览器*推荐latest stable version)2.分区 默认 + /hadoop3.网络设置 ...
- CSS中如何将li横向排列
直接贴段例子代码吧: @{ Layout = null;} <!DOCTYPE html><style type="text/css"> .test li ...
- php读取mysql中文数据出现乱码
1.PHP页面语言本身的编码类型不合适,这时候,你直接在脚本中写的中文肯定是乱码,不用说数据库了: 解决方法:选择'UTF8'或者'gb2312',这样客户浏览器会自动选择并出现正确的中文显示. ...
- Jquery 计算表格某一列的合计
<script type="text/javascript"> function heji(table_id, lie, val_id) { //合计 --xpp // ...