简体   繁体   中英

C - recursion not working in a Binary Search Tree

I'm implementing a BST in C. If the tree only has 2 leaves and nothing in between, recursion works. But as soon as it goes deeper (eg below 3 levels deep) - the recursion breaks.

My code:

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

typedef struct node {
    int val;
    struct node* left;
    struct node* right;
} node_t;

void print_tree(node_t* n, int indent) {
    //base case, tree leaves
    if (n->left == NULL && n->right == NULL) {
        printf("%*c", indent, ' ');
        printf("%d\n", n->val);
    } else {
        //print current level
        printf("%*c", indent, ' ');
        printf("%d\n", n->val);
        //then kids
        print_tree(n->left, indent+2);
        print_tree(n->right, indent+1);
    }
}

int bst() {
    node_t* n1;
    n1 = (node_t*) malloc(sizeof(node_t));

    //level 1 children
    n1->val = 5;
    n1->left = (node_t*) malloc(sizeof(node_t));
    n1->right = (node_t*) malloc(sizeof(node_t));

    //level 2 children
    n1->left->val = 3;
    n1->left->left = (node_t*) malloc(sizeof(node_t));
    n1->left->right = NULL;

    n1->right->val = 10;
    n1->right->left = (node_t*) malloc(sizeof(node_t));
    n1->right->right = NULL;

    //level 3 children
    n1->left->left->val = 1;
    n1->left->left->left = NULL;
    n1->left->left->right = NULL;

    n1->right->left->val = 6;
    n1->right->left->left = NULL;
    n1->right->left->right = NULL;

    print_tree(n1, 0);

    return 1;
}

What happens:

 5
  3
    1

What I want to happen:

 5
  3
    1
  10
    6

When I run in debug mode I see that for some reason the base case never triggers and so at some point n itself becomes NULL and the if statement breaks.

Why is the base case not triggering?

First point :

In your function print_tree , you are dereferencing n without checking if it is NULL . This will cause trouble when there are nodes whose one child is NULL and the other child is not NULL .

You should add

if (n == NULL) return;

just after

void print_tree(node_t* n, int indent) {

Second point :

The line

        print_tree(n->right, indent+1);

is wrong and it will make the number of spaces of 10 and 6 fewer than expected.

It should be

        print_tree(n->right, indent+2);

Your recursion eventually reaches the point where it calls print_tree on the NULL pointer n1->left->right and you trigger a segmentation fault by trying to access n1->left->right->left in the first if statement of the function.

This might be more of what you want

void print_tree(node_t* n, int indent) {
    if(n == NULL) {
        return;
    }
    
    printf("%*c", indent, ' ');
    printf("%d\n", n->val);
    
    print_tree(n->left, indent+2);
    print_tree(n->right, indent+2);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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