0
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.

2
  • 1
    What is the exact problem? It is very difficult to solve "not working fine" ... Commented Nov 26, 2012 at 10:22
  • void Inorder_traversal(harsha *pnode) { if(pnode!=NULL) { Inorder_traversal(pnode->prev); printf("%d\t",pnode->data); Inorder_traversal(pnode->next); } else return; } is my Inorder function and if the input is 5 4 8. it is printing 4 5 8 with some big numbers followed by and the program stopped working with out going to other functions postorder and Preorder Commented Nov 26, 2012 at 10:30

1 Answer 1

1

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. :)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.