【发布时间】:2014-02-06 05:28:15
【问题描述】:
我正在尝试创建一个用于搜索 n 叉树的函数,但效果不佳,它在第 2 级之后返回错误的节点。有人知道为什么吗?
这里是节点实现
typedef struct node
{
char name[30];
int year;
struct node* ptr;
struct node* p[10];
} node;
还有一个功能
node *search(node *p, char* name, int year)
{
int i, n;
if(p == NULL)
return (NULL);
if((!strcmp(p->name, name) && (p->year == year))
return (p);
n = number(p); \\returns number of childs
for(i = 0; i < n; i++)
if(search(p->p[i], name, year))
return (p->p[i]);
}
【问题讨论】:
-
如果
for循环search确实到达return会发生什么?你还没有处理这个案子。另外,ptr是做什么用的,代码中没有提到? -
如果forgets返回是什么意思? ptr 是指向父节点的指针,我需要它用于其他功能。