Home>

I am creating an ascending sort algorithm using a binary tree in C language.
If i try to display them in ascending order, a segmentation fault will occur. I don't know the cause, so please let me know.

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

struct tree {
    int value;
    struct tree * left;
    struct tree * right;
};
// add node
int append (int input, struct tree * current_node) {
    if (input<current_node->value) {
        if (current_node->left! = NULL) {
            append (input, current_node->left);
        } else {
            struct tree new = {input, NULL, NULL};
            current_node->left =&new;
        }
    } else {
        if (current_node->right! = NULL) {
            append (input, current_node->right);
        } else {
            struct tree new = {input, NULL, NULL};
            current_node->right =&new;
        }
    }
}

// Display in ascending order
void sort (struct tree * current_node) {
    if (current_node->left! = NULL) {
        sort (current_node->left);
    }
    printf ("% d,", current_node->value);
    if (current_node->right! = NULL) {
        sort (current_node->right);
    }
}

int main () {
    int sequence [] = {2, 4, 6, 1, 8, 2, 10, 0};
    int n = sizeof (sequence)/sizeof (int);
    struct tree root = {
        sequence [0],
        NULL,
        NULL
    };
    for (int i = 1;i<n;i ++) {
        append (sequence [i],&root);
    }
    sort (&root);
    printf ("\ n");
}

There is a segmentation fault at the sort () function.

What I tried

I tried stepping with gdb, but when I entered sort (), the value of each node was strange like -129660088.

environment

WSL2, Ubuntu20.04LTS, gcc version 9.3.0,

  • Answer # 1

    append struct tree new = {input, NULL, NULL};new is
    It is an automatic variable, memory is allocated here and it is initialized by input,
    current_node->left =&new;or current_node->right =&new;
    Later} will free up new memory and use it for other purposes.
    current_node->left and current_node->right do not have correct data
    It will point to memory.

    You have to allocate a memory area with malloc.

    + struct tree * new_node (int input)
    + {
    + struct tree * new = malloc (sizeof (struct tree));
    + new->value = input;
    + new->left = new->right = NULL;
    + return new;
    +}
    +
     // add node
    -int append (int input, struct tree * current_node) {
    + void append (int input, struct tree * current_node) {
         if (input<current_node->value) {
             if (current_node->left! = NULL) {
                 append (input, current_node->left);
             } else {
    --struct tree new = {input, NULL, NULL};
    --current_node->left =&new;
    + current_node->left = new_node (input);
             }
         } else {
             if (current_node->right! = NULL) {
                 append (input, current_node->right);
             } else {
    --struct tree new = {input, NULL, NULL};
    --current_node->right =&new;
    + current_node->right = new_node (input);
             }
         }
    + void freeAll (struct tree * p)
    + {
    + if (p->left) freeAll (p->left);
    + if (p->right) freeAll (p->right);
    + free (p);
    +}
     int main () {
         int sequence [] = {2, 4, 6, 1, 8, 2, 10, 0};
         int n = sizeof (sequence)/sizeof (int);
    --struct tree root = {
    --sequence [0],
    --NULL,
    --NULL
    -};
    + struct tree * root = new_node (sequence [0]);
         for (int i = 1;i<n;i ++) {
    --append (sequence [i],&root);
    + append (sequence [i], root);
         }
    --sort (&root);
    + sort (root);
         printf ("\ n");
    + freeAll (root);
     }

  • Answer # 2

    The struct tree root that can be entered with append is a local variable, and all the same variables (addresses) can be entered.
    Sorting it is totally nonsense.

    # Probably the problem with the question is due to it