starkk92
starkk92

Reputation: 5924

Creating node in Binary search function

typedef struct node
{
    int data;
    struct node *next,*prev;
}harsha;

void create_leaf(harsha **pnode,int value)
{
    harsha *temp=*pnode;
    if(*pnode==NULL)
    {
        *pnode=(struct node *)malloc(sizeof(struct node));
        if(!(*pnode))
        {
            printf("mem not allocated");
            exit(0);
        }
        (*pnode)->data=value;
        (*pnode)->prev=NULL;
        (*pnode)->next=NULL;
    }
    else
    {
        if(value>(temp->data))
        create_leaf(&(temp->next),value);
        else if(value<temp->data)
        create_leaf(&(temp->prev),value);
    }
}

This is function I have written for Inserting the new node into the binary search tree.Is there any problem with my code as my Inorder,Preorder and Postorder traversals are not working fine.

Upvotes: 0

Views: 72

Answers (1)

Ognjen Kocic
Ognjen Kocic

Reputation: 128

Put some printf to see are pointers matching. As I can tell from your output you have some pointers on 8 that are not null. :) Have you initialized pnode to null when sending &pnode to function? I think it's that. :)

Upvotes: 1

Related Questions