博主是学生,水平有限,多多包涵。
之前学了平衡二叉树,想用数组表示平衡二叉树,但发现算法和用指针有很大不同,所以拿出来分享
#include
#include
#include
#include
#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].bf=EH;avl[lc].bf=LH;break;
}
avl[rd].bf=EH;
L_Rotate(lc);
R_Rotate(t);
break;}
}
}
void RightBalance(int t){//右平衡处理
int lc,rd;
rd=avl[t].rchild;
lc=avl[rd].lchild;
switch(avl[rd].bf){
case LH:{
switch(avl[lc].bf){
case LH:avl[t].bf=EH;avl[rd].bf=RH;break;
case EH:avl[t].bf=avl[rd].bf=EH;break;
case RH:avl[t].bf=LH;avl[rd].bf=EH;break;
}
avl[lc].bf=EH;
R_Rotate(rd);
L_Rotate(t);
break;}
case RH:{
avl[t].bf=avl[rd].bf=EH;
L_Rotate(t);break;}
}
}
int EQ(char e,int T){//树中是否有相同元素
int B=0;
if(e==avl[T].data){
B=1;
n=T;
}
else {
if(avl[T].lchild&&B!=1)B=EQ(e,avl[T].lchild);
if(avl[T].rchild&&B!=1)B=EQ(e,avl[T].rchild);
}
return B;
}
int LT(char e,int T){//比较大小
if(e
int InsertAVL(int T,char e){//插入元素
if(!avl[T].data){
avl[T].data=e;
avl[T].lchild=avl[T].rchild=NULL;
avl[T].bf=EH;
taller=TRUE;
}
else{
if(EQ(e,T)){
taller=FALSE;
return 0;
}
if(LT(e,T)){
if(avl[T].lchild==0){
avl[T].lchild=haizi;
avl[haizi].parent=T;
haizi++;
}
if(!InsertAVL(avl[T].lchild,e))
return 0;
if(taller)
switch(avl[T].bf){
case LH:{
LeftBalance(T);
taller=FALSE;
break;}
case EH:{
avl[T].bf=LH;
taller=TRUE;
break;}
case RH:{
avl[T].bf=EH;
taller=FALSE;
break;}
}
}
else {
if(avl[T].rchild==0){
avl[T].rchild=haizi;
avl[haizi].parent=T;
haizi++;
}
if(!InsertAVL(avl[T].rchild,e))
return 0;
if(taller)
switch(avl[T].bf){
case LH:{
avl[T].bf=EH;
taller=FALSE;
break;}
case EH:{
avl[T].bf=RH;
taller=TRUE;
break;}
case RH:{
RightBalance(T);
taller=FALSE;
break;}
}
}
}
return 1;
}
int DeleteAVL(int T,char e){//删除元素
int p,child,high=0;
if(!EQ(e,T)){
printf("没有此元素\n");
return 0;
}
else{
if(avl[n].lchild==0&&avl[n].rchild==0){
p=avl[n].parent;
if(avl[avl[n].parent].lchild==n){
avl[avl[n].parent].lchild=0;child=1;
}
else{
avl[avl[n].parent].rchild=0;
child=-1;
}
switch(avl[avl[n].parent].bf){
case EH:
if(child==1)
avl[avl[n].parent].bf=RH;
else avl[avl[n].parent].bf=LH;
break;
case LH:
if(child==1)
avl[avl[n].parent].bf=EH;
else LeftBalance(avl[n].parent);
high=1;
break;
case RH:
if(child==1
)RightBalance(avl[n].parent);
else avl[avl[n].parent].bf=EH;
high=1;
break;
}
}else if(avl[n].lchild!=0&&avl[n].rchild!=0){
if(avl[avl[n].lchild].rchild==0){
p=avl[n].lchild;
avl[avl[n].lchild].rchild=avl[n].rchild;
avl[avl[n].lchild].parent=avl[n].parent;
if(avl[avl[n].parent].rchild==n)
avl[avl[n].parent].rchild=avl[n].lchild;
else avl[avl[n].parent].lchild=avl[n].lchild;
switch(avl[n].bf){
case EH:avl[p].bf=RH;break;
case RH:RightBalance(p);high=1;break;
case LH:avl[p].bf=EH;high=1;break;
}
}else{
p=avl[n].lchild;
for(;avl[p].rchild!=0;p=avl[p].rchild);
avl[n].data=avl[p].data;
if(avl[p].rchild!=0){
avl[avl[p].parent].rchild=avl[p].lchild;
avl[avl[p].lchild].parent=avl[p].parent;
high=1;
p=avl[p].lchild;
avl[p].bf=EH;
}else{
if(avl[avl[p].parent].lchild==n){
avl[avl[n].parent].lchild=0;
child=1;
}
else{
avl[avl[p].parent].rchild=0;
child=-1;
}
p=avl[p].parent;
switch(avl[p].bf){
case EH:
if(child==1)
avl[p].bf=RH;
else avl[p].bf=LH;
break;
case LH:
if(child==1)
avl[p].bf=EH;
else LeftBalance(p);
high=1;
break;
case RH:
if(child==1)
RightBalance(p);
else avl[p].bf=EH;
high=1;
break;
}
}
}
}else if(avl[n].lchild==0){
p=n;
avl[n].data=avl[avl[n].rchild].data;
avl[n].rchild=0;
avl[n].bf=EH;
high=1;
}else{
p=n;
avl[n].data=avl[avl[n].lchild].data;
avl[n].lchild=0;
avl[n].bf=EH;
high=1;
}
for(;avl[p].parent!=0;p=avl[p].parent){
}
}
}
void xianxu(int T){//先序遍历
bianli[0][bl++]=avl[T].data;
if(avl[T].lchild!=0)xianxu(avl[T].lchild);
if(avl[T].rchild!=0)xianxu(avl[T].rchild);
}
void zhongxu(int T){//中序遍历
if(avl[T].lchild!=0)zhongxu(avl[T].lchild);
bianli[1][bl++]=avl[T].data;
if(avl[T].rchild!=0)zhongxu(avl[T].rchild);
}
void xianshi(){
FILE *fp2;
if(!(fp2=fopen("bianli.txt","w")))
printf("文件打开有误");
bl=0;
xianxu(avl[0].data);
bianli[0][bl]='\0';
bl=0;
zhongxu(avl[0].data);
bianli[1][bl]='\0';
fprintf(fp2,"先序:%s\n",bianli[0]);
fprintf(fp2,"中序:%s\n",bianli[1]);
fclose(fp2);
}
void charu(){//从"zifu.txt”中读取数据并插入到平衡二叉树
FILE *fp1;
int i,temp;
if(!(fp1=fopen("zifu.txt","r")))
printf("文件打开有误\n");
fscanf(fp1,"%s",zifu);
fclose(fp1);
haizi=2;
avl[0].data=1;
for(i=0;i
void shanchu(){//删除结点
char temp;
printf("请输入删除结点:");
fflush(stdin);
scanf("%c",&temp);
DeleteAVL(avl[0].data,temp);
xianshi();
}
void erchashu(){//二叉树的操作
int choose;
system("CLS");
printf("请选择:1、生成平衡二叉树并输出 2、删除节点 ");
fflush(stdin);
scanf("%d",&choose);
switch(choose){
case 1:charu();system("PAUSE");break;
case 2:shanchu();system("PAUSE");break;
default:printf("输入有误");
}
}