【问题标题】:Binary Search Tree in C, Segmentation Fault Error [duplicate]C中的二叉搜索树,分段错误错误[重复]
【发布时间】:2016-04-23 18:24:00
【问题描述】:

我在用 C 语言编写 BST 时遇到问题。我不断收到分段错误错误。我相信问题源于 insertNode 函数。我在函数中添加了 printf() 语句,并直接在函数调用之后查看是否添加了 newNode。请忽略其余代码,因为它没有完成,只是试图让 insertNode 函数工作。

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

//structure for node
struct btNode {
    int data;
    struct btNode *left;
    struct btNode *right;
};

//prototypes
struct btNode* createNode(int x);
void insertNode(struct btNode *tree, struct btNode *root);

int main(){
    int x,n=-1,i=0; //local variables

    struct btNode *head=NULL;

    while (n <= 0){
        printf("Enter the number of nodes in the Tree(>0): ");
        scanf("%i", &n);
    }


    while(i < n){
        printf("Enter an integer: ");
        scanf("%i", &x);
        struct btNode *newNode=createNode(x);
        insertNode(head,newNode);
        printf("%d",head->data); //breaks program here????

        i++;
    }

    while (x < 0){
        printf("Enter a integer from 0-5: ");
        scanf("%i",&x);

        if (x == 0){
            printf("Program Exit.\n");

            exit(0);

        }else if(x==1){


        }else if(x==2){


        }else if(x==3){

        }else if (x==4){

        }else if(x==5){

        }

        x=-1;
    }

    return 0;
}

//creates and returns a pointer to a new node
struct btNode* createNode(int x)
{
    struct btNode *newNode;
    newNode=(struct btNode*)malloc(sizeof(struct btNode));

    if (newNode == NULL){
        printf("Memory Allocation Failed./n");
        exit(20);
    }else{
        newNode->data=x;
        newNode->left=NULL;
        newNode->right=NULL;
        return newNode;
    }
}


void insertNode(struct btNode *tree, struct btNode *newNode){
    if (tree==NULL){
        tree=newNode;
        printf("%d",tree->data); //works fine here!
    }else if(tree->data <= newNode->data){
        insertNode(tree->right, newNode);
    }else if(tree->data > newNode->data){
        insertNode(tree->left, newNode);
    }
}

【问题讨论】:

  • 你应该测试来自scanf()的返回值;如果您的程序在第一个循环中获得 EOF 或非数字(例如a),它不会停止。始终测试来自scanf() 等的结果。如果您期望一个值,请测试它是否返回1;它可能返回0(表示输入的不是数字)或EOF
  • 这个问题实际上是许多其他问题的重复——列表和树都遇到了“如何将信息返回给调用代码”的基本问题。

标签: c pointers segmentation-fault binary-search-tree


【解决方案1】:

您必须在将node 插入树结构后返回它。所以你正确的功能是:

struct btNode *insertNode(struct btNode *tree, struct btNode *newNode){
    if (tree==NULL){
        tree=newNode;
        printf("%d", tree->data); //works fine here!
        return tree;
    }else if(tree->data <= newNode->data){
        tree->right = insertNode(tree->right, newNode);
        return tree;
    }else if(tree->data > newNode->data){
        tree->left = insertNode(tree->left, newNode);
        return tree;      
    }
}

同时修改你的调用:

head = insertNode(head, newNode);

【讨论】:

  • 或者只是让函数将struct btNode **tree 当作@OldProgrammer 所暗示的那样。
  • @JonathanLeffler 我有我的朋友。
  • 所以我明白了;直到我去编辑它才出现(对我来说,因为我使用 HTTPS Everywhere)......
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多