我正在嘗試生成字串的二叉樹。串列中包含的結構是使用以下代碼創建的:
typedef struct WordNode {
char *word;
unsigned count;
struct WordNode *left;
struct WordNode *right;
} wordnode;
我首先在主函式中將根節點初始化為 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);
}
}
}
This function checks if the current node is NULL. If it is, it assigns a new struct generated by the generateNode function using the given word to the current struct. If the current struct is not null, it compares the given word to the current structs word and either adds one to the counter or attempts to recursively place the node in the current nodes left struct (if it is less than the current structs word) or the right struct (if it is more).
The generateNode function generates a node like this:
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;
}
The issue I am experiencing is that the program seems to believe that the head struct is always NULL when it is checked in the placeWordInTree function. When placing printf statements for debugging, which told me when each of the possible outcomes of the placeWordInTree function, it always executed the code as if the head struct was always NULL. What is even more confusing though, is that when the contents of the tree were printed, the word contained by the head struct was always the first word of the input file. After lengthy debugging with no success, the tree is not even printed anymore.
I've checked the file input, and it works fine. I've checked the display function, and it worked fine (but I'll put it below here just in case.) And I've checked the main function, which all looked fine. The issue just seems to be that the else statement for the placeWordInTree functions initial if statement never executes.
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 ;
}
uj5u.com熱心網友回復:
您需要在 placeWordInTree 函式中通過參考傳遞節點。在 C 中這是通過傳遞一個指向所需變數的指標來實作的。
void placeWordInTree(char *word, wordnode **currNode)
并在呼叫函式時添加運算子的地址。
placeWordInTree(wordBuffer, &head);
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/335248.html
