Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9091527
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T22:37:54+00:00 2026-06-16T22:37:54+00:00

I have the below code which I am using to implement BinarySearchTree. Somehow it

  • 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);
    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T22:37:56+00:00Added an answer on June 16, 2026 at 10:37 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a working JavaScript code below which dynamically creates JSON object using JSON.parse
I have below code which overrides equals() and hashcode() methods. public boolean equals(Object obj)
I have below snippet of code in which TestClass is extending jPanel which is
I want to know is below code correct ? I have following code which
I have the code below which I use clone() and live() . The code
I have the code below which I use clone() and delegate() . The code
I have the below code, which iterates over a list based on a custom
I have the below code for my a Dialog box for a which contains
I have the following code below which works, but I want to insert values
Consider the code below (which has been simplified). I have a service class that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.