平衡二叉树的数组表示算法

博主是学生,水平有限,多多包涵。

之前学了平衡二叉树,想用数组表示平衡二叉树,但发现算法和用指针有很大不同,所以拿出来分享

#include <stdio.h>
#include <tchar.h>
#include<stdlib.h>
#include<string.h>

#define max 100
#define NULL 0
#define LH +1
#define EH 0
#define RH -1
#define TRUE 1
#define FALSE 0

typedef struct{
	int parent,lchild,rchild,bf;
	char data;
}shu[max];// 定义平衡二叉树的数组表示
shu avl;
char zifu[max];//将要构建成平衡二叉树的字符
int taller,bl,n,haizi;
char bianli[3][max];
void R_Rotate(int p){// 右旋处理
	int temp;
	temp=avl[p].lchild;
	avl[temp].parent=avl[p].parent;
	if(avl[p].parent==0)
		avl[0].data=temp;
	else if(avl[avl[p].parent].lchild==p)
		avl[avl[p].parent].lchild=temp;
	else avl[avl[p].parent].rchild=temp;
	avl[p].parent=temp;
	avl[p].lchild=avl[temp].rchild;
	avl[avl[temp].rchild].parent=p;
	avl[temp].rchild=p;
}
void L_Rotate(int p){// 左旋处理
	int temp;
	temp=avl[p].rchild;
	avl[temp].parent=avl[p].parent;
	if(avl[p].parent==0)
		avl[0].data=temp;
	else if(avl[avl[p].parent].lchild==p)
		avl[avl[p].parent].lchild=temp;
	else avl[avl[p].parent].rchild=temp;
	avl[p].parent=temp;
	avl[p].rchild=avl[temp].lchild;
	avl[avl[temp].lchild].parent=p;
	avl[temp].lchild=p;
}
void LeftBalance(int t){//左平衡处理
	int lc,rd;
	lc=avl[t].lchild;
	rd=avl[lc].rchild;
    switch(avl[lc].bf){
	case LH:{
    avl[t].bf=avl[lc].bf=EH;
	R_Rotate(t);break;}
    case RH:{
    switch(avl[rd].bf){
    	case LH:avl[t].bf=RH;avl[lc].bf=EH;break;
    	case EH:avl[t].bf=avl[lc].bf=EH;break;
    	case RH:avl[t].b
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值