树5 Root of AVL Tree

《数据结构》04-树5 Root of AVL Tree 题目 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... 阅读详情

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
#include<stdio.h>
#include<stdlib.h>
typedef int ElementType;
typedef struct AVLNode *Position;
typedef Position AVLTree;
struct AVLNode{
	ElementType Data;
	AVLTree Left;
	AVLTree Right;
	int Height;
};


int Max( int a, int b){
	return a>b? a : b;
}


int GetHeight(AVLTree A){  //树的高度,从0开始数
	
	if(A==NULL){
		return -1;
	}
	else{
		int BLHeight = GetHeight(A->Left);
		int BRHeight = GetHeight(A->Right);
		if(BLHeight >= BRHeight) return BLHeight+1;
		else {
			return BRHeight+1;}
	}
}


AVLTree SingleLeftRotation(AVLTree A){
	AVLTree B = A->Left;   //B是A的左子树
	A->Left = B->Right;	    //B的右子树比A小,比B大,挂到A左边
	B->Right = A;    //把A挂到B右边
	A->Height = Max(GetHeight(A->Left),GetHeight(A->Right)) + 1;
	B->Height = Max(GetHeight(B->Left),A->Height) + 1;


	return B;
}


AVLTree SingleRightRotation(AVLTree A){
	AVLTree B = A->Right;
	A->Right = B->Left;
	B->Left = A;
	A->Height = Max(GetHeight(A->Left),GetHeight(A->Right)) + 1;
	B->Height = Max(GetHeight(B->Left),A->Height) +1;


	return B;
}


AVLTree DoubleLeftRightRotation(AVLTree A){
	
	//将B和C做右单旋,返回C
	A->Left = SingleRightRotation(A->Left);
	//A和C左单旋,返回C
	return SingleLeftRotation(A);
}


AVLTree DoubleRightLeftRotation(AVLTree A){


	A->Right = SingleLeftRotation(A->Right);


	return SingleRightRotation(A);
}


AVLTree Insert(AVLTree T, ElementType X){
	if(!T){  //若空树,则新建一个树
		T=(AVLTree)malloc(sizeof(struct AVLNode));
		T->Data = X;
		T->Height = 0;
		T->Left = T->Right = NULL;
	}


	else if(X < T->Data){
		//插入到左子树
		T->Left = Insert(T->Left,X);
		//插入后进行调整,如果需要左旋
		if(GetHeight(T->Left) - GetHeight(T->Right) == 2)
			if(X < T->Left->Data) //如果LL
				T = SingleLeftRotation(T);
			else
				T = DoubleLeftRightRotation(T);
	}


	else if(X > T->Data){
		
		T->Right = Insert(T->Right,X);
		
		if(GetHeight(T->Left) - GetHeight(T->Right) == -2)
			if(X > T->Right->Data)
				T = SingleRightRotation(T);
			else
				T = DoubleRightLeftRotation(T);
	}
	T->Height = Max(GetHeight(T->Left), GetHeight(T->Right)) + 1;
	
	return T;		
	}




int main(){
	int N,temp;
	scanf("%d",&N);
	AVLTree T=NULL;
	for (int i = 0; i < N; ++i)
	{
		scanf("%d",&temp);
		T = Insert(T,temp);
	}
	printf("%d",T->Data );
}

惊叹于LR,RL实现的简洁优雅。

告别路径打架!用Python实现CBS算法搞定仓库机器人调度(附完整代码) 本文详细介绍了如何使用Python实现CBS(Conflict-Based Search)算法解决仓库机器人调度中的路径冲突问题。通过多智能体路径规划(MAPF)技术,有效避免AGV在智能仓库中的碰撞和死锁,提供完整代码实现和工业场景优化技巧,帮助开发者快速构建高效的机器人调度系统。 阅读详情

相关推荐

【树莓派4B】八、树莓派软路由,Ubuntu18.04配置路由转发

我的目的是通过把树莓派做成一个软路由,相当于一个智能路由器。 材料 树莓派4B,操作系统其实可以是其他的Linux版本。以为4B自带无线网卡,就用自带无线网卡作为路由出口。 大概思路 具体步骤 配置两个网卡的IP sudo vim /etc/network/interface 这里设置静态IP,也可以动态获取。 #Local loopback auto lo iface lo inet loopback #Ethernet0 auto eth0 iface eth0 inet static addr

林愿留的博客 10万+

10-排序5 PAT Judge

The ranklist of PAT is generated from the status list, which shows the scores of the submissions. This time you are supposed to generate the ranklist for PAT. Input Specification Each input file contains one test case. For each case, the first line contain

HONGRRY的博客 203

MIPI转LVDS芯片调试实战:从Kernel到UEFI的完整点亮流程

本文详细解析了MIPI转LVDS芯片调试的全流程,从Kernel驱动开发到UEFI早期初始化,涵盖I2C通信、电源时序控制等核心挑战。通过LT9211芯片实战案例,提供设备树配置、电源时序优化及显示异常排查等实用技巧,助力工程师高效完成屏幕调试工作。

weixin_30314813的博客 452

树5 Root of AVL Tree

题目: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...

Dezeming的博客 6825

ArcGIS 泛克里金插值

按照如图所示的步骤填写参数,1处选择subset features。 2:输入数据。 3:输出训练数据。 4:输出测试数据。 5:80,表示80%的比例是训练数据,20%是测试数据。 6:输入的百分比。 1:选择geostatistical wizard。 2:插值方法,克里金插值 3:训练数据。 4:数据字段。 点击下一步: 1:表示泛克里金插值。 2:输出表面类型。 3:转换类型none。 4:趋势剔除的阶数。 点击下一步: 表示东北—西南方向变化平缓。 1:模型优化 2:模型类型(stabl.

yuanYW7556的博客 7275

验证(uvm常见问题)--子类和父类的句柄和对象问题

sv中的子类和父类 在sv中,有如下两个类,child继承于parent。然后在test module中,正确转换的方法如下面所示。其中c1,c2,p1 都指向了同一个子类的对象c1。总结如下: 一个子类的对象是包括子类的成员变量,方法和父类的成员变量,方法。 子类的句柄c1可以直接赋值给父类的句柄p1,此时p1只可以访问父类的成员变量和方法,或者子类的虚方法,比如下面调用p1.display是...

q876507447的博客 7634

04-树5 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 heights of the two child subtrees of any node differ by at most one; if at any time they d

arctanx 1067

WorkBuddy 技能指南:基础办公与文档处理

WorkBuddy 不仅仅是一个对话工具——它通过技能(Skills)系统,将通用 AI 助手转变为具备专业领域能力的智能助手。每个技能都是一套经过精心设计的"能力包",包含专业知识、工作流程和专用工具。本文聚焦于文档处理类技能——这是日常工作中使用频率最高、最实用的能力集合。

u011186532的专栏 705

详解-04树5 Root of AVL Tree

题目来源: 中国大学MOOC-陈越、何钦铭-数据结构 题目详情: 译文: AVL树是一种自平衡二叉搜索树。 在AVL树中,任何节点的两个子树的高度最多相差1。 如果它们之间的高度差超过1,则将进行重新平衡以恢复此属性。 图1-4为旋转规则。 现在给出了一系列插入,返回得到的AVL树的根结点。 输入格式: 每个输入文件包含一个测试用例。 对于每种情况,第一行都包含一个正整数N(≤20),它是要插入的键的总数。 然后在下一行中给出N个不同的整数键。 一行中的所有数字都用空格分隔。 输出格式: 对于每个测试用例

Young824的博客 688

第一部分:VTK基础入门(第1章:VTK简介与概述)

VTK简介与核心特性概述 VTK(Visualization Toolkit)是一个开源的跨平台C++类库,专用于3D计算机图形学、图像处理和科学可视化。由Kitware公司开发,VTK自1993年起源以来,经历了开源化、成熟化和现代化多个发展阶段,现已成为科学可视化的核心工具之一。 VTK的核心特性包括: 管道架构:模块化设计实现数据流管理 丰富的数据支持:处理多边形数据、图像数据等 强大的渲染能力:集成OpenGL,支持多种渲染技术 最新版本VTK 9.4.2增强了现代C++支持,优化了内存管理,并提升

仰望星空的博客 2363

HyperLynx(十八)DDR(一)DDR简介和DDR的数据仿真

LVTTL电平为单端信号,而SSTL电平本质上是差分对,其实现机制是将信号与参考电平Vref组成差分对进行比较,如图所示,VIH和VIL各有一个交流参数AC和直流参数DC,信号第一次越过AC门限的时刻,作为时序计算的参考点,此后只要信号保持DC门限内,逻辑状态保持不变,这种设计减小了噪声、振铃等对于信号质量的影响。经过调整后,控制器在发送颗粒1的DQS后,延时1ns再发送颗粒2的DQS,则颗粒1在1ns后收到对齐的DQS和CLK,颗粒2在2ns后收到对齐的DQS和CLK,从而满足了时序关系。

小幽余生不加糖 7680

PTA-04-树5 Root of AVL Tree

url=非常详细AVL的旋转

Sukihasimoto的博客 281

Python爬虫实战:使用最新技术抓取百度贴吧帖子内容

本文详细介绍了使用Python最新技术抓取百度贴吧帖子的完整流程。通过库,我们实现了高效的异步爬虫,具备JavaScript渲染、智能反爬应对、数据持久化等高级功能。这个爬虫框架具有良好的扩展性,可以根据具体需求进行功能增强。

2201_76125261的博客 779

MOOC 04-树5 Root of AVL Tree

MOOC TPA作业 学习笔记

鸿雁丨红豆灬自学计算机学习记录 363

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

ssm框架搭建

这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载

04-树5 Root of AVL Tree(25 分)

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

中央单元 1186

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

上一篇: 树4 是否同一棵二叉搜索树
下一篇: 树7 堆中的路径
DedicateToAI
博客等级 码龄9年 6粉丝 90原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值