gpt4 book ai didi

C - n-ary 树的根未被保存/更新

转载 作者:太空宇宙 更新时间:2023-11-04 00:00:49 24 4
gpt4 key购买 nike

我正在尝试编写一个程序,将家谱表示为 n 叉树。该程序必须从 CSV 文件中读取名称并构建树。树由以下结构表示:

typedef
struct NTree_S {
char * name; // name of the person
struct NTree_S *next; // pointer to first child
struct NTree_S *child; // pointer to next sibling
} NTree;

当使用硬编码值时,程序在构建树和更新根时没有问题。

// CASE #4 //
printf("%s\n", "Entered CASE #4");
NTree *root4 = NULL;
root4 = add_child(root4, "Dad", "Son");
root4 = add_child(root4, "Son", "Baby");
print_tree(root4, root4->name);

输出:

Entered CASE #4
Dad had Son
Son had Baby
Baby had no offspring.

但是,当使用 build_tree() 函数时,程序不会保 stub 。

NTree * build_tree( FILE *fp) {
NTree* root = NULL;
char line[1024]; // max length of any line in an input file
while(fgets(line, sizeof(line), fp) != NULL) {
char *token = strtok(line, ",");
char *parent = token; // save first token as parent
while(token != NULL) {
token = strtok(NULL, ","); // get next token
root = add_child(root, parent, token); // add child
if(root == NULL) {
printf("%s\n", "root is NULL");
}
}
}
return root; // built tree
}

该函数获取正确的父项和要添加的标记(子项),但始终打印树为 NULL。我不确定为什么没有保存和更新根目录。由于工作中的硬编码示例,我对更改我的实现以使用指向指针的指针犹豫不决。为什么在硬编码示例中而不是在 build_tree() 中更新和保 stub 目录?

更新:

我更改了 build_tree() 声明:

void build_tree(NTree** root, FILE *fp);

然后我这样调用 add_child():

add_child(root, parent, token); // add child

但是,我的 root 仍然有同样的问题。每次我打印树时,都会发生段错误,因为根是 NULL。有人可以就我的 add_child 函数给我反馈吗?

void add_child(NTree **tree, char* parent, char* child) {
NTree *add = create_node(child); // node to add
if(*tree == NULL) { // tree is empty
*tree = create_node(parent); // add parent as the root
(*tree)->child = add;
return;
}
NTree *found = find_node(*tree, parent); // search tree for parent
if(found != NULL) { // found parent
printf("%s\n", "found parent");
NTree *found2 = find_node(found, child); // search parent tree
if(found2 == NULL) { // child not already in tree
found =add_child_helper(found, child); // add child
return;
} else {
// error
return;
}
} else { // parent not found
int cmp = strcmp((*tree)->name, child); // child is root
if(cmp == 0) {
NTree *newroot = create_node(parent); // new root
newroot->child = *tree;
return;
} else {
// error
return;
}
}
}
return;
}

NTree * add_child_helper(NTree *parent, char* child) {
if(parent->child) { // parent already has child
return add_sibling(parent->child, child);
} else {
parent->child = create_node(child); // make child
return parent;
}
}

NTree * add_sibling(NTree *child, char* sibling) {
while(child->next) { // find last sibling
child = child->next;
}
child->next = create_node(sibling); // add the sibling
return child;
}

更新 2: 从命令行运行时,会保存原始根目录,但未正确放置子目录。这是一个例子:

  • command>添加爸爸,儿子
  • root 为空...
  • 命令>打印爸爸
  • 爸爸生了儿子
  • 儿子没有后代。
  • command>添加儿子,宝贝
  • 找到 parent
  • parent 姓名:儿子
  • 错误:子项已经作为父项的子项在树中。
  • command>添加随机,随机
  • 找到 parent
  • 家长姓名:随机
  • 错误:子项已经作为父项的子项在树中。
  • 命令>打印爸爸
  • 爸爸有随机的
  • random 没有后代。

Dad 根已保存,但它只会有一个 child 。 add_child_helper 也应该使用指向指针的指针吗?

最佳答案

我将这两个函数都更改为采用双指针:

add_sibling(NTree** child, char* sibling);
add_child_helper(NTree** parent, char* child);

将 &root 传递给 add_child 正确地更新了根。

add_child(&root, parent, child);

谢谢你的帮助。

关于C - n-ary 树的根未被保存/更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45383886/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com