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特别不喜欢那些随便转载别人的原创文章又不给 ...
随机推荐
- iOS - UI - UITableView
1.UITableView 表格视图 服从数据源 - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSIn ...
- QQ互联登陆出现(1054) Unknown column “conuintoken” in “field list”
在dz论坛中打开了QQ互联登录功能之后提示(1054) Unknown column conuintoken in field list 错误 我们看到sql 错误就是说没有conuintoken字了 ...
- MyEclipse设置编码方式 转载【http://www.cnblogs.com/susuyu/archive/2012/06/27/2566062.html】
1.windows->Preferences……打开"首选项"对话框,左侧导航树,导航到general->Workspace, 右侧Text file encoding ...
- hdu-5698 瞬间移动(数论+快速幂)
题目链接: 瞬间移动 Problem Description 有一个无限大的矩形,初始时你在左上角(即第一行第一列),每次你都可以选择一个右下方格子,并瞬移过去(如从下图中的红色格子能直接瞬移到蓝 ...
- 碎片化时代:O2O服务成最大趋势
从86年第一封电子E-Mail发出至今28个年头,中国互联网从单一点对点,点对多,多对多,如今又回归到点对点,从最基础的Mail.黄页应用到基于互动的Search.Blog以及基于满足人类基本需求的沟 ...
- z-index无效
1.要想给元素设置z-index样式,必须先让它变成定位元素,说的明白一点,就是要给元素设置一个postion:relative(定位元素:position属性值设置除默认值static以外的元素,包 ...
- 学习Slim Framework for PHP v3 ( 二)
昨天说到能够成功将本地的URL通过在index.php 中添加get(pattern,clouser)路由到指定的处理类中,处理后(这里指存入数据库中),然后返回response在浏览器中显示. 昨天 ...
- PLSQL执行sql语句输出的中文是???之解决方法和步骤
方法/步骤 1 登陆plsql,执行sql语句,输出的中文标题显示成问号????:条件包含中文,则无数据输出 步骤阅读 2 输入sql语句select * from V$NLS_PARAMETERS查 ...
- Linux下向SVN服务器添加新文件步骤
1.将文件checkout到本地目录 svn checkout path(path是服务器上的目录) 例如:svn checkout svn://192.168.1.1/pro/domain ...
- Javascript之拖拽库
在手机上运行触屏拖动时,我发现页面并没有反应,服务器端执行javascript在手机端与电脑端不能“相同式”实现(电脑端运行正常,而手机端不一样),这是为甚么呢? 首先,我们都知道javascript ...