当前位置:网站首页>【无标题】
【无标题】
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);
}
}
}
}
边栏推荐
猜你喜欢
随机推荐
为你的“架构”安排定期体检吧!
漏刻有时文档系统之XE培训系统二次开发配置手册
app直播源码,点击搜索栏自动弹出下拉框
SQL的 ISNULL 函数
In the background of the GBase 8c database, what command is used to perform the master-slave switchover operation for the gtm and dn nodes?
57:第五章:开发admin管理服务:10:开发【从MongoDB的GridFS中,获取文件,接口】;(从GridFS中,获取文件的SOP)(不使用MongoDB的服务,可以排除其自动加载类)
datax - 艰难debug路
正则表达式
The graphic details Eureka's caching mechanism/level 3 cache
【kali-信息收集】(1.4)识别活跃的主机/查看打开的端口:Nmap(网络映射器工具)
SENSORO成长伙伴计划 x 怀柔黑马科技加速实验室丨以品牌力打造To B企业影响力
From ordinary advanced to excellent test/development programmer, all the way through
即时通讯开发移动端弱网络优化方法总结
How to query database configuration parameters in GBase 8c, such as datestyle.What function or syntax to use?
Win11如何开启剪贴板自动复制?Win11开启剪贴板自动复制的方法
洛谷 P2440 木材加工
从普通进阶成优秀的测试/开发程序员,一路过关斩将
【ES】ES2021 我学不动了,这次只学 3 个。
我的驾照考试笔记(3)
DAO开发教程【WEB3.0】









