简体   繁体   English

C - 递归在二叉搜索树中不起作用

[英]C - recursion not working in a Binary Search Tree

I'm implementing a BST in C.我正在 C 中实施 BST。 If the tree only has 2 leaves and nothing in between, recursion works.如果树只有 2 片叶子,中间什么都没有,递归就可以了。 But as soon as it goes deeper (eg below 3 levels deep) - the recursion breaks.但是一旦它变得更深(例如深度低于 3 级) - 递归就会中断。

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.当我在调试模式下运行时,我看到由于某种原因基本情况永远不会触发,因此在某些时候n本身变成NULL并且 if 语句中断。

Why is the base case not triggering?为什么基本情况没有触发?

First point :第一点

In your function print_tree , you are dereferencing n without checking if it is NULL .在您的 function print_tree中,您取消引用n而不检查它是否是NULL This will cause trouble when there are nodes whose one child is NULL and the other child is not NULL .当有节点的一个子节点是NULL而另一个子节点不是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.是错误的,它会使106的空格数比预期的少。

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.您的递归最终到达它在NULL指针n1->left->right上调用print_tree的点,并且您通过尝试在 function 的第一个if语句中访问n1->left->right->left来触发分段错误。

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

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

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