二叉树插入(仅获取最后一项......)

时间:2022-05-04 20:14:06

I'm trying to load a file which has couple of infor, and I want to add them into a binary tree. For sure there is no problem with addToTree and printPreOrder functions, but i don't know why I can only print ( or maybe add ) the last item in the file. Whats the problem?

我正在尝试加载一个有几个infor的文件,我想将它们添加到二叉树中。确定addToTree和printPreOrder函数没有问题,但我不知道为什么我只能打印(或者可能添加)文件中的最后一项。有什么问题?

newTree = createBinTree(&cmpInt, &destroyNode);
newRes = createRes();
while (fgets(buffer, 100, fp) != NULL)
{
    strcpy(newRes->name,strtok(buffer, ","));
    strcpy(newRes->food,strtok(NULL, ","));
    newRes->star = atoi(strtok(NULL, ","));
    addToTree(newTree, newRes);
}
printPreOrder(newTree, &printNode);

1 个解决方案

#1


2  

You are always adding to the same pointer to newRes. While reading you overwrite your previous newRes entries. You need to call createRes for each object you are reading.

您总是添加到newRes的相同指针。在读取时会覆盖以前的newRes条目。您需要为正在阅读的每个对象调用createRes。

#1


2  

You are always adding to the same pointer to newRes. While reading you overwrite your previous newRes entries. You need to call createRes for each object you are reading.

您总是添加到newRes的相同指针。在读取时会覆盖以前的newRes条目。您需要为正在阅读的每个对象调用createRes。