我正在尝试在二叉搜索树中实现插入子例程,其中每个节点都有一个单词(字符串)作为键。 我从STDIN中取出单词并使用insert函数插入它们。但每当我进行顺序遍历时,我发现树不会超过3个节点。有什么不对?我的代码附在此处:
typedef struct treenode
{
char word[20]; //word is the key for each node
struct treenode *left;
struct treenode *right;
} treenode;
treenode *insert (treenode *node, char *word)
{
if(node==NULL) //if a null node is reached,make a temp node and append it
{
treenode *temp;
temp = (treenode *) malloc(sizeof(treenode));
strcpy (temp ->word,word);
temp-> left = NULL;
temp-> right = NULL;
return temp;
}
if ((strcmp(word, node ->word)>0)) //if the key is greater than node.word,go to right child
{
node-> right = insert(node-> right, word);
}
else if(strcmp(word,node->word)<=0) //if key <node.word,go to left child
{
node-> left = insert(node-> left, word);
}
}
void printinorder(treenode *node)
{
if(node == NULL) return;
printinorder(node-> left);
printf("%s ",node-> word);
printinorder(node-> right);
}
int main()
{
treenode *root = NULL;
char string[20];
scanf("%s",string); //first input is taken once here and once again inside loop,but its OK
root = insert(root, string+1); //duplicate entries are also stored
while(strcmp(string,".")!=0) //input is terminated by a full stop
{
insert(root, string+1);
scanf("%s",string);
}
printinorder(root); //finally printing the BST
return 0;
}
答案 0 :(得分:0)
您的实现问题在于您尝试将create_node
和insert
组合到一个函数中,然后将该单个函数用作树的“一刀切”解决方案初始化和插入。这引起了insert
返回类型的并发症(如评论中所述),可能会导致您错过insert
中所需的递归。 (注意 else
和insert
代码下的tree->right
中添加了tree->left
最好将create_node
和insert
例程拆分为单独的函数。这样就可以将create_node
定义为类型treenode
,将insert
定义为类型void
。这简化了每个代码,使逻辑更容易,更易读。这样,您的代码就会变成:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct treenode
{
char word[20]; //word is the key for each node
struct treenode *left;
struct treenode *right;
} treenode;
treenode* create_node (char *word)
{
treenode* tmp = malloc (sizeof (struct treenode));
if (tmp) {
strcpy (tmp ->word,word);
tmp->left = NULL;
tmp->right = NULL;
} else {
fprintf (stderr, "%s() error: memory exhausted\n", __func__);
}
return tmp;
}
void insert (treenode *tree, char *word)
{
if (strcmp (word, tree->word) > 0)
{
if (tree->right == NULL) {
tree->right = create_node (word);
insert (tree->right, word);
} else {
insert (tree->right, word);
}
}
else if (strcmp (word, tree->word) < 0)
{
if (tree->left == NULL) {
tree->left = create_node (word);
insert (tree->left, word);
} else {
insert (tree->left, word);
}
}
}
void printinorder(treenode *node)
{
if(node == NULL) return;
printinorder(node-> left);
printf("%s ",node-> word);
printinorder(node-> right);
}
int main()
{
treenode *root = NULL;
char string[20];
scanf ("%s",string); //first input is taken once here and once again inside loop,but its OK
root = create_node (string); //duplicate entries are also stored
while (strcmp (string,".")!=0) //input is terminated by a full stop
{
insert (root, string);
scanf ("%s",string);
}
printinorder(root); //finally printing the BST
return 0;
}
<强>建立/运行:强>
$ gcc -Wall -Wextra -o bst bst.c
$ ./bst
my_dog
has
fleas
my_cat
likes
snakes
.
fleas has likes my_cat my_dog snakes
另外,正如评论中所述 - 不要投射malloc 。这是不必要的,并增加了出错的可能性。