求助,二叉树的查找结点问题
答案:2 悬赏:40 手机版
解决时间 2021-11-14 19:55
- 提问者网友:心如荒岛囚我终老
- 2021-11-14 04:17
求助,二叉树的查找结点问题
最佳答案
- 五星知识达人网友:走死在岁月里
- 2021-11-14 05:49
#include
#include
#include
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)
{
if(bt!=NULL&&!find)
if(bt->data==x)
{
find=1;
printf("结点存在\n");
return;
}
else
{
searchtree(bt->lchild,x);
searchtree(bt->rchild,x);
}
}
void main()
{
BitTree bt=NULL;
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的结点:\n");
printf("\n输入结点x=");
scanf("%d",&x);
searchtree(bt,x);
if(find==0)
printf("结点不存在\n");
printf("\n");
getchar();
}
哎哎,改了。。。。
运行结果:
1 2 -1 -1 3 -1 -1
中序遍历:
2 1 3
叶子节点的个数:
2
交换左右子树:
3 1 2
查找值为x的结点:
输入结点x=2
结点存在
Press any key to continue
#include
#include
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)
{
if(bt!=NULL&&!find)
if(bt->data==x)
{
find=1;
printf("结点存在\n");
return;
}
else
{
searchtree(bt->lchild,x);
searchtree(bt->rchild,x);
}
}
void main()
{
BitTree bt=NULL;
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的结点:\n");
printf("\n输入结点x=");
scanf("%d",&x);
searchtree(bt,x);
if(find==0)
printf("结点不存在\n");
printf("\n");
getchar();
}
哎哎,改了。。。。
运行结果:
1 2 -1 -1 3 -1 -1
中序遍历:
2 1 3
叶子节点的个数:
2
交换左右子树:
3 1 2
查找值为x的结点:
输入结点x=2
结点存在
Press any key to continue
全部回答
- 1楼网友:洒脱疯子
- 2021-11-14 06:13
五湖四海皆春色 万水千山尽得辉 横批:万象更新
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯