n元树搜索功能 [英] n-ary tree searching function

查看:36
本文介绍了n元树搜索功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个用于 n 元树搜索的函数,但效果不佳,它在第 2 级后返回错误的节点.有人知道为什么吗?

I'm trying to make a function for n-ary tree searching, but it doesn't work well, it returns wrong node after level 2. Anyone knows why?

这里是节点实现

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]);
}

推荐答案

返回包含请求节点的子节点,但不返回节点本身.

You return the child that holds the requested node but not the node itself.

for(i = 0; i < n; i++)
{
    if ((p2 = search(p->p[i], name, year)))
            return p2;
}
return NULL;

这篇关于n元树搜索功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆