简体   繁体   English

二进制搜索树中的插入失败

[英]Insertion failure in a Binary Search Tree

Here is my code for the creation and insertion of the binary search tree 这是我用于创建和插入二进制搜索树的代码

struct BTNode
{
    int info;
    struct BTNode *left, *right;
};

struct BTNode* create(int data)
{
    struct BTNode *root;
    struct BTNode *n = malloc(sizeof(struct BTNode));

    n->info = data;
    n->left = NULL;
    n->right = NULL;
    root = n;

    return(root);
}

struct BTNode* insert(struct BTNode *root, int data)
{
    struct BTNode *ptr;
    struct BTNode *n = malloc(sizeof(struct BTNode));
    if (n == NULL)
        printf("\nOUT OF MEMORY!\n");

    n->info = data;
    n->left = NULL;
    n->right = NULL;


    ptr = root;
    while (ptr != NULL){
        if (data < ptr->info){
            if (ptr->left == NULL)
            ptr->left = n;

            ptr = ptr->left;
        }
        else if (data > ptr->info){
            if (ptr->right == NULL)
            ptr->right = n;

            ptr = ptr->right;
        }
    }

    return(n);
}

and here is the main() function 这是main()函数

int main()
{
    struct BTNode *root = NULL;
    int choice, data;

    printf("\nWrite the root data: ");
    scanf("%d", &data);
    root = create(data);

    while (1){
    printf("\n1.Insert 2.Preorder 3.Exit\n");
    scanf("%d", &choice);
    switch(choice){
        case 1:
        printf("\nWrite the data: ");
        scanf("%d", &data);
        insert(root, data);
        break;

I am able to create the root node but whenever I am trying to insert the data, I give my data and the compiler stops doing anything. 我能够创建根节点,但是每当我尝试插入数据时,我都会给我数据,而编译器将停止执行任何操作。 Any idea why this is happening? 知道为什么会这样吗?

Your while() loop goes on forever because you keep going even after you find a place to insert the node: while()循环将永远持续下去,因为即使在找到插入节点的位置之后,您仍会继续循环:

while(ptr!=NULL){
    if(data<ptr->info){
        if(ptr->left==NULL)
        ptr->left=n;

        ptr=ptr->left;
    }
    else if(data>ptr->info){
        if(ptr->right==NULL)
        ptr->right=n;

        ptr=ptr->right;
    }
}

You need to break out of the while() loop after inserting the node: 插入节点后,您需要打破while()循环:

while (ptr != NULL) {
    if (data < ptr->info) {
        if (ptr->left == NULL) {
            ptr->left = n;
            break;
        }


        ptr = ptr->left;
    } else if (data > ptr->info) {
        if (ptr->right == NULL) {
            ptr->right = n;
            break;
        }


        ptr = ptr->right;
    }
}

Also, bonus points for checking if malloc() fails 另外,用于检查malloc()失败的奖励积分

struct BTNode *n = malloc(sizeof(struct BTNode));
if (n == NULL)
    printf("\nOUT OF MEMORY!\n");

But negative points for simply continuing on anyway, you should exit the function if malloc() fails 但是无论如何简单地继续运行,负点是,如果malloc()失败,则应该退出该函数

struct BTNode *n = malloc(sizeof(struct BTNode));
if (n == NULL) {
    printf("\nOUT OF MEMORY!\n");
    return NULL:
}

And then of course, the code calling insert() should know what to do if insert() returns NULL. 然后,当然,调用insert()的代码应该知道如果insert()返回NULL时该怎么做。

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

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