如何建立并遍历二叉树中节点为X的节点,如果找到返回其双亲节点的值否者返回0
答案:2 悬赏:50 手机版
解决时间 2021-12-17 19:44
- 提问者网友:眼中星河已尽
- 2021-12-17 07:09
如何建立并遍历二叉树中节点为X的节点,如果找到返回其双亲节点的值否者返回0
最佳答案
- 五星知识达人网友:葬她心田
- 2021-12-17 08:02
输入数据是整形的,每输入一个数,回车一次,子树为空输入0.以根左右的方式创建树。输入格式如:
10
21
0
0
51
0
0
该程序已测试,得到正确结果。
#include "stdlib.h"
typedef int Element;
struct Tree
{
Element date;
struct Tree *left, *right;
};
void CreateTree(struct Tree **r);
void PrintTree(struct Tree *r);
int ParentValue(struct Tree *r, int x);
struct Tree *Find2(struct Tree *r , struct Tree **p, int x);
int main()
{
struct Tree *r;
CreateTree(&r);
PrintTree(r);
Element x=1;
printf("\nPlease input the data you want to find:");
scanf("%d",&x);
while (x!=0)
{
int v=ParentValue(r,x);
if (v != 0)
{
printf("\nfind %d",v);
}
else
{
printf("\nnot find!");
}
printf("\nPlease input the data you want to find:");
scanf("%d",&x);
}
return 0;
}
void CreateTree(struct Tree **r)
{
Element ch;
scanf("%d",&ch);
if (ch == 0)
{
*r = NULL;
return ;
}
(*r) = (struct Tree *)malloc(sizeof(struct Tree));
(*r)->date = ch;
CreateTree( ( &(*r)->left));
CreateTree( &((*r)->right));
}
void PrintTree(struct Tree *r)
{
if (r == NULL)
{
return ;
}
printf("%d ",r->date);
PrintTree(r->left);
PrintTree(r->right);
}
int ParentValue(struct Tree *r, Element x)
{
struct Tree *p;
p = NULL;
Find2(r, &p, x);
if (p == NULL)
{
return 0;
}
else
{
return p->date;
}
}
struct Tree *Find2(struct Tree *r , struct Tree **p, int x)
{
if (r == NULL)
{
return NULL;
}
if (r->date == x)
{
return r;
}
struct Tree *temp;
temp = Find2(r->left, p, x);
if (temp != NULL)
{
*p = r;
return NULL;
}
temp = Find2(r->right, p, x);
if (temp != NULL)
{
*p = r;
return NULL;
}
return NULL;
}
10
21
0
0
51
0
0
该程序已测试,得到正确结果。
#include "stdlib.h"
typedef int Element;
struct Tree
{
Element date;
struct Tree *left, *right;
};
void CreateTree(struct Tree **r);
void PrintTree(struct Tree *r);
int ParentValue(struct Tree *r, int x);
struct Tree *Find2(struct Tree *r , struct Tree **p, int x);
int main()
{
struct Tree *r;
CreateTree(&r);
PrintTree(r);
Element x=1;
printf("\nPlease input the data you want to find:");
scanf("%d",&x);
while (x!=0)
{
int v=ParentValue(r,x);
if (v != 0)
{
printf("\nfind %d",v);
}
else
{
printf("\nnot find!");
}
printf("\nPlease input the data you want to find:");
scanf("%d",&x);
}
return 0;
}
void CreateTree(struct Tree **r)
{
Element ch;
scanf("%d",&ch);
if (ch == 0)
{
*r = NULL;
return ;
}
(*r) = (struct Tree *)malloc(sizeof(struct Tree));
(*r)->date = ch;
CreateTree( ( &(*r)->left));
CreateTree( &((*r)->right));
}
void PrintTree(struct Tree *r)
{
if (r == NULL)
{
return ;
}
printf("%d ",r->date);
PrintTree(r->left);
PrintTree(r->right);
}
int ParentValue(struct Tree *r, Element x)
{
struct Tree *p;
p = NULL;
Find2(r, &p, x);
if (p == NULL)
{
return 0;
}
else
{
return p->date;
}
}
struct Tree *Find2(struct Tree *r , struct Tree **p, int x)
{
if (r == NULL)
{
return NULL;
}
if (r->date == x)
{
return r;
}
struct Tree *temp;
temp = Find2(r->left, p, x);
if (temp != NULL)
{
*p = r;
return NULL;
}
temp = Find2(r->right, p, x);
if (temp != NULL)
{
*p = r;
return NULL;
}
return NULL;
}
全部回答
- 1楼网友:心毁拾荒
- 2021-12-17 08:38
额
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯