Runtime errors with c++ binary search tree

N

Nick Kiguta

I have two classes, a Node class and a Bstree class. The Bstree class
contains a pointer to a Node class as one of its private members. Now I
need to build a binary search tree from multiple objects of the Node
class. I have a function addNode which is defined as follows

void Bstree::addNode ( Node **start, int val )
{
if( *start == NULL ) //if bst empty
{
*start = new Node ( val );
}
else //if bst not empty
{ //val is less than current nodes value
if( val < (*start)->getValue() )
{
Node * pLeft = ((*start)->getLeftPtr());
addNode ( &pLeft, val );
}

//val is greater than current nodes value
else if( val > (*start)->getValue() )
{
Node *pRight = ((*start)->getRightPtr());
addNode ( &pRight, val );
}

else{
cout <<"Duplicate data. IGNORED!!"<<endl;
}
}
}

When I call this function in my main program, I am calling it this way.

cout<<"Enter a number to add to the binary tree"<<endl;
cin >> num;
Node* temp = btree.getRoot();
btree.addNode ( &temp, num );[/highlight]

Function getRoot() returns the root pointer of type class Node which is
a private member of class Bstree. However, when I try to print the
tree, I get memory errors. Its as if the left or right pointers dont
have any values/as if they are null yet I am inputting values in them.
I have tried since this morning and still cant get it. This is my first
C++ assignment. Any help is greatly appreciated.
 
G

gpriv

My guess (without seeing details for Node class) you are not getting
updated left/right pointers back to Node structure. You allocating new
node for pLeft, but **start node is not getting this new value. Same
goes to pRight.
 
N

Nick Kiguta

The Node class's private members are
...//
private:
int value;
Node* left_ptr;
Node* right_ptr;
static int NumNodes;
};
The interface has set functions for all except NumNodes and has get
functions for all the variables (and a printNodeValue( ) ).
The get functions for the pointers are defined as follows;

Node* Node::getRightPtr( )
{
return right_ptr;
}

The constructor for the Node class sets value to user supplied entry
and sets the left and the right pointers to NULL right of the bat.
Is there a catch im falling for here such that the start node is not
getting new values? I dont understand why this isnt working. Any help
is appreciated. Thanks.
 
G

gpriv

Why you need Bstree class separate to Node?


void Node::Add(int newVal)
{
if (newVal > value)
{
if (right_ptr) right_ptr->Add(newVal);
else
right_ptr = new Node(newVal);
}
else if (newVal < value)
{
// Same for right ptr
}
else
{
// Duplicate
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top