1

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?

2 Answers 2

3

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);
Sign up to request clarification or add additional context in comments.

Comments

3

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);
}

1 Comment

indent+1 should be indent+2 to match the output.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.