C 树不';不要长得超过3号

C 树不';不要长得超过3号,c,binary-search-tree,C,Binary Search Tree,我试图在二叉搜索树中实现插入子例程,每个节点都有一个单词(字符串)作为键。 我从STDIN中提取单词,并使用insert函数插入它们。但每当我按顺序遍历时,我发现树的长度不会超过3个节点。有什么不对劲?我的代码附在这里: typedef struct treenode { char word[20]; //word is the key for each node struct treenode *left; struct treenode *right;

我试图在二叉搜索树中实现插入子例程,每个节点都有一个单词(字符串)作为键。 我从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;
}
typedef结构树节点
{
char word[20];//word是每个节点的键
结构树节点*左;
结构树节点*右侧;
}三烯醇;
树节点*插入(树节点*节点,字符*字)
{   
if(node==NULL)//如果到达NULL节点,则创建一个临时节点并附加它
{
treenode*温度;
temp=(treenode*)malloc(sizeof(treenode));
strcpy(临时->单词,单词);
temp->left=NULL;
temp->right=NULL;
返回温度;
}
if((strcmp(word,node->word)>0))//如果键大于node.word,则转到右侧子项
{
节点->右侧=插入(节点->右侧,word);
}
else if(strcmp(字,节点->字)左,字);
}
}
无效打印顺序(treenode*节点)
{
if(node==NULL)返回;
打印顺序(节点->左侧);
printf(“%s”,节点->单词);
打印顺序(节点->右侧);
}
int main()
{
treenode*root=NULL;
字符串[20];
scanf(“%s”,string);//第一个输入在这里进行一次,在循环中进行一次,但它是确定的
root=insert(root,string+1);//还存储重复的条目
while(strcmp(string,“.”!=0)//输入以句号终止
{
插入(根,字符串+1);
scanf(“%s”,字符串);
}
printinorder(root);//最后打印BST
返回0;
}

实现中的问题是,您试图将
创建_节点
插入
组合到单个函数中,然后将该单个函数用作树初始化和插入的通用解决方案。在这里,它导致了
insert
返回类型的复杂性(如注释中所述),并可能导致您错过
insert
中所需的递归。(注意
插入
树->右
树->左
下添加和一个
else

最好将
create_节点
insert
例程拆分为单独的函数。这允许将
create\u节点
定义为类型
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;
}

此外,如评论中所述-请勿播放malloc。这是不必要的,而且会增加出错的可能性。

您可能希望在这件事的底部找到回报。和.尝试调试器并查看
*insert()
@WhozCraig谢谢,返回成功。
$ gcc -Wall -Wextra -o bst bst.c
$ ./bst
my_dog
has
fleas
my_cat
likes
snakes
.
fleas has likes my_cat my_dog snakes