0

I have the below code which I am using to implement BinarySearchTree. Somehow it doesn't build the binarytree.Can anybody help what is the issue ?

typedef struct BinarySearchTree

{

    struct BinarySearchTree *left;
    int nodeval;
    struct BinarySearchTree *right;

} 
BST;

BST *root=NULL;

void addrootnode(BST *,int );

void addnode(BST *,int );

void deletenode(int );

void printnodes();

main()

{

    int nodeval;
    char choice;
    printf("\n r->rootnode \n a->add \n d->delete \n p->print \n e->exit\n");
    while (1)
    {   
        printf("\nEnter your choice:");
        scanf("%c",&choice);
        switch (choice)
        {
        case 'r':
            printf("\nEnter the root node value to add: ");
            scanf("%d",&nodeval);
            addrootnode(root,nodeval);
            break;
        case 'a':
            printf("\nEnter the node value to add: ");
            scanf("%d",&nodeval);
            addnode(root,nodeval);
            break;
        case 'd':
            printf("\nEnter the node value to delete: ");
            scanf("%d",&nodeval);
            break;
        case 'p':
            //printf("\n");
            printnodes(root);
            break;
        case 'e':           
            exit(0);
        default:
            break;
        }
    }
    getchar();

}//end of main

//addrootnode

void addrootnode(BST *ptr,int num)

{

    if(ptr==NULL)
    {
        ptr=(BST *) malloc(sizeof(BST));
        ptr->left=NULL;
        ptr->nodeval=num;   
        ptr->right=NULL;        
        root=ptr;       
    }
}
//add node

void addnode(BST *ptr,int num)
{

    if(ptr==NULL)
    {
        ptr=(BST *) malloc(sizeof(BST));
        ptr->left=NULL;
        ptr->nodeval=num;   
        ptr->right=NULL;        


    }
    else if(num < ptr->nodeval )
    {       
        addnode(ptr->left,num);
    }
    else
    {

        addnode(ptr->right,num);
    }

}

//delete functionality

void deletenode(int nodeval)
{
}

//print all nodes

void printnodes(BST *ptr)

{

    if (ptr)

    {
        printnodes(ptr->left);
        printf("%d\t",ptr->nodeval);
        printnodes(ptr->right);
    }

}
2
  • Have you tried stepping through the code in a debugger? Do it, line by line, while checking variables and pointers. Commented Jan 6, 2013 at 6:46
  • You should create a separate function that creates and returns node, and another that adds nodes (as well as the root node). Thus, you will be able to avoid having this problem, as mentioned by CubeSchrauber. Commented Jan 6, 2013 at 6:50

1 Answer 1

1

You should not use BST *ptr as argument to addnode. You try to assign a value to it but at this time ptr is a local variable to the function addnode an does not alter the intended left or right pointer of the parent node.

Try to think about using BST **ptr instead.

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

2 Comments

is there a way I can implement the same without using a pointer to pointer.?
@krrishna You should create a separate function that creates and returns nodes, and another that adds nodes (as well as the root node).

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.