C 在二叉搜索树中查找节点
我的findNode在我的insert函数中被调用,地址为tree。因为我插入的第一个单词树应该是空的,但是当我调试时,它跳过了FindWords函数中的这个检查 我不确定这里的树的值是多少,如果它不是空的,请告诉我C 在二叉搜索树中查找节点,c,search,binary-search-tree,C,Search,Binary Search Tree,我的findNode在我的insert函数中被调用,地址为tree。因为我插入的第一个单词树应该是空的,但是当我调试时,它跳过了FindWords函数中的这个检查 我不确定这里的树的值是多少,如果它不是空的,请告诉我 struct TREENODE { struct TREENODE *parent; // point to parent node of this node struct TREENODE *left; // point to left child of this
struct TREENODE {
struct TREENODE *parent; // point to parent node of this node
struct TREENODE *left; // point to left child of this node
struct TREENODE *right; // point to right child of this node
char *word; // store a unique word in the text file
int count; // num of times this word appears in the file
};
typedef struct TREENODE NODE;
#define MAXWORDLEN 1000
当我插入第一个单词时,树在这里应该是空的,因为不存在单词。但是当不存在单词时,该函数不返回NULL,而是跳转到if语句(strcmp(word,tree->word==0))并导致分段错误
NODE* findNode(char *word, NODE* tree) {
// return node storing word if exists
if (tree == NULL){return NULL;}
if (strcmp(word, tree->word)==0)
return tree;
if (strcmp(word, tree->word) <0)
return findNode(word, tree->left);
return findNode(word, tree->right);
}
void insertNode(char *word, NODE** address_of_tree ) {
NODE *tree = *address_of_tree;
NODE *node;
node = findNode(word, tree);
if (node == NULL) {
NODE *new_node = malloc(sizeof(NODE));
new_node->word = word;
new_node->parent = *address_of_tree;
new_node->left = NULL;
new_node->right = NULL;
if (tree == NULL) {
*address_of_tree = new_node;
return;
}
if (strcmp(word, tree->word) < 0) {
// word must be added to left subtree
if (tree->left !=NULL) insertNode(word, &tree->left);
else {
new_node->parent = tree;
tree->left = new_node;
}
}
else {
if (tree->right != NULL) insertNode(word, &(tree->right));
else {
new_node->parent = tree;
tree->right = new_node;
}
}
}
else {
// update the count for the word
node->count += 1;
}
}
void printTree(NODE* tree) {
// print each word and its count in alphabetical order
if (tree == NULL) return;
printTree(tree->left);
printf("word: %s\ncount: %d", tree->word, tree->count);
printTree(tree->right);
}
int main() {
// read text from stdin
// each time we read a word
// insert this word into the tree
NODE *tree; // the tree we are using to store the words
char word[MAXWORDLEN];
while(scanf("%s", word) != EOF) {
// insert this word into the tree
insertNode(word, &tree);
}
printTree(tree);
return 0;
}
NODE*findNode(char*word,NODE*tree){
//返回存储单词的节点(如果存在)
如果(tree==NULL){returnnull;}
if(strcmp(字、树->字)==0)
回归树;
if(strcmp(字,树->字)左);
返回findNode(字、树->右);
}
void insertNode(字符*字,节点**树的地址){
NODE*tree=*目录树的地址;
节点*节点;
node=findNode(字、树);
if(node==NULL){
NODE*new_NODE=malloc(sizeof(NODE));
新建节点->word=word;
新建_节点->父节点=*_树的地址_;
新建_节点->左=空;
新建_节点->右=空;
if(tree==NULL){
*_树的地址_=新的_节点;
返回;
}
if(strcmp(字、树->字)<0){
//单词必须添加到左子树
if(tree->left!=NULL)insertNode(word,&tree->left);
否则{
新建_节点->父节点=树;
树->左=新建_节点;
}
}
否则{
如果(tree->right!=NULL)insertNode(word,&(tree->right));
否则{
新建_节点->父节点=树;
树->右=新建_节点;
}
}
}
否则{
//更新单词的计数
节点->计数+=1;
}
}
无效打印树(节点*树){
//按字母顺序打印每个单词及其计数
if(tree==NULL)返回;
打印树(树->左);
printf(“单词:%s\n计数:%d”,树->单词,树->计数);
打印树(树->右);
}
int main(){
//从标准文本中读取文本
//每次我们读一个单词
//将这个词插入树中
NODE*tree;//我们用来存储单词的树
字符字[MAXWORDLEN];
while(scanf(“%s”,word)!=EOF){
//将这个词插入树中
插入节点(单词和树);
}
打印树(树);
返回0;
}
您正在缓冲区word
中获取输入,并将其传递给插入节点()
。在insertNode()
中,您正在执行以下操作
new_node->word = word;
这将使所有新节点new\u node->word
指针指向相同的缓冲区word
。word
缓冲区内容的任何更改都将反映在所有节点nodes->word
值中。相反,你应该这样做
new_node->word = strdup(word);
strdup()
复制给定字符串。它使用malloc
为新字符串获取内存。一旦你用完了,一定要把它放出来
在创建树之前,还应使用NULL
初始化树
NODE *tree = NULL;
因为您正在传递指向
insertNode()
的tree
指针,并且在insertNode()
中检查tree
指针是否为NULL
。因此,您应该使用NULL
显式初始化树
指针,您最初如何调用insertNode
函数?请将您的问题包括在内。谢谢您的回答。我添加了一个更具体的点,指出了问题所在以及代码的其余部分。请通知抱歉,感谢更改itI我尝试了这两种方法,但我遇到了分段错误,因为在插入节点函数中,我调用了findNode函数,因此如果将树指针设置为NULL,它会自动表示节点不存在。在这种情况下,计数将始终为零。@dark\u deer您将tree
设置为NULL
的位置在哪里?在进入while
循环之前,应在main()中设置tree=NULL
。当findNode()
函数为NULL
(这是递归的终止条件)时,它正在处理tree
。因此,当tree
为NULL
时调用findNode()
应该不会有任何问题。通过这两个更改,当我运行代码时,我得到了正确的输出-输入:a
,输出:word:a,count:2
。