简体   繁体   English

C中的BST程序

[英]BST program in C

Please help me with this. 请帮我解决一下这个。 I keep getting seg faults! 我不断出现段错误! I want to use recursion to create and insert a new node. 我想使用递归来创建和插入一个新节点。 Please help me debug this. 请帮我调试一下。

//Create a Binary Search Tree From an array.
struct Tree
{
        int data;
    struct Tree *lchild;
    struct Tree *rchild;
};
struct Tree *root = NULL;
struct Tree *node(int val)
{
    struct Tree *tempnode;
    tempnode = (struct Tree*)malloc(sizeof(struct Tree));
    tempnode->data = val;
    tempnode->rchild = NULL;
    tempnode->lchild = NULL;
    return tempnode;
}

void createTree(struct Tree *curr, int val)
{
    struct Tree *newnode = node(val);

    if (curr == NULL)
        curr = newnode;
    else if(val < curr->data)
    {
        createTree(curr->lchild,val);
    }
    else if(val > curr->data)
    {
        createTree(curr->rchild,val);

    }
    else
        printf("Error Similar data found\n");
}

void inorder(struct Tree *root)
{
    if (root->lchild != NULL)
        inorder(root->lchild);
    printf("[%d]    ",root->data);
    if (root->rchild != NULL)
        inorder(root->rchild);
}
int main()
{
//    root = NULL;
    int i = 0, arr[5] = {41,12,32,23,17};
    for(i;i<5;i++)
        createTree(root,arr[i]);
    inorder(root);
    return 0;
}

why do I keep getting seg fault. 为什么我会不断出现段错误。 Can someone explain me? 有人可以解释我吗? Am I doing something I should not? 我在做我不应该做的事情吗? Or am I missing at some point? 还是我在某个时候想念?

The root of the tree is not assigned anywhere; 树的根未分配到任何地方; in your function createTree you probably think that it is assigned in: 在函数createTree您可能认为它是在以下位置分配的:

if (curr == NULL)
    curr = newnode;

But curr is local to the function and does not affect root . 但是curr对函数而言是本地的,不会影响root You need to change the argument curr to be a pointer to pointer, otherwise the function does not work for assigning the root node, or child nodes. 您需要将参数curr更改为指向指针的指针,否则该功能不适用于分配根节点或子节点。 The root of the tree is not assigned anywhere; 树的根未分配到任何地方; in your function createTree you probably think that it is assigned in: 在函数createTree您可能认为它是在以下位置分配的:

if (curr == NULL)
    curr = newnode;

But curr is local to the function and does not affect root even if you gave it as the argument curr . 但是curr是函数的局部函数,即使您将其作为参数curr也不会影响root You need to change the argument curr to be a pointer to pointer, otherwise the function does not work for assigning the root node, or child nodes. 您需要将参数curr更改为指向指针的指针,否则该功能不适用于分配根节点或子节点。 That is, the function declaration becomes: 也就是说,函数声明变为:

void createTree(struct Tree **curr, int val)

Of course you must then change the use of curr inside the function accordingly (ie, the address pointed to is *curr where it used to be curr ), calls of the function need to pass the address, and not value, of the pointer (eg, createTree(&root, arr[i]) ). 当然,您必须相应地在函数内部更改curr的用法(即,指向的地址是*curr ,以前是curr ),函数的调用需要传递指针的地址,而不是值(例如, createTree(&root, arr[i]) )。

edit : Or, indeed, have the function return curr and always assign the return value to the relevant pointer at every place where you call createTree , thanks to @JonathanLeffler for the observation. 编辑 :或者,实际上,让函数返回curr并始终在您调用createTree每个位置将返回值分配给相关的指针,这要感谢@JonathanLeffler的观察。

Learn to use a debugger! 学习使用调试器!

Stepping through the main function, you would have seen that the value of root would have remained NULL after each call to createTree 逐步执行main函数,您将看到每次调用createTree之后, root的值将保持为NULL

The createTree function is not modifying the value of root , but only modifying its copy of the value of root . createTree函数不会修改root的值,而只会修改其root值的副本。

Your createTree function needs to take a struct Tree **curr , a pointer-to-a-pointer. 您的createTree函数需要采用struct Tree **curr ,即指向指针的指针。 This allows the function to modify the original value, not the local copy. 这允许函数修改原始值,而不是本地副本。

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

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