PAT_A1066#Root of AVL Tree
Source:
Description:
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 (≤) 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
Keys:
Code:
/*
Data: 2019-06-24 15:44:17
Problem: PAT_A1066#Root of AVL Tree
AC: 12:20 题目大意:
构造AVL树,打印根结点
*/
#include<cstdio>
#include<algorithm>
using namespace std;
struct node
{
int data,height;
node *lchild,*rchild;
}; int GetHeight(node *root)
{
if(root == NULL)
return ;
else
return root->height;
} void UpdataHeight(node *&root)
{
root->height = max(GetHeight(root->lchild),GetHeight(root->rchild))+;
} int GetBalanceFactor(node *root)
{
return GetHeight(root->lchild) - GetHeight(root->rchild);
} void LeftRotation(node *&root)
{
node *temp = root->rchild;
root->rchild = temp->lchild;
temp->lchild = root;
UpdataHeight(root);
UpdataHeight(temp);
root = temp;
} void RightRotation(node *&root)
{
node *temp = root->lchild;
root->lchild = temp->rchild;
temp->rchild = root;
UpdataHeight(root);
UpdataHeight(temp);
root = temp;
} void Insert(node *&root, int x)
{
if(root == NULL)
{
root = new node;
root->data = x;
root->height=;
root->lchild = root->rchild = NULL;
}
else if(x < root->data)
{
Insert(root->lchild, x);
UpdataHeight(root);
if(GetBalanceFactor(root) == )
{
if(GetBalanceFactor(root->lchild) == )
RightRotation(root);
else
{
LeftRotation(root->lchild);
RightRotation(root);
}
}
}
else
{
Insert(root->rchild, x);
UpdataHeight(root);
if(GetBalanceFactor(root) == -)
{
if(GetBalanceFactor(root->rchild) == -)
LeftRotation(root);
else
{
RightRotation(root->rchild);
LeftRotation(root);
}
}
}
} int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("Test.txt", "r", stdin);
#endif // ONLINE_JUDGE int n,x;
scanf("%d", &n);
node *root = NULL;
for(int i=; i<n; i++)
{
scanf("%d", &x);
Insert(root, x);
}
printf("%d", root->data); return ;
}
PAT_A1066#Root of AVL Tree的更多相关文章
- 04-树5 Root of AVL Tree + AVL树操作集
平衡二叉树-课程视频 An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the tw ...
- 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, ...
- 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
PAT甲级1066. Root of AVL Tree 题意: 构造AVL树,返回root点val. 思路: 了解AVL树的基本性质. AVL树 ac代码: C++ // pat1066.cpp : ...
- 04-树4. Root of AVL Tree (25)
04-树4. Root of AVL Tree (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue An A ...
- 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 ...
- pat1066. 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) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue An A ...
- Root of AVL Tree
04-树5 Root of AVL Tree(25 分) An AVL tree is a self-balancing binary search tree. In an AVL tree, the ...
随机推荐
- UltraEdit UE如何设置自动换行
1如何设置Ultraedit自动换行 学过编程方面电脑知识的朋友可能都清楚,ultraedit是一款易用强大的文本编辑工具.并且打开没有Unicode签名(BOM)的UTF-8格式半角英文文件 ...
- dhtmlx中添加一列(将相似button、下拉列表、输入框显示在一行上)
{ type: "label", list: [ { { type: "label", labelWidth: 55 }, { type: "newc ...
- 一个unity3d lightmap问题
上周美术同学在使用unity3d制作lightmap的过程中,发现部分被lightmap影响的模型在移动端上效果与pc端不一致.当时我大概看了下,分析后,得到一个结论是“在移动端上lightmap的h ...
- CocoaPods 的安装和使用介绍
CocoaPods 的安装和使用介绍 安装 安装方式异常简单 , Mac 下都自带 ruby,使用 ruby 的 gem 命令即可下载安装: 1 2 $ sudo gem install cocoap ...
- 使用VS2005安装和编译QT4.53源码
学习Qt,当然是QT4好.可是装了4.86以后,网上下载的书中的例子大多无法直接用VS执行(个人不喜欢用QT Creator),即打开pro转换的时候出错(我也懒的研究为什么出错了).看了一下发布时间 ...
- 【转】Caused by: java.lang.NoClassDefFoundError: android.support.v7.gridlayout.R$dimen 异常解决方法
在使用gridlayout中遇到 Caused by: java.lang.NoClassDefFoundError: android.support.v7.gridlayout.R$dimen 问题 ...
- MSP430:串口输出
初始化 void Uart_Init(void) { BCSCTL1 = CALBC1_1MHZ; // Set DCO DCOCTL = CALDCO_1MHZ; P1SEL = BIT1 + BI ...
- MAC地址 初识
MAC地址 即物理地址/硬件地址 地址长度为48位,6字节. 格式为:00-23-5A-15-99-42 一个网卡对应一个MAC地址(比如笔记本,有线网卡有一个MAC地址,无线网卡也有一个MAC地址) ...
- Gold Coins
http://poj.org/problem?id=2000 #include<stdio.h> ; int main() { int coin[N]; ,j,k; j = ; k = ; ...
- webpack的初步使用(01)
webpack:1.安装:在项目文件下先npm init初始化,一路回车2.进入到建立的项目下:cd projectname3.安装webpack:npm install webpack --save ...