简体   繁体   中英

Inserting in Binary Search Tree (C) using for loop

I'm having trouble inserting in a Binary Search Tree using for loop, when I call the InorderTraversal function, there is no output all I get is a blank line, as far as I think rest of the code is okay the only problem is in the insert function.

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

typedef struct BinaryTree{

    int data;
    struct BinaryTree *left;
    struct BinaryTree *right;
} node;

node* Insert(node* head, int value)
{
    _Bool flag = true;

    for(node *temp = head; flag == true; (temp = (value >= temp->data)?(temp->right):(temp->left)))
    {
        if(temp == NULL)
        {
            temp = (node*)malloc(sizeof(node*));
            temp->data = value;
            temp->left = NULL;
            temp->right = NULL;
            flag = false;
        }
    }

    return head;
}

void InorderTraversal(node* head)
{
    if(head == NULL)
    {
        return;
    }

    InorderTraversal(head->left);
    printf("%d ",head->data);
    InorderTraversal(head->right);
}

int main(void)
{
    node *head = NULL;

    for(int i = 0; i < 40; i++)
    {
        head = Insert(head,i);
    }

    InorderTraversal(head);

    return 0;
}

Call me crazy, but shouldn't that malloc(sizeof(node*)) be malloc(sizeof node) ?
I am not that so informed, other than being able to read C, so excuse me if this is simply wrong...

Edit: ... or malloc(sizeof * temp)

When you insert the first node you dereference an uninitialized pointer here:

temp->data

Where temp is head and head in uninitialized and pointing to NULL.

So you first have to make special case when head is NULL:

if( !head )
{
    head = malloc(sizeof(node));
    head->data = value;
    head->left = NULL;
    head->right = NULL;

    return head ;
}

When you continue adding elements you don't update the pointer of the last node. Your for loop should have an extra pointer to the previous node and when you get to the last node and find NULL update the previous nodes left or right pointer.

if(temp == NULL)    //wrong, should be: not equal
{
    temp = (node*)malloc(sizeof(node*));   //wrong, should be: sizeof the node not the pointer
    temp->data = value;
    temp->left = NULL;
    temp->right = NULL;
    flag = false;    //use break instead
}

here the previous node pointer left or right is not updated and when you search you can't find any node.

Here try these changes in your Insert function

node* Insert(node *head, int value)
{

    if(!head)                              //Explicitly insert into head if it is NULL
    {
        head = malloc(sizeof *head);
        head->data = value;
        head->left = NULL;
        head->right = NULL;
        return head;
    }

    for(node *temp = head,*temp2 = head; ;(temp = (value >= temp->data)?(temp->right):(temp->left)))
    {
        if(temp == NULL)
        {
            temp = malloc(sizeof *temp);
            temp->data = value;
            temp->left = NULL;
            temp->right = NULL;

            if(value >= temp2->data)  //update previous nodes left or right pointer accordingly 
                temp2->right = temp;
            else
                temp2->left = temp;

            break;
        }

        temp2 = temp;      //Use a another pointer to store previous value of node
    }

    return head;
}

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