Question

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);
}
Was it helpful?

Solution

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.

OTHER TIPS

Change void BST::insert(bst_node* root, int val) to void BST::insert(bst_node*& root, int val) or void BST::insert(bst_node** root, int val)

The easyest with no other modification to the function would be to use bst_node*&

The pointer itself is just an integer passed to the function, this pointer is then copied, so changing the value of the pointer doesn't is not changed outside the scope of the function. A reference to the pointer will make you're changes avaliable outside the functions scope.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top