当前位置:网站首页>【无标题】
【无标题】
2022-08-01 19:49:00 【疯疯癫癫才自由】
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 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 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树的插入维护:
#include <iostream>
#include <cstdio>
using namespace std;
typedef struct TNode* BinTree;
struct TNode
{
int data;
BinTree lchild,rchild;
int height;
};
BinTree NewNode(int val);
int getHeight(BinTree root);
void updateHeight(BinTree& root);
int getBalanceFactor(BinTree root);
void L(BinTree& root);
void R(BinTree& root);
void Insert(BinTree &BST,int val);
int main()
{
int n;
scanf("%d",&n);
BinTree BST=NULL;
for(int i=0;i<n;++i)
{
int val;
scanf("%d",&val);
Insert(BST,val);
}
printf("%d\n",BST->data);
return 0;
}
BinTree NewNode(int val)
{
BinTree node=new TNode;
node->data=val;
node->height=1;
node->lchild=node->rchild=NULL;
return node;
}
int getHeight(BinTree root)
{
if(root==NULL)
return 0;
else
return root->height;
}
void updateHeight(BinTree& root)
{
root->height=max(getHeight(root->lchild),getHeight(root->rchild))+1;
}
int getBalanceFactor(BinTree root)
{
return getHeight(root->lchild) - getHeight(root->rchild);
}
void L(BinTree& root)
{
BinTree temp=root->rchild;
root->rchild=temp->lchild;
temp->lchild=root;
updateHeight(root);
updateHeight(temp);
root=temp;
}
void R(BinTree& root)
{
BinTree temp=root->lchild;
root->lchild=temp->rchild;
temp->rchild=root;
updateHeight(root);
updateHeight(temp);
root=temp;
}
void Insert(BinTree &BST,int val)
{
if(!BST)
{
BST=NewNode(val);
return;
}
if(val<BST->data)
{
Insert(BST->lchild,val);
updateHeight(BST);
if(getBalanceFactor(BST)==2)
{
if(getBalanceFactor(BST->lchild)==1)
R(BST);
else if(getBalanceFactor(BST->lchild)==-1)
{
L(BST->lchild);
R(BST);
}
}
}
else
{
Insert(BST->rchild,val);
updateHeight(BST);
if(getBalanceFactor(BST)==-2)
{
if(getBalanceFactor(BST->rchild)==-1)
L(BST);
else if(getBalanceFactor(BST->rchild))
{
R(BST->rchild);
L(BST);
}
}
}
}
边栏推荐
- 常用命令备查
- 大神经验:软件测试的自我发展规划
- Pytorch模型训练实用教程学习笔记:三、损失函数汇总
- Compose实战-实现一个带下拉加载更多功能的LazyColumn
- cf:D. Magical Array【数学直觉 + 前缀和的和】
- 10 个 PHP 代码安全漏洞扫描程序
- From ordinary advanced to excellent test/development programmer, all the way through
- How PROE/Croe edits a completed sketch and brings it back to sketching state
- DAO开发教程【WEB3.0】
- 57:第五章:开发admin管理服务:10:开发【从MongoDB的GridFS中,获取文件,接口】;(从GridFS中,获取文件的SOP)(不使用MongoDB的服务,可以排除其自动加载类)
猜你喜欢
第58章 结构、纪录与类
57:第五章:开发admin管理服务:10:开发【从MongoDB的GridFS中,获取文件,接口】;(从GridFS中,获取文件的SOP)(不使用MongoDB的服务,可以排除其自动加载类)
Try compiling QT test on Allwinner V853 development board
终于有人把AB实验讲明白了
Source code analysis of GZIPOutputStream class
58:第五章:开发admin管理服务:11:开发【管理员人脸登录,接口】;(未实测)(使用了阿里AI人脸识别)(演示了,使用RestTemplate实现接口调用接口;)
Win11怎么安装语音包?Win11语音包安装教程
57: Chapter 5: Develop admin management services: 10: Develop [get files from MongoDB's GridFS, interface]; (from GridFS, get the SOP of files) (Do not use MongoDB's service, you can exclude its autom
BN BatchNorm + BatchNorm的替代新方法KNConvNets
分享一个适用于MCU项目的代码框架
随机推荐
有序双向链表的实现。
如何记录分析你的炼丹流程—可视化神器Wandb使用笔记【1】
mysql自增ID跳跃增长解决方案
CMake教程——Leeds_Garden
When installing the GBase 8c database, the error message "Resource: gbase8c already in use" is displayed. How to deal with this?
openresty 动态黑白名单
专利检索常用的网站有哪些?
MySQL你到底都加了什么锁?
数据库系统原理与应用教程(071)—— MySQL 练习题:操作题 110-120(十五):综合练习
ARTS_202207W2
Win11如何开启剪贴板自动复制?Win11开启剪贴板自动复制的方法
nacos安装与配置
An implementation of an ordered doubly linked list.
cf:D. Magical Array【数学直觉 + 前缀和的和】
Try compiling QT test on Allwinner V853 development board
密码学的基础:X.690和对应的BER CER DER编码
Compose实战-实现一个带下拉加载更多功能的LazyColumn
10 个 PHP 代码安全漏洞扫描程序
Pytorch模型训练实用教程学习笔记:一、数据加载和transforms方法总结
Compse编排微服务实战