这是二叉树遍历的应用,我的删除结点的操作为什么没执行那?请各位帮忙看看哪里出问题了,谢谢了
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
typedef int ElemType;
typedef struct BitNode
{
ElemType data;
struct BitNode *lchild,*rchild;
}BitNode,*BitTree;
BitTree CreateBiTree(void)
{
BitTree bt;
ElemType x;
scanf("%d",&x);
if(x==-1)
bt=NULL; //安排空指针
else
{
bt=(BitTree)malloc(sizeof(BitNode));
bt->data =x; //生成新结点
bt->lchild =CreateBiTree(); //建立左子树
bt->rchild =CreateBiTree(); //建立右子树
}
return bt; //返回根节点指针
}
void InOrderTraverse(BitTree bt) //中序遍历
{
if(bt!=NULL)
{
InOrderTraverse(bt->lchild);
printf("%4d",bt->data);
InOrderTraverse(bt->rchild);
}
}
int n=0;
int leafcount(BitTree bt)
{
if(bt!=NULL)
{
if(bt->lchild==NULL&&bt->rchild==NULL)
n++;
leafcount(bt->lchild);
leafcount(bt->rchild);
}
return n;
}
BitTree exchangetree(BitTree bt)
{
BitTree t;
if(bt!=NULL)
{
if(bt->lchild!=NULL||bt->rchild!=NULL)
{
t=bt->lchild;bt->lchild=bt->rchild;bt->rchild=t;
}
bt->lchild=exchangetree(bt->lchild);
bt->rchild=exchangetree(bt->rchild);
}
return bt;
}
int hightree(BitTree bt)
{
int H,H1,H2;
if(bt==NULL)
H=0;
else
{
H1=hightree(bt->lchild);
H2=hightree(bt->rchild);
H=(H1>H2?H1:H2)+1;
}
return H;
}
int find=0;
void searchtree(BitTree bt,ElemType x,BitTree *p,BitTree *f)
{
if(bt!=NULL&&!find)
if(bt->data==x)
{
find=1;*p=bt;
}
else
{
*f=bt;
searchtree(bt->lchild,x,p,f);
*f=bt;
searchtree(bt->rchild,x,p,f);
}
}
void deltree(BitTree bt,ElemType x)
{
BitTree p,f,q,s;
p=f=NULL;
searchtree(bt,x,&q,&f);
if(p!=NULL)
if(p->lchild!=NULL)
{
q=p->lchild;
s=q;
while(s->rchild!=NULL)
{
q=s;s=s->rchild;
}
if(s!=q)
q->rchild=s->lchild;
else
p->lchild=q->lchild;
p->data=s->data;
free(s);
}
else
{
if(f!=NULL)
if(p==f->lchild)
f->lchild=p->rchild;
else
f->rchild=p->rchild;
else
bt=bt->rchild;
free(p);
}
else
printf("结点x在二叉树中不存在!\n");
}
void main()
{
BitTree bt=NULL,p,f;
ElemType x;
int count=0,high=0;
bt=CreateBiTree();
printf("中序遍历:\n");
InOrderTraverse(bt);
printf("\n叶子节点的个数:\n");
count=leafcount(bt);
printf("%4d",count);
printf("\n交换左右子树:\n");
exchangetree(bt);
InOrderTraverse(bt);
printf("\n删除结点x=");
scanf("%d",&x);
searchtree(bt,x,&p,&f);
printf("\n");
InOrderTraverse(bt);
printf("\n");
getch();
}