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

  • SEARCH
  • Home
  • 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 8429527
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T05:21:50+00:00 2026-06-10T05:21:50+00:00

I am trying to write simple Binary Search tree class (BST). When I add

  • 0

I am trying to write simple Binary Search tree class (BST).

When I add a single node (eg.value = 10), the root gets updated and I verified using the VS C++ debugger at the end of BST::insert(…).

Then I tried to display (Case statement 3) the node(value -10) and nothing gets printed. The reason is that, when getRoot() is called, the root is NULL (??!!!)

Can someone please help me debug this issue.

#include <iostream>
#include <stdio.h>
#include "bst_node.h"

class BST{
private:
    bst_node *root;

public:
    bst_node* getRoot();
    void insert(bst_node *, int val);
    void deleteValue(int val);
    void display(bst_node *);
    void destroy();

    BST()
    {
        root = NULL;
        std::cout<<"BST constructor"<<"\n";
    }

    ~BST()
    {
        std::cout<<"BST destructor"<<"\n";
    }
};

int main()
{
    int item;
    int choice;
    BST tree1;
    while(1)
    {
        printf("\n Choices are:\n");
        printf("\n 1.Insertbeg\n\n 2.deleteNode\n\n 3.display\n\n 4.exit\n\n");
        printf(" Enter U'r choice: ");
        scanf("%d",&choice);
        switch(choice)
        {
            case 1: 
                {
                    printf("Enter element to be inserted: ");
                    scanf("%d",&item);
                    tree1.insert(tree1.getRoot(), item); 
                    break;
                }
            case 2:
                {
                    printf("Enter element to be deleted: ");
                    scanf("%d",&item);
//                  tree1.deleteValue(item); 
                    break;
                }
            case 3:
                {
                    tree1.display(tree1.getRoot()); 
                    break;
                }
            case 4: 
                    exit(1);
                    break;
            default: printf("INVALID CHOICE TRY AGAIN\n");
        }
    }
}

void BST::insert(bst_node* root, int val)
{
    if( root == NULL)
    {
        // add first element 
        bst_node *new_node = bst_node::create_empty_node();
        new_node->val = val;
        root = new_node;
    }

    else if(val < root->val )
    {
        insert(root->l_child, val); 
    }

    else if (val >= root->val)
    {
        insert(root->r_child, val);
    }
}

void BST::display(bst_node *root)
{
    if(root == NULL)
    {
        return;
    }
    else
    {
        std::cout << root->val <<"\n";
        display(root->l_child);
        display(root->r_child);
    }
}

void BST::deleteValue(int val)
{
    std::cout<< " Do nothing";
}

bst_node* BST::getRoot()
{
    return root;
}

bst_node.h


class bst_node
{
public:
    int val;
    bst_node *l_child;
    bst_node *r_child;
    static bst_node* create_empty_node();
};

bst_node* bst_node::create_empty_node()
{
    bst_node *new_node;
    new_node = new bst_node;
    new_node -> l_child = new_node -> r_child = NULL;
    return(new_node);
}
  • 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-10T05:21:51+00:00Added an answer on June 10, 2026 at 5:21 am

    You are hiding the private member root by naming the argument to insert() root as well. All modifications to root will apply on the local variable. For example the line:

    root = new_node;
    

    will have no affect. You are setting a new value to a vriable that is never used again.

    Doing so is usually a bad practice (with some exceptions, such as setters). Try to rename and check if it solves at least some of your issues.

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

Sidebar

Related Questions

I was just trying to write a simple binary search tree program where the
I am trying to write header file. I can write simple headers like add(int
I'm trying to write a simple unit containing a class TMainWindow to improve my
I am trying to write a simple audit class that takes input via operator
Possible Duplicate: Binary tree - Dereferencing pointers I was just trying to write a
I'm trying to save a simple (but quite big) Tree structure into a binary
I was trying to write a method which set values in a binary search
I'm trying to write simple proxy server for some purpose. In it I use
I trying to write a simple function to call unmanaged code from managed code.
I am trying to write some simple code which will read a text file

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.