pat04-树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<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<queue>
#include<vector>
using namespace std;
int num;
struct node{
int v,h;
node *l,*r;
node(){
l=r=NULL;
h=;
}
};
int max(int a,int b){
if(a>b){
return a;
}
else{
return b;
}
}
int GetHigh(node *h){
if(!h){
return ;
}
return h->h;
}
void LeftRotation(node *&h){
node *p=h->l;
h->l=p->r;
p->r=h;
h=p;
h->r->h=max(GetHigh(h->r->l),GetHigh(h->r->r))+;
h->h=max(GetHigh(h->l),GetHigh(h->r))+;
}
void RightRotation(node *&h){
node *p=h->r;
h->r=p->l;
p->l=h;
h=p;
h->l->h=max(GetHigh(h->l->l),GetHigh(h->l->r))+;
h->h=max(GetHigh(h->l),GetHigh(h->r))+;
}
void RightLeftRotation(node *&h){
LeftRotation(h->r);
//h->h=max(GetHigh(h->l),GetHigh(h->r))+1;
RightRotation(h);
}
void LeftRightRotation(node *&h){
RightRotation(h->l);
//h->h=max(GetHigh(h->l),GetHigh(h->r))+1;
LeftRotation(h);
}
void AVLInsert(int v,node *&h){
if(!h){//已经到了最底层
h=new node();
h->v=v;
return;
}
//bool can=false;
if(v<h->v){
AVLInsert(v,h->l);
if(GetHigh(h->l)-GetHigh(h->r)==){ //can=true; if(v<h->l->v){//左单旋
LeftRotation(h);
}
else{//左右双旋
LeftRightRotation(h);
}
}
}
else{
AVLInsert(v,h->r);
if(GetHigh(h->l)-GetHigh(h->r)==-){ //can=true; if(v>h->r->v){//左单旋
RightRotation(h);
}
else{//左右双旋
RightLeftRotation(h);
}
}
}
//if(!can)
h->h=max(GetHigh(h->l),GetHigh(h->r))+; //更新树高为1或2的树的树高
}
/*void prefind(node *h){
if(h){
//cout<<11<<endl;
cout<<h->v<<endl;
prefind(h->l);
prefind(h->r);
}
}*/
int main(){
//freopen("D:\\INPUT.txt","r",stdin);
int n;
while(scanf("%d",&n)!=EOF){
node *h=NULL;
int i;
for(i=;i<n;i++){
scanf("%d",&num);
AVLInsert(num,h);
} //prefind(h); //检测 printf("%d\n",h->v);
}
return ;
}
模板:
typedef struct AVLTreeNode *AVLTree;
typedef struct AVLTreeNode{
ElementType Data;
4 AVLTree Left;
AVLTree Right;
int Height;
};
AVLTree AVL_Insertion ( ElementType X, AVLTree T )
{ /* 将 X 插入 AVL 树 T 中,并且返回调整后的 AVL 树 */
if ( !T ) { /* 若插入空树,则新建包含一个结点的树 */
T = (AVLTree)malloc(sizeof(struct AVLTreeNode));
T->Data = X;
T->Height = ;
T->Left = T->Right = NULL;
} /* if (插入空树) 结束 */
else if (X < T->Data) { /* 插入 T 的左子树 */
T->Left = AVL_Insertion(X, T->Left);
if (GetHeight(T->Left) - GetHeight(T->Right) == )
/* 需要左旋 */
if (X < T->Left->Data)
T = SingleLeftRotation(T); /* 左单旋 */
else
T = DoubleLeftRightRotation(T); /* 左-右双旋 */
} /* else if (插入左子树) 结束 */
else if (X > T->Data) { /* 插入 T 的右子树 */
T->Right = AVL_Insertion(X, T->Right);
if (GetHeight(T->Left) - GetHeight(T->Right) == - )
/* 需要右旋 */
if (X > T->Right->Data)
30 T = SingleRightRotation(T); /* 右单旋 */
else
T = DoubleRightLeftRotation(T); /* 右-左双旋 */
} /* else if (插入右子树) 结束 */
/* else X == T->Data,无须插入 */
T->Height = Max(GetHeight(T->Left),GetHeight(T->Right))+;
/*更新树高*/
return T;
}
AVLTree SingleLeftRotation ( AVLTree A )
{ /* 注意: A 必须有一个左子结点 B */
/* 将 A 与 B 做如图 4.35 所示的左单旋,更新 A 与 B 的高度,返回新的根结点 B */
AVLTree B = A->Left;
A->Left = B->Right;
B->Right = A;
A->Height = Max(GetHeight(A->Left), GetHeight(A->Right))+;
B->Height = Max(GetHeight(B->Left), A->Height)+;
return B;
}
AVLTree DoubleLeftRightRotation ( AVLTree A )
{ /* 注意: A 必须有一个左子结点 B,且 B 必须有一个右子结点 C */
/* 将 A、 B 与 C 做如图 4.38 所示的两次单旋,返回新的根结点 C */
A->Left = SingleRightRotation(A->Left); /*将 B 与 C 做右单旋, C 被返回*/
return SingleLeftRotation(A); /*将 A 与 C 做左单旋, C 被返回*/
}
pat04-树4. Root of AVL Tree (25)的更多相关文章
- 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 ...
- pat 甲级 1066. Root of AVL Tree (25)
1066. Root of AVL Tree (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue An A ...
- PTA 04-树5 Root of AVL Tree (25分)
题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/668 5-6 Root of AVL Tree (25分) An AVL tree ...
- 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 ...
- 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 分) An AVL tree is a self-balancing binary search tree. In an AVL tree, t ...
- 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 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 ...
- 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 ...
随机推荐
- MVC区域路由配置
- html5 video使用autoplay属性时,声音混乱
html5 video使用autoplay属性时,声音混乱 页面代码 Index.html <html xmlns="http://www.w3.org/1999/xhtml" ...
- vs2015+opencv3.3.1+ c++实现 静态背景下多运动目标提取,检测
静止背景下运动物体的提取,跟踪出运动轨迹 下载地址 https://download.csdn.net/download/li_haoren/10761361 1.两遍扫描法得到第n帧的连通域,分离出 ...
- 微信小程序设计稿pt怎么转rpx
什么是逻辑分辨率? 什么是物理分辨率? 什么是DPI?(Dots Per Inch) 最早的时候,这个单位是用来描述打印机的性能的,意思是每英寸能打多少个墨点,毫无疑问,DPI越高,打印出来的东西就会 ...
- Django的文件上传以及预览、存储
思路: 文件上传通过前端的input标签,input设置display:none属性. 内容显示需要让前端通过<img>标签读取图片内容,可以通过<label>标签连接< ...
- 解决Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker fro问题
项目中碰到一个问题,就是将一个map转换成json格式的时候出现错误,最后排查将延迟加载关闭后成功转换,因为数据量较大,于是重新创建了一个对象进行接收. 解决办法是在配置文件中进行配置 虽然解决了这个 ...
- react.js学习之路五
最近没时间写博客,但是我一直在学习react,我发现react是一个巨大的坑,而且永远填不完的坑 关于字符串的拼接: 在react中,字符串的拼接不允许出现双引号“” ,只能使用单引号' ',例如这样 ...
- centos7用docker安装mysql5.7.24后配置主从
1)使用docker安装完成mysql5.7.24,我规划的是3台: 192.168.0.170(Master) 192.168.0.169(Slave) 192.168.0.168(Slave) 2 ...
- 【离散数学】 SDUT OJ 1.3按位AND和按位OR
1.3按位AND和按位OR Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Problem Description 已知长度为 ...
- Matlab2015 双目相机自动标定
标定步骤 调出标定工具箱 在命令行输入stereoCameraCalibrator,出现如下界面: 勾选相应的选项 然后将上面的“Skew”.“Tangential Distortion”以及“3 C ...