【发布时间】:2021-12-10 00:40:57
【问题描述】:
我正在尝试生成字符串的二叉树。列表中包含的结构是使用以下代码创建的:
typedef struct WordNode {
char *word;
unsigned count;
struct WordNode *left;
struct WordNode *right;
} wordnode;
我在 main 函数中首先将根节点初始化为 NULL,如下所示:
// The initial struct begins empty.
wordnode *head = NULL;
这是因为初始结构将是给定输入文件中包含的第一个单词。一旦它被初始化为 NULL,我就开始处理文件内容:
// While the end of file marker has not been reached...
while (EOF != fscanf(inHandle, BUFFMT, wordBuffer)) {
// Clean the word currently in the wordBuffer.
cleanWord(wordBuffer);
// If the buffer contains an actual word...
if (strlen(wordBuffer) > 0) {
// Add one to the total word count.
totalWords++;
placeWordInTree(wordBuffer, head);
}
}
placeWordInTree 函数处理输入文件的每个单词(占第一个空根节点):
void placeWordInTree(char *word, wordnode *currNode) {
// If the current node is empty, initialize it to a newly generated node.
if (currNode == NULL) {
currNode = generateNode(word);
// If the base case has not been satisfied...
} else {
// Store the result of comparing the given word and the current structs word.
int strComp = strcmp(word, currNode->word);
// If the words are equal, add one to the current structs counter.
if (strComp == 0) {
currNode->count++;
// If the given word is less than the struct word, recursively call this function
// with the given node and the current structs left node.
} else if (strComp < 0) {
placeWordInTree(word, currNode->left);
// If the given word is greater than the struct word, recursively call this
// function with the given word and the current structs right node.
} else if (strComp > 0) {
placeWordInTree(word, currNode->right);
}
}
}
这个函数检查当前节点是否为NULL。如果是,则将 generateNode 函数使用给定单词生成的新结构分配给当前结构。如果当前结构不为空,它将给定字与当前结构字进行比较,然后将计数器加一或尝试递归地将节点放置在当前节点左结构中(如果它小于当前结构字)或正确的结构(如果更多)。
generateNode 函数生成一个像这样的节点:
wordnode *generateNode(char *word) {
// Allocates enough memory to hold a new word node.
wordnode *newNode = malloc(sizeof(wordnode));
// Allocates enough memory to store the new nodes given word.
newNode->word = malloc(strlen(word)+1);
// Copies the given word into the new nodes word field.
strncpy(newNode->word, word, strlen(word));
// Sets the new nodes count to 1, and its branches to NULL.
newNode->count = 1;
newNode->left = NULL;
newNode->right = NULL;
// Return the new node.
return newNode;
}
我遇到的问题是程序似乎认为在 placeWordInTree 函数中检查 head 结构时始终为 NULL。当放置 printf 语句进行调试时,它告诉我 placeWordInTree 函数的每个可能结果何时执行,它总是执行代码,就好像 head 结构总是 NULL。更令人困惑的是,当打印树的内容时,head 结构包含的单词始终是输入文件的第一个单词。经过长时间的调试没有成功,树甚至不再打印了。
我检查了文件输入,它工作正常。我检查了显示功能,它工作正常(但我将它放在下面以防万一。)我检查了主要功能,一切看起来都很好。问题似乎只是 placeWordInTree 函数的 else 语句初始 if 语句永远不会执行。
unsigned displayTree(wordnode *currNode) {
unsigned treeLength = 0;
// If the current node is NULL, return 0;
if (currNode == NULL) {
return treeLength;
// If the current node is not null, recursively call displayList on its left and
// right pointers and add their return value to the total tree node count.
} else {
printf("\n%6u\t%s", currNode->count, currNode->word);
treeLength += displayTree(currNode->left);
treeLength += displayTree(currNode->right);
}
return treeLength++;
}
【问题讨论】:
-
我认为这让我朝着正确的方向前进!我现在将指针传递给指向结构的指针,但是当我使用'->'(即*currNode->word)引用结构的值时,编译器返回一个错误,指出我应该使用'->' '->'。我也尝试过没有取消引用指向指针的指针,但它仍然返回相同的问题。
-
看起来像 operator precedence 问题:尝试将
*currNode->word更改为(*currNode)->word。 -
看来这就是问题所在!来自 (*currNode) 的引用值现在可以工作了!我还不得不有点奇怪地引用左右节点: &((*currNode)->left) 除此之外,新问题只是一个段错误......打印语句放置表明它发生在 placeWordInTree 函数是叫什么?