在二叉搜索树C中插入一个单词

在二叉搜索树C中插入一个单词,c,binary-search-tree,C,Binary Search Tree,我正在尝试构建一个二进制搜索树,它存储单词/定义对 所以我的结构是这样的: struct BinarySearchTree_t { char *word,*def; struct BinarySearchTree_t *left; struct BinarySearchTree_t *right; }; typedef struct BinarySearchTree_t BinarySearchTree; 所以我在insertWord函数中被阻塞,该函数在二叉搜索树中插

我正在尝试构建一个二进制搜索树,它存储单词/定义对

所以我的结构是这样的:

struct BinarySearchTree_t
{
    char *word,*def;
    struct BinarySearchTree_t *left;
    struct BinarySearchTree_t *right;
};
typedef struct BinarySearchTree_t BinarySearchTree;
所以我在insertWord函数中被阻塞,该函数在二叉搜索树中插入一个单词/定义对。该词或定义均不得为空。NULL被视为一个特殊值。如果单词已经存在,则此函数将用新定义替换当前定义并返回旧定义

这就是功能:

char* insertWord(BinarySearchTree *tree, char *word, char *definition)
{
    int r;
    char* a;

    if((tree==NULL))
    {
        BinarySearchTree* tmp;
        tmp = malloc( sizeof( BinarySearchTree ) );
        tmp->word= malloc((strlen(word)+1)*sizeof(char));
        tmp->def = malloc((strlen(definition)+1)*sizeof(char));
        strcpy(tmp->word, word);
        strcpy(tmp->def , definition);
        tmp->left = NULL;
        tmp->right = NULL;

        *tree = *tmp;
        return NULL;
    }
    else
    {
        a= tree->word;
        r= strcmp(a,word);
        if(r = 0)
        {
            char* ret= tree->def;
            strcpy(tree->word, word);
            strcpy(tree->def , definition);
            return ret;
        }
        else if(r<0)
           return insertWord((tree->right),word,definition);
        else
           return insertWord((tree->left),word,definition);

    }
}
char* insertWord(BinarySearchTree **tree, char *word, char *definition) 
    {                                                                   
    int r;
    char* a;

    if(((*tree)==NULL) || ((*tree)!=NULL && (*tree)->mot==NULL))
    {
        BinarySearchTree* tmp;
        tmp = malloc( sizeof( BinarySearchTree ) ); 
        tmp->left = NULL;                           
        tmp->right = NULL;                          

        tmp->mot = malloc((strlen(word)+1)*sizeof(char));
        tmp->def = malloc((strlen(definition)+1)*sizeof(char));
        strcpy(tmp->mot , word);                    
        strcpy(tmp->def , definition);              

        *tree = tmp;
        return NULL;
    }
    else
    {
        a= (*tree)->mot;
        r= strcmp(a,word);
        if(r == 0)
        {
            char* ret= (*tree)->def;
            strcpy((*tree)->mot , word);
            strcpy((*tree)->def , definition);
            return ret;
        }
        else if(r<0)
            return insertWord(&((*tree)->right),word,definition); 
        else
            return insertWord(&((*tree)->left),word,definition);
    }
}
char*insertWord(二进制搜索树*tree,char*word,char*definition)
{
INTR;
char*a;
if((tree==NULL))
{
二进制搜索树*tmp;
tmp=malloc(sizeof(BinarySearchTree));
tmp->word=malloc((strlen(word)+1)*sizeof(char));
tmp->def=malloc((strlen(定义)+1)*sizeof(char));
strcpy(tmp->word,word);
strcpy(tmp->def,定义);
tmp->left=NULL;
tmp->right=NULL;
*tree=*tmp;
返回NULL;
}
其他的
{
a=树->字;
r=strcmp(a,字);
如果(r=0)
{
char*ret=tree->def;
strcpy(树->单词,单词);
strcpy(树->定义,定义);
返回ret;
}
else if(右)、单词、定义);
其他的
返回insertWord((树->左),word,definition);
}
}
有什么问题


已编辑:正确的功能:

char* insertWord(BinarySearchTree *tree, char *word, char *definition)
{
    int r;
    char* a;

    if((tree==NULL))
    {
        BinarySearchTree* tmp;
        tmp = malloc( sizeof( BinarySearchTree ) );
        tmp->word= malloc((strlen(word)+1)*sizeof(char));
        tmp->def = malloc((strlen(definition)+1)*sizeof(char));
        strcpy(tmp->word, word);
        strcpy(tmp->def , definition);
        tmp->left = NULL;
        tmp->right = NULL;

        *tree = *tmp;
        return NULL;
    }
    else
    {
        a= tree->word;
        r= strcmp(a,word);
        if(r = 0)
        {
            char* ret= tree->def;
            strcpy(tree->word, word);
            strcpy(tree->def , definition);
            return ret;
        }
        else if(r<0)
           return insertWord((tree->right),word,definition);
        else
           return insertWord((tree->left),word,definition);

    }
}
char* insertWord(BinarySearchTree **tree, char *word, char *definition) 
    {                                                                   
    int r;
    char* a;

    if(((*tree)==NULL) || ((*tree)!=NULL && (*tree)->mot==NULL))
    {
        BinarySearchTree* tmp;
        tmp = malloc( sizeof( BinarySearchTree ) ); 
        tmp->left = NULL;                           
        tmp->right = NULL;                          

        tmp->mot = malloc((strlen(word)+1)*sizeof(char));
        tmp->def = malloc((strlen(definition)+1)*sizeof(char));
        strcpy(tmp->mot , word);                    
        strcpy(tmp->def , definition);              

        *tree = tmp;
        return NULL;
    }
    else
    {
        a= (*tree)->mot;
        r= strcmp(a,word);
        if(r == 0)
        {
            char* ret= (*tree)->def;
            strcpy((*tree)->mot , word);
            strcpy((*tree)->def , definition);
            return ret;
        }
        else if(r<0)
            return insertWord(&((*tree)->right),word,definition); 
        else
            return insertWord(&((*tree)->left),word,definition);
    }
}
char*insertWord(二进制搜索树**树,char*word,char*definition)
{                                                                   
INTR;
char*a;
如果(((*tree)=NULL)| |((*tree)!=NULL&&(*tree)->mot==NULL))
{
二进制搜索树*tmp;
tmp=malloc(sizeof(BinarySearchTree));
tmp->left=NULL;
tmp->right=NULL;
tmp->mot=malloc((strlen(单词)+1)*sizeof(字符));
tmp->def=malloc((strlen(定义)+1)*sizeof(char));
strcpy(tmp->mot,word);
strcpy(tmp->def,定义);
*tree=tmp;
返回NULL;
}
其他的
{
a=(*树)->mot;
r=strcmp(a,字);
如果(r==0)
{
char*ret=(*tree)->def;
strcpy((*树)->mot,word);
strcpy((*树)->def,定义);
返回ret;
}
else if(右)、单词、定义);
其他的
返回insertWord(&(*tree)->左),单词,定义;
}
}

所以,您在第一次访问SearchTree时,尝试初始化指向其根的指针,对吗?问题是您正在修改指针
*树的本地副本,而不是父(调用)函数中存在的实际指针。如果您计划在被调用函数中修改指向搜索树的指针,则应在调用
insertWord
中传递指向
*树的指针(即指向指针的指针)。
您应该将定义更改为:

char*insertWord(二进制搜索树**树,char*word,char*definition)


因此,您应该在
insertWord
函数中修改
的所有访问。

替代工作代码-通过以下方式获得干净的健康清单:

#包括
#包括
#包括
结构二进制搜索树
{
字符*word,*def;
结构二进制搜索树(左);
结构BinarySearchTree_t*右;
};
typedef结构二进制搜索树\u t二进制搜索树;
静态void freeTree(BinarySearchTree*root);
静态无效转储树(BinarySearchTree*根);
外部字符*插入字(二进制搜索树**ptree,字符*字,字符*定义);
char*insertWord(二进制搜索树**ptree,char*word,char*definition)
{
如果(*ptree==NULL)
{
二进制搜索树*tmp=malloc(sizeof(*tmp));
tmp->word=strdup(word);
tmp->def=strdup(定义);
tmp->left=NULL;
tmp->right=NULL;
*ptree=tmp;
返回tmp->def;
}
其他的
{
二进制搜索树*tree=*ptree;
int r=strcmp(树->字,字);
如果(r==0)
{
自由(树->定义);
树->定义=strdup(定义);
返回树->定义;
}
else如果(r<0)
返回insertWord(&tree->right,word,definition);
其他的
返回insertWord(&tree->left,word,definition);
}
}
内部主(空)
{
char*word_defs[][2]=
{
{“猫”,“猫”},
{“狗”,“狗”},
{“盒子”、“纸箱”},
{“猫”,“咕噜咕噜的小动物”},
};
BinarySearchTree*根=0;
对于(size_t i=0;i%s\n”,i,word_defs[i][0],word_defs[i][1]);
char*def=insertWord(&root,word_defs[i][0],word_defs[i][1]);
树根;
printf(“新定义:%s\n”,def);
}
自由树(根);
返回0;
}
静态void freeTree(BinarySearchTree*根)
{
如果(根!=0)
{
freeTree(根->左);
freeTree(根->右);
自由(根->字);
自由(根->定义);
自由根;
}
}
静态无效转储树(BinarySearchTree*根)
{
如果(根->左!=0)
转储树(根->左);
printf(“%p:%s=>%s\n”,(void*)根,根->字,根->定义);
如果(根->右!=0)
转储树(根->右);
}

这个版本报告了这个词的新定义。原始代码可能报告了旧定义;修复此代码以使其报告旧定义并不困难(但需要稍微小心,以确保旧定义已实际发布,且不打印空指针)。

strcpy(tree->mot,word)
此成员
mot
在您的结构中的何处?欢迎使用堆栈溢出。请尽快阅读这一页。你有什么问题