当前位置:网站首页>C语言实现平衡二叉树
C语言实现平衡二叉树
2022-06-22 18:28:00 【单单一个越字】
B站小甲鱼视频
https://www.bilibili.com/video/BV1jW411K7yg?p=79
博客园教程(按步骤讲解)
https://www.cnblogs.com/zhujunxxxxx/p/3348798.html
小甲鱼代码:
#define LH 1
#define EH 0
#define RH -1
typedef struct BiTNode
{
int data;
int bf;
struct BiTNode *lchild, *rchild;
} BiTNode, *BiTree;
void R_Rotate(BiTree *p)
{
BiTree L;
L = (*p)->lchild;
(*p)->lchild = L->rchild;
L->rchild = (*p);
*p = L;
}
void LeftBalance(BiTree *T)
{
BiTree L, Lr;
L = (*T)->lchild;
switch(L->bf)
{
case LH:
(*T)->bf = L->bf = EH;
R_Rotate(T);
break;
case RH:
Lr = L->rchild;
switch(Lr->bf)
{
case LH:
(*T)->bf = RH;
L->bf = EH;
break
case EH:
(*T)->bf = L->bf = EH;
break;
case RH:
(*T)->bf = EH;
L->bf = LH;
break;
}
Lr->bf = EH;
L_Rotate(&(*T)->lchild);
R_Rotate(T);
}
}
int InsertAVL(BiTree *T, int e, int *taller)
{
if( !*T )
{
*T = (BiTree)malloc(sizeof(BiTNode));
(*T)->data = e;
(*T)->lchild = (*T)->rchild = NULL;
(*T)->bf = EH;
*taller = TRUE;
}
else
{
if(e == (*T)->data)
{
*taller = FALSE;
return FALSE;
}
if(e < (*T)->data)
{
if(!InsertAVL(&(*T)->lchild, e, taller))
{
return FALSE;
}
if(*taller)
{
switch((*T)->bf)
{
case LH:
LeftBalance(T);
*taller = FALSE;
break;
case EH:
(*T)->bf = LH;
*taller = TRUE;
break;
case RH:
(*T)->bf = EH;
*taller = FALSE;
break;
}
}
}
else
{
if(!InsertAVL(&(*T)->rchild, e, taller))
{
return FALSE;
}
if(*taller)
{
switch((*T)->bf)
{
case LH:
(*T)->bf = EH;
*taller = FALSE;
break;
case EH:
(*T)->bf = RH;
*taller = TRUE;
break;
case RH:
RightBalance(T);
*taller = FALSE;
break;
}
}
}
}
}
边栏推荐
- 堆排序(原理加代码)
- 数组对象实现一 一对比(索引和id相同的保留原数据,原数组没有的数据从默认列表加进去)
- Input two strings and output the longest substring with the same length
- Openpnp debugging ------ 0816 Feida Tui 0402 taping
- AttributeError: ‘KeyedVectors‘ object has no attribute ‘wv‘
- NRF51822外设学习
- 51万奖池邀你参战!第二届阿里云ECS CloudBuild开发者大赛来袭
- Recommend an anatomy website
- DIV横向布局
- 0.1----- process of drawing PCB with AD
猜你喜欢

About Random Forest

AB打包有的Shader没有触发IPreprocessShaders的回调

A homekit enabled camera? Zhiting IPC camera IC1 unpacking experience

小波变换db4进行四层分解及其信号重构—matlab分析及C语言实现

Adapter mode of structural mode

what? Homekit, Micah, aqara and other ecosystems can also be linked with tmall elf ecology through zhiting?

0.0 - how can SolidWorks be uninstalled cleanly?

1.4-----PCB设计?(电路设计)确定方案

Fault analysis | from data_ Free exception

1.2----- mechanical design tools (CAD software) and hardware design tools (EDA software) and comparison
随机推荐
Velocity syntax
Some problem records of openpnp using process
第一章 力扣热题100道(1-5)
Ts as const
产品几何技术规范(GPS) 线性尺寸公差ISO代号体系
Solution de pin hors grille dans altium designer
Problems of different renderers running on the web with flutter2.0
Teachers, I want to ask you a question. I run flinkcdc locally to synchronize MySQL data. The timestamp field parsing is normal,
Input two strings and output the longest substring with the same length
08_一句话让你人间清醒
界面开发组件DevExpress ASP.NET Core v21.2 - UI组件增强
通过base64下载文件(将base64转为blob)
自定义控件AutoScaleMode为Font造成宽度增加的问题
NLP-D57-nlp比赛D26&刷题D13&读论文&&找了一个多小时bug
第一篇 热身--隐式类型转换还是其他?
技术管理进阶——你了解成长的全貌吗?
0816飞达的缺点(改进方向)
How to choose smart home? Take a look at this shopping guide
老师们,我想请教一个问题,我本地跑flinkcdc同步mysql数据timestamp字段解析正常,
生产系统SQL执行计划突然变差怎么办?