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 (≤) which is the total number of keys to be inserted. Then Ndistinct 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

题意:

将输入调整为平衡二叉树(AVL),输出根结点元素

题解:

判断插入结点对现有结点的平衡因子的影响,进而进行LL,LR,RL,RR旋转
假设三个结点连接关系为A->B->C,C为新插入结点并使得A的平衡因子==2
若C在A的左孩子的左子树上,则对A与B进行LL旋转
若C在A的左孩子的右子树上,则对A,B,C进行LR旋转,可分解为首先对B与C进行RR旋转,再对A与C进行LL旋转
若C在A的右孩子的右子树上,则对A与B进行RR旋转
若C在A的右孩子的左子树上,则对A,B,C进行RL旋转,可分解为首先对B与C进行LL旋转,再对A与C进行RR旋转

平衡二叉树选择详解:

4种平衡调整如下(结点的数字仅作标记作用):

(图中数字仅用于区分节点的不同,不用来表示节点的数值大小)

①LL:

对于根节点:左边比右边多

对于左节点:左边比右边多

右单旋转

  

②RR:

对于根节点:边比左边多

对于节点:边比边多

左单旋转

  

③LR平衡旋转:

对于根节点:左边比右边多

对于节点:右边比左边多

先左后右(先处理节点,再处理节点)

  

④RL平衡旋转:

对于根节点:右边比左边多

对于右节点:左边比右边多

先右后左(先处理右节点,再处理节点)

  

AC代码:

#include<iostream>
#include<algorithm>
using namespace std;
int n;
struct node{
int data;
node *lchild,*rchild;
};
node *Newnode(int x){//新建一个结点
node* newnode=new node;
newnode->data=x;
newnode->lchild=newnode->rchild=NULL;
return newnode;
}
int Height(node* root){//返回高度
if(root==NULL) return ;
else return max(Height(root->lchild),Height(root->rchild))+;
}
int getbalance(node* root){//检查是否平衡
return Height(root->lchild)-Height(root->rchild);
}
void R(node*&root){//右旋
//左节点成为根节点
node* temp=root->lchild;
root->lchild=root->rchild;//根的左边换成了左节点的右节点
temp->rchild=root;//根自己成为了原来左节点的右节点
root=temp;
}
void L(node*&root){//左旋
//右节点成为根节点
node *temp=root->rchild;
root->rchild=temp->lchild;//根的右边换成了右节点的左节点
temp->lchild=root;//根自己成为了原来右节点的左节点
root=temp;
}
void insert(node*&root,int x){
if(root==NULL){
root=Newnode(x);
return;
}
if(x<root->data){
insert(root->lchild,x);
if(getbalance(root)==){//左边必比右边高2
if(getbalance(root->lchild)==){//左节点的左边比右边高1
R(root);//右单旋
}else if(getbalance(root->lchild)==-){//左节点的右边比左边高1
L(root->lchild);//对于左节点左旋
R(root);//再跟节点右旋
}
}
}else{
insert(root->rchild,x);
if(getbalance(root)==-){//右边必比左边高2
if(getbalance(root->rchild)==){//右节点的左边比右边高1
R(root->rchild);//对于右节点右旋
L(root);//再跟节点左旋
}else if(getbalance(root->rchild)==-){//右节点的右边比左边高1
L(root);//左单旋
}
}
}
}
int main(){
scanf("%d",&n);
node *root = NULL;
for(int i=;i<n;i++){
int x;
scanf("%d",&x);
insert(root,x);
}
printf("%d",root->data);//输出处理好的平衡二叉树的根节点
return ;
}

PAT 甲级 1066 Root of AVL Tree (25 分)(快速掌握平衡二叉树的旋转,内含代码和注解)***的更多相关文章

  1. pat 甲级 1066. Root of AVL Tree (25)

    1066. Root of AVL Tree (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue An A ...

  2. 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 ...

  3. PAT甲级1066. Root of AVL Tree

    PAT甲级1066. Root of AVL Tree 题意: 构造AVL树,返回root点val. 思路: 了解AVL树的基本性质. AVL树 ac代码: C++ // pat1066.cpp : ...

  4. PAT 甲级 1066 Root of AVL Tree

    https://pintia.cn/problem-sets/994805342720868352/problems/994805404939173888 An AVL tree is a self- ...

  5. 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 ...

  6. 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 ...

  7. 【PAT甲级】1066 Root of AVL Tree (25 分)(AVL树建树模板)

    题意: 输入一个正整数N(<=20),接着输入N个结点的值,依次插入一颗AVL树,输出最终根结点的值. AAAAAccepted code: #define HAVE_STRUCT_TIMESP ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. 开启idea自动Build功能

    修改Intellij IDEA的配置两步:1.setting -> Compile -> Build project automatically --> 选中 2.CTRL + SH ...

  2. 常见的C语言编程规范

    头文件: 1.头文件中适合放置接口的声明,不适合放置实现. 2.头文件应向稳定的方向包含,产品依赖于平台,平台依赖于标准库. 3. .c/.h文件禁止包含用不到的头文件. 4.每一个.c文件应有一个同 ...

  3. bug的编写技巧与级别划分

    一.bug编写技巧 确.清晰.简洁.完整.一致 二.bug包含的要素 缺陷ID.缺陷标题.测试环境.缺陷发现日期时间.缺陷提交人 缺陷优先级.缺陷严重等级.发现缺陷软件版本.测试类型 缺陷复现步骤.期 ...

  4. CEOI2019 / CodeForces 1192B. Dynamic Diameter

    题目简述:给定一棵$N \leq 10^5$个节点的树,边上带权,维护以下两个操作: 1. 修改一条边的边权: 2. 询问当前树的直径长度. 解1:code 注意到树的直径有以下性质: 定理:令$\t ...

  5. ACwing : 798. 差分矩阵

    不得不说之前的差分我真的是掌握的不好.. 一维差分确实简单一看就会,但是学会了之后却并不能灵活的运用. 而二维的差分我甚至还琢磨了很长时间 懒得画图所以没有图..对于二维差分的定义,百度百科是这么说的 ...

  6. YouTube排名第一的励志英文演讲《Dream(梦想)》

    I don’t know what that dream is that you have, I don't care how disappointing it might have been as ...

  7. linux10.日志服务器建立和克隆机的网卡问题

    日志服务器建立       克隆虚拟机网卡混乱问题处理           vim /etc/udev/rules.d/70-perisistent-net.rules 调整mac地址与设备的对应关系 ...

  8. Unity3D 2D模拟经营游戏 洗车沙龙 完整源码

    Car Wash Salon Game 描述洗车模板与几个迷你游戏相关的汽车清洁,洗涤和装饰. 简单但有趣的游戏和伟大的视觉效果. 此模板不包含在应用中! 自定义应用程序的示例,有些功能在本项目中不受 ...

  9. 微信小程序左滑删除未操作有复位效果

    1.wxml <!--pages/test/test.wxml--> <view class="page"> <movable-area class= ...

  10. SKU是什么意思?

    在做电商项目时候必定会遇到这个词,那是什么意思呢?其实简单来讲就是一个单位. SKU全称为Stock Keeping Unit(库存量单位),即库存进出计量的基本单元,可以是以件,盒,托盘等为单位.S ...