当前位置:网站首页>【无标题】
【无标题】
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);
}
}
}
}
边栏推荐
- What are the application advantages of SaaS management system?How to efficiently improve the digital and intelligent development level of food manufacturing industry?
- 第58章 结构、纪录与类
- [Server data recovery] Data recovery case of offline multiple disks in mdisk group of server Raid5 array
- 17. Load balancing
- 第56章 业务逻辑之物流/配送实体定义
- 使用常见问题解答软件的好处有哪些?
- mysql自增ID跳跃增长解决方案
- kubernetes - deploy nfs storage class
- 油猴hook小脚本
- 我的驾照考试笔记(2)
猜你喜欢
随机推荐
大神经验:软件测试的自我发展规划
短视频软件开发,Android开发,使用Kotlin实现WebView
正则表达式
PROE/Croe如何编辑已完成的草图,让其再次进入草绘状态
数据库系统原理与应用教程(070)—— MySQL 练习题:操作题 101-109(十四):查询条件练习
app直播源码,点击搜索栏自动弹出下拉框
突破边界,华为存储的破壁之旅
nacos安装与配置
MySQL你到底都加了什么锁?
为什么限制了Oracle的SGA和PGA,OS仍然会用到SWAP?
The solution to the vtk volume rendering code error (the code can run in vtk7, 8, 9), and the VTK dataset website
mysql解压版简洁式本地配置方式
openresty 动态黑白名单
XSS range intermediate bypass
【webrtc】sigslot : 继承has_slot 及相关流程和逻辑
When installing the GBase 8c database, the error message "Resource: gbase8c already in use" is displayed. How to deal with this?
Debug一个ECC的ODP数据源
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
安装win32gui失败,解决问题
数据可视化









