当前位置: 技术问答>linux和unix
平衡二叉树的问题
来源: 互联网 发布时间:2015-12-06
本文导语: 各位高手,这是我的平衡二叉树的代码,在linux下编译没有问题,但就是得不到结果,请各位帮忙看看怎么回事,谢谢!(不考虑重复数据) #include"stdio.h" #include"malloc.h" #define LH +1 //左高 #define EH 0 //等高 #define R...
各位高手,这是我的平衡二叉树的代码,在linux下编译没有问题,但就是得不到结果,请各位帮忙看看怎么回事,谢谢!(不考虑重复数据)
#include"stdio.h"
#include"malloc.h"
#define LH +1 //左高
#define EH 0 //等高
#define RH -1 //右高
#define EQ(a,b) ((a)=(b))
#define LT(a,b) ((a)rchild;
tn->rchild=lc->lchild;
lc->lchild=tn;
tn=lc;
// free(lc);
}
void R_Rotate(treebs tn)
{
treebs lc;
//BStree*rc=NULL;
//rc=tn->lchild;
//lc=tn;
//lc->lchild=rc->rchild;
//tn=rc;
//lc->data=tn->data;
//lc->bf=tn->bf;
//tn->bf=rc->bf;
//tn->data=rc->data;
//tn->lchild =rc->lchild;
//tn->rchild=lc;
lc=tn->lchild;
tn->lchild=lc->rchild;
lc->rchild=tn;
lc=tn;
// free(lc);
}
void LeftBalance(treebs tn)//二叉树左平衡旋转处理
{
treebs rd;
treebs lc=tn->lchild;
switch(lc->bf)
{
case LH:
tn->bf=lc->bf=EH;
R_Rotate(tn);
break;
case RH:
rd=lc->rchild;
switch(rd->bf)
{
case LH:
tn->bf=RH;lc->bf=EH;break;
case EH:
tn->bf=lc->bf=EH;break;
case RH:
tn->bf=EH;lc->bf=LH;break;
}
rd->bf=EH;
L_Rotate(tn->lchild);
R_Rotate(tn);
}
}
void RightBalance(treebs tn)//二叉树右平衡旋转处理
{
treebs rd;
treebs lc=tn->rchild;
switch(lc->bf)
{
case LH:
tn->bf=lc->bf=EH;
L_Rotate(tn);
break;
case RH:
rd=lc->rchild;
switch(rd->bf)
{
case LH:
tn->bf=EH;lc->bf=RH;break;
case EH:
tn->bf=lc->bf=EH;break;
case RH:
tn->bf=LH;lc->bf=EH;break;
}
rd->bf=EH;
R_Rotate(tn->lchild);
L_Rotate(tn);
}
}
int inserttn(treebs tn,int e,int taller) //插入结点
{
treebs p ;
//struct BStree *tn1;
//tn1=&tn;
if(!tn)
{
//if(!(tn=(BStree*)malloc(sizeof(BStree)))) return 0;
tn=(treebs)malloc(sizeof(BStree));
tn->data=e;
tn->lchild=tn->rchild=NULL;
tn->bf=EH;
taller=1;
}else
{
if(edata)
{
if(!inserttn(tn->lchild ,e,taller)) return 0;
if(taller!=0)
{
switch(tn->bf)
{
case LH:
LeftBalance(tn);taller=0;break;
case EH:
tn->bf=LH;taller=1;break;
case RH:
tn->bf=EH;taller=0;break;
}
}
}else
{
if(!inserttn(tn->rchild ,e,taller)) return 0;
if(taller)
{
switch(tn->bf)
{
case RH:
RightBalance(tn);taller=0;break;
case EH:
tn->bf=RH;taller=1;break;
case LH:
tn->bf=EH;taller=0;break;
}
}
}
}
return 1;
}
void showtree(treebs tn)
{
if(tn)
{
showtree(tn->lchild);
printf("%5d",tn->data);
showtree(tn->rchild);
}
}
int main()
{
treebs tn;
int taller;
int e;
/*tn = (treebs)malloc(sizeof(BStree));
tn->data=0;
tn->lchild=tn->rchild=NULL;
tn->bf=EH;*/
tn=NULL;
while(1)
{
printf("请输入数据:");
scanf("%d",&e);
if (e==0){ exit(0); }
inserttn(tn,e,taller);
showtree(tn);
printf("n");
}
showtree(tn);
return 1;
}
#include"stdio.h"
#include"malloc.h"
#define LH +1 //左高
#define EH 0 //等高
#define RH -1 //右高
#define EQ(a,b) ((a)=(b))
#define LT(a,b) ((a)rchild;
tn->rchild=lc->lchild;
lc->lchild=tn;
tn=lc;
// free(lc);
}
void R_Rotate(treebs tn)
{
treebs lc;
//BStree*rc=NULL;
//rc=tn->lchild;
//lc=tn;
//lc->lchild=rc->rchild;
//tn=rc;
//lc->data=tn->data;
//lc->bf=tn->bf;
//tn->bf=rc->bf;
//tn->data=rc->data;
//tn->lchild =rc->lchild;
//tn->rchild=lc;
lc=tn->lchild;
tn->lchild=lc->rchild;
lc->rchild=tn;
lc=tn;
// free(lc);
}
void LeftBalance(treebs tn)//二叉树左平衡旋转处理
{
treebs rd;
treebs lc=tn->lchild;
switch(lc->bf)
{
case LH:
tn->bf=lc->bf=EH;
R_Rotate(tn);
break;
case RH:
rd=lc->rchild;
switch(rd->bf)
{
case LH:
tn->bf=RH;lc->bf=EH;break;
case EH:
tn->bf=lc->bf=EH;break;
case RH:
tn->bf=EH;lc->bf=LH;break;
}
rd->bf=EH;
L_Rotate(tn->lchild);
R_Rotate(tn);
}
}
void RightBalance(treebs tn)//二叉树右平衡旋转处理
{
treebs rd;
treebs lc=tn->rchild;
switch(lc->bf)
{
case LH:
tn->bf=lc->bf=EH;
L_Rotate(tn);
break;
case RH:
rd=lc->rchild;
switch(rd->bf)
{
case LH:
tn->bf=EH;lc->bf=RH;break;
case EH:
tn->bf=lc->bf=EH;break;
case RH:
tn->bf=LH;lc->bf=EH;break;
}
rd->bf=EH;
R_Rotate(tn->lchild);
L_Rotate(tn);
}
}
int inserttn(treebs tn,int e,int taller) //插入结点
{
treebs p ;
//struct BStree *tn1;
//tn1=&tn;
if(!tn)
{
//if(!(tn=(BStree*)malloc(sizeof(BStree)))) return 0;
tn=(treebs)malloc(sizeof(BStree));
tn->data=e;
tn->lchild=tn->rchild=NULL;
tn->bf=EH;
taller=1;
}else
{
if(edata)
{
if(!inserttn(tn->lchild ,e,taller)) return 0;
if(taller!=0)
{
switch(tn->bf)
{
case LH:
LeftBalance(tn);taller=0;break;
case EH:
tn->bf=LH;taller=1;break;
case RH:
tn->bf=EH;taller=0;break;
}
}
}else
{
if(!inserttn(tn->rchild ,e,taller)) return 0;
if(taller)
{
switch(tn->bf)
{
case RH:
RightBalance(tn);taller=0;break;
case EH:
tn->bf=RH;taller=1;break;
case LH:
tn->bf=EH;taller=0;break;
}
}
}
}
return 1;
}
void showtree(treebs tn)
{
if(tn)
{
showtree(tn->lchild);
printf("%5d",tn->data);
showtree(tn->rchild);
}
}
int main()
{
treebs tn;
int taller;
int e;
/*tn = (treebs)malloc(sizeof(BStree));
tn->data=0;
tn->lchild=tn->rchild=NULL;
tn->bf=EH;*/
tn=NULL;
while(1)
{
printf("请输入数据:");
scanf("%d",&e);
if (e==0){ exit(0); }
inserttn(tn,e,taller);
showtree(tn);
printf("n");
}
showtree(tn);
return 1;
}
|
两个函数需要改一下:
/*
* Change pointer value via passing argument
* must use pointer to pointer, that is **
*/
int inserttn(treebs *tn,int e,int taller) //插入结点
{
if(!(*tn))
{
*tn=(treebs)malloc(sizeof(BStree));
(*tn)->data=e;
(*tn)->lchild=(*tn)->rchild=NULL;
(*tn)->bf=EH;
taller=1;
}
else
{
if(edata)
{
if(!inserttn(&((*tn)->lchild) ,e,taller)) return 0;
if(taller!=0)
{
switch((*tn)->bf)
{
case LH:
LeftBalance((*tn));
taller=0;
break;
case EH:
(*tn)->bf=LH;
taller=1;
break;
case RH:
(*tn)->bf=EH;taller=0;break;
}
}
}
else
{
if(!inserttn(&((*tn)->rchild) ,e,taller)) return 0;
if(taller)
{
switch((*tn)->bf)
{
case RH:
RightBalance((*tn));taller=0;break;
case EH:
(*tn)->bf=RH;taller=1;break;
case LH:
(*tn)->bf=EH;taller=0;break;
}
}
}
}
return 1;
}
int main()
{
treebs tn;
int taller;
int e;
/*tn = (treebs)malloc(sizeof(BStree));
tn->data=0;
tn->lchild=tn->rchild=NULL;
tn->bf=EH;*/
tn=NULL;
while(1)
{
printf("请输入数据:");
scanf("%d",&e);
if (e==0){exit(0);}
inserttn(&tn,e,taller); // 取2级指针
showtree(tn);
printf("n");
}
showtree(tn);
return 1;
}
请用二级指针, 我用其他开发工具试过这个改过的程序, 可以得到预定结果。
插入的递归实现没有错。
/*
* Change pointer value via passing argument
* must use pointer to pointer, that is **
*/
int inserttn(treebs *tn,int e,int taller) //插入结点
{
if(!(*tn))
{
*tn=(treebs)malloc(sizeof(BStree));
(*tn)->data=e;
(*tn)->lchild=(*tn)->rchild=NULL;
(*tn)->bf=EH;
taller=1;
}
else
{
if(edata)
{
if(!inserttn(&((*tn)->lchild) ,e,taller)) return 0;
if(taller!=0)
{
switch((*tn)->bf)
{
case LH:
LeftBalance((*tn));
taller=0;
break;
case EH:
(*tn)->bf=LH;
taller=1;
break;
case RH:
(*tn)->bf=EH;taller=0;break;
}
}
}
else
{
if(!inserttn(&((*tn)->rchild) ,e,taller)) return 0;
if(taller)
{
switch((*tn)->bf)
{
case RH:
RightBalance((*tn));taller=0;break;
case EH:
(*tn)->bf=RH;taller=1;break;
case LH:
(*tn)->bf=EH;taller=0;break;
}
}
}
}
return 1;
}
int main()
{
treebs tn;
int taller;
int e;
/*tn = (treebs)malloc(sizeof(BStree));
tn->data=0;
tn->lchild=tn->rchild=NULL;
tn->bf=EH;*/
tn=NULL;
while(1)
{
printf("请输入数据:");
scanf("%d",&e);
if (e==0){exit(0);}
inserttn(&tn,e,taller); // 取2级指针
showtree(tn);
printf("n");
}
showtree(tn);
return 1;
}
请用二级指针, 我用其他开发工具试过这个改过的程序, 可以得到预定结果。
插入的递归实现没有错。
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。