简体   繁体   English

C-使用递归创建二叉树时出现分段错误

[英]C - Segmentation Fault while Creating Binary Tree Using recursion

I was trying to write a simple program of creating a binary tree using pointers in C , but I am unable to find the problem with this code. 我试图编写一个使用C中的指针创建二叉树的简单程序,但是我找不到此代码的问题。 I am receiving Segmentation Fault on the second insertion. 我在第二次插入时收到分段错误。

The program takes input of five numbers and then creates a binary tree using the array input.Sample run : 该程序输入五个数字,然后使用数组输入创建一个二叉树。

Here is the output of the program 这是程序的输出

Enter 5 elements : 输入5个元素:

45 78 89 32 46 45 78 89 32 46

In generateBST 在generateBST中

In insert Going to right sub tree 在插入转到右子树

In insert 在插入

Segmentation fault 分段故障

Please help me resolve this error. 请帮助我解决此错误。 Thank you. 谢谢。

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

typedef struct node {
    int value;
    struct node * lst;
    struct node * rst;
}Node;

void printBST(Node *root){
    puts("In printBST");
    if(root == NULL){
        return;
    }
    printBST(root->lst);
    printf(" %d ", root->value);
    printBST(root->rst);
}

void insert(Node **root, int element){
    puts("In insert");
    if((*root)->value > element){
        puts("Going to left sub tree");
        insert(&(*root)->lst ,element);
    } else if ((*root)->value < element) {
        puts("Going to right sub tree");
        insert(&(*root)->rst ,element);
    } else {
        puts("Creating a new node to insert");
        Node * newNode = (Node*)malloc(sizeof(Node));
        newNode->value = element;
        newNode->lst = NULL;
        newNode->rst = NULL;
        (*root) = newNode;
    }
}

Node* generateBST(int *elements, int n){
    puts("In generateBST");
    int i =0;
    Node * root = NULL;
    root = (Node*)malloc(sizeof(Node));
    root->value = *(elements);
    root->lst = NULL;
    root->rst = NULL;
    for(i=1; i < n; i++){
        insert(&root, *(elements+i));
    }
    return root;
}

int main(){
    Node * root = NULL;
    int i = 0, element, *elements ;
    elements = (int*)malloc(sizeof(int)*5);
    puts("Enter 5 elements : ");
    fflush(stdin);
    for(i = 0; i < 5; i++){
        scanf("%d",&element);
        elements[i] = element;
    }
    root = generateBST(elements,5);
    printBST(root);
    //deallocBST(root);
    return 0;
}

In insert, if *root is null you get a segmentation fault when you dereference (*root)->value . 在插入中,如果*root为null,则在取消引用(*root)->value时会遇到分段错误。 You need to handle that case: 您需要处理这种情况:

void insert(Node **root, int element){
    puts("In insert");
    if (*root == null || (*root)->value == element) {
        puts("Creating a new node to insert");
        Node * newNode = malloc(sizeof(Node));
        newNode->value = element;
        newNode->lst = NULL;
        newNode->rst = NULL;
        (*root) = newNode;
    } else if((*root)->value > element){
        puts("Going to left sub tree");
        insert(&(*root)->lst ,element);
    } else {  /* (*root)->value < element */
        puts("Going to right sub tree");
        insert(&(*root)->rst ,element);
    }
}

Just found your problem using gdb. 刚刚使用gdb找到了您的问题。 You have an error on line 22. The problem is that you don't check whether root is null or not which is problem for the insertion of the first element. 第22行出现错误。问题是您不检查root是否为null,这对于插入第一个元素是有问题的。

To run your program through gdb simply compile and run using: 要通过gdb运行程序,只需使用以下命令进行编译和运行:

g++ -g YourPorgram.cpp
gdb a.out
run

When the programs stops (segfaultor other problems) type: 当程序停止(segfaultor或其他问题)时,键入:

bt

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM