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

本文由一名学生分享,在理解了平衡二叉树的概念后,尝试将其转换为数组表示,过程中发现与指针表示法有显著差异,文章将深入探讨这一转换算法。
摘要由CSDN通过智能技术生成

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

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

#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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值