博主是学生,水平有限,多多包涵。
之前学了平衡二叉树,想用数组表示平衡二叉树,但发现算法和用指针有很大不同,所以拿出来分享
#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