Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C BST中的分段错误_C_Segmentation Fault - Fatal编程技术网

C BST中的分段错误

C BST中的分段错误,c,segmentation-fault,C,Segmentation Fault,当我调用main中的find_word函数时,我总是遇到一个分段错误。 当添加一个单词时,我希望返回1,当它找到该单词时,我希望它返回1。 所以我也不确定我的insert方法是否正确 #include <string.h> #include <stdlib.h> #include <stdio.h> struct node { char *word; struct node *left; struct node *right; };

当我调用main中的find_word函数时,我总是遇到一个分段错误。 当添加一个单词时,我希望返回1,当它找到该单词时,我希望它返回1。 所以我也不确定我的insert方法是否正确

#include <string.h>
#include <stdlib.h>
#include <stdio.h>

struct node {
    char *word;
    struct node *left;
    struct node *right;
};
static struct node *root;


int init(void)
{
    struct node *new_node = malloc (sizeof(struct node));
    if(new_node==NULL){
        return 0;
    }

    else{
        root = new_node;
        new_node->left = NULL;
        new_node->right = NULL;
        return 1;
    }
}

static int insert(struct node *newnode, char *word)
{
    struct node *temp = NULL;
    if(!(newnode))
        {
            temp = (struct node *)malloc(sizeof(struct node));
            temp->left =NULL;
            temp->right = NULL;
            temp->word = word;
            newnode = temp;
            return 0;
        }

    if(word < (newnode)->word)
        {
            insert((newnode)->left, word);
        }
    else if(word > (newnode)->word)
        {
            insert((newnode)->right, word);
        }
    return 1;
}

int add_word(char *word)
{
    return insert(root,word);

}
static int find(char *word, struct node *newnode){
    if(newnode==NULL){
        return 0;
    }
    else if(strcmp(word,newnode->word)>0){
        find(word,newnode->left);
    }

    else if(strcmp(newnode->word,word)<0){
        find(word,newnode->right);
    }
    else{

        return 1;

    }
    return 0;
}


int find_word(char *word)
{
    return find(word,root);
}




int main(int argc,char *argv[])
{
    int k;
    char l[5];


    k = init();
    printf("init: %d\n",k);

    strcpy(l,"x");
    k = add_word(l);
    printf("add_word(%s): %d\n",l,k);

    strcpy(l,"x");
    k = find_word(l);
    printf("find_word(%s): %d\n",l,k);



    return 0;
}
#包括
#包括
#包括
结构节点{
字符*字;
结构节点*左;
结构节点*右;
};
静态结构节点*root;
int init(void)
{
结构节点*新节点=malloc(sizeof(结构节点));
if(新节点==NULL){
返回0;
}
否则{
根=新的_节点;
新建_节点->左=空;
新建_节点->右=空;
返回1;
}
}
静态int-insert(结构节点*newnode,字符*word)
{
结构节点*temp=NULL;
如果(!(新节点))
{
temp=(结构节点*)malloc(sizeof(结构节点));
temp->left=NULL;
temp->right=NULL;
临时->单词=单词;
newnode=temp;
返回0;
}
如果(word<(新节点)->word)
{
插入((新节点)->左,单词);
}
else if(word>(新节点)->word)
{
插入((新节点)->右侧,word);
}
返回1;
}
int add_单词(字符*单词)
{
返回插入(根、字);
}
静态整型查找(字符*单词,结构节点*新节点){
if(newnode==NULL){
返回0;
}
else if(strcmp(word,newnode->word)>0){
查找(word,新节点->左);
}
else if(strcmp(newnode->word,word)right);
}
否则{
返回1;
}
返回0;
}
int find_单词(字符*单词)
{
返回find(单词、词根);
}
int main(int argc,char*argv[])
{
int k;
charl[5];
k=init();
printf(“初始:%d\n”,k);
strcpy(l,“x”);
k=添加单词(l);
printf(“添加单词(%s):%d\n”,l,k);
strcpy(l,“x”);
k=查找单词(l);
printf(“查找单词(%s):%d\n”,l,k);
返回0;
}

如果
newnode->word
为空,则应在当前节点插入单词,以处理空的根节点

static int insert(struct node *newnode, char *word)
{
    struct node *temp = NULL;
    if(!(newnode))
        {
            temp = (struct node *)malloc(sizeof(struct node));
            temp->left =NULL;
            temp->right = NULL;
            temp->word = malloc(strlen(word)+1);
            strcpy(temp->word, word);
            newnode = temp;
            return 0;
        }

    if (newnode->word == NULL) {
        newnode->word = malloc(strlen(word)+1);
        strcpy(newnode->word, word);
        return 1;
    }

    if(strcmp(word,(newnode)->word) < 0)
        {
            insert((newnode)->left, word);
        }
    else if(strcmp(word,(newnode)->word) > 0)
        {
            insert((newnode)->right, word);
        }
    return 1;
}
这样修理

#include <string.h>
#include <stdlib.h>
#include <stdio.h>

struct node {
    char *word;
    struct node *left;
    struct node *right;
};

static struct node *root = NULL;

static int insert(struct node **newnode, char *word){
    struct node *temp = NULL;
    int cmp;

    if(!*newnode){
        temp = (struct node *)malloc(sizeof(struct node));
        temp->left =NULL;
        temp->right = NULL;
        temp->word = strdup(word);
        *newnode = temp;
        return 0;
    }

    if((cmp=strcmp(word, (*newnode)->word)) < 0)
        return insert(&(*newnode)->left, word);
    if(cmp > 0)
        return insert(&(*newnode)->right, word);
    return 1;
}

int add_word(char *word){
    return insert(&root, word);
}

static int find(char *word, struct node *newnode){
    int cmp;
    if(newnode==NULL)
        return 0;
    if((cmp=strcmp(word, newnode->word)) == 0)
        return 1;
    if(cmp < 0)
        return find(word, newnode->left);
    return find(word, newnode->right);
}

int find_word(char *word){
    return find(word, root);
}

int main(int argc,char *argv[]){
    int k;
    char *w;

    k = add_word(w="x");
    printf("add_word(%s): %d\n", w, k);

    k = find_word(w);
    printf("find_word(%s): %d\n", w, k);

    return 0;
}
#包括
#包括
#包括
结构节点{
字符*字;
结构节点*左;
结构节点*右;
};
静态结构节点*root=NULL;
静态整数插入(结构节点**新节点,字符*word){
结构节点*temp=NULL;
int-cmp;
如果(!*newnode){
temp=(结构节点*)malloc(sizeof(结构节点));
temp->left=NULL;
temp->right=NULL;
temp->word=strdup(word);
*newnode=temp;
返回0;
}
如果((cmp=strcmp(word,(*newnode)->word))<0)
返回insert(&(*newnode)->左,单词);
如果(cmp>0)
返回insert(&(*newnode)->右键,单词);
返回1;
}
int add_单词(字符*单词){
返回insert(&root,word);
}
静态整型查找(字符*单词,结构节点*新节点){
int-cmp;
if(newnode==NULL)
返回0;
if((cmp=strcmp(word,newnode->word))==0)
返回1;
if(cmp<0)
返回find(word,newnode->left);
返回find(word,newnode->right);
}
int find_单词(字符*单词){
返回find(单词、词根);
}
int main(int argc,char*argv[]){
int k;
char*w;
k=添加单词(w=“x”);
printf(“添加单词(%s):%d\n”,w,k);
k=查找单词(w);
printf(“查找单词(%s):%d\n”,w,k);
返回0;
}

insert
应该使用
strcmp
来比较
word
newnode->word
,而不是
@Barmar我更改了它,所以它现在的
strcmp(word,newnode->word)>0
strcmp(word,newnode->word)你之前没有发布类似的问题吗?它看起来好像被删除了,因为我现在找不到它。正如我在这里所建议的,在调试器下运行代码,这样当错误发生时,您就可以看到哪些变量是无效的。我将尝试调试。您可以只更新原始问题,而不是发布新问题。我已更新答案,以显示如何在将
word
插入树中时制作副本。它仍然输出即使未添加
b
,仍能找到它。我很困惑。修复了
find
函数。好吧,它返回
0
,没有
b
,但添加
abc
时仍然返回
0
?尽管我现在明白了,我已经检查了两次同样的东西。对不起,我已经为你调试完你的程序了。如果你自己都不知道怎么做,你怎么能期望成为一名程序员呢?使用调试器,设置断点,单步通过程序查看它在做什么。
#include <string.h>
#include <stdlib.h>
#include <stdio.h>

struct node {
    char *word;
    struct node *left;
    struct node *right;
};

static struct node *root = NULL;

static int insert(struct node **newnode, char *word){
    struct node *temp = NULL;
    int cmp;

    if(!*newnode){
        temp = (struct node *)malloc(sizeof(struct node));
        temp->left =NULL;
        temp->right = NULL;
        temp->word = strdup(word);
        *newnode = temp;
        return 0;
    }

    if((cmp=strcmp(word, (*newnode)->word)) < 0)
        return insert(&(*newnode)->left, word);
    if(cmp > 0)
        return insert(&(*newnode)->right, word);
    return 1;
}

int add_word(char *word){
    return insert(&root, word);
}

static int find(char *word, struct node *newnode){
    int cmp;
    if(newnode==NULL)
        return 0;
    if((cmp=strcmp(word, newnode->word)) == 0)
        return 1;
    if(cmp < 0)
        return find(word, newnode->left);
    return find(word, newnode->right);
}

int find_word(char *word){
    return find(word, root);
}

int main(int argc,char *argv[]){
    int k;
    char *w;

    k = add_word(w="x");
    printf("add_word(%s): %d\n", w, k);

    k = find_word(w);
    printf("find_word(%s): %d\n", w, k);

    return 0;
}