By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,286 Members | 2,364 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,286 IT Pros & Developers. It's quick & easy.

Runtime errors with c++ binary search tree

P: n/a
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.

Dec 9 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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.

Nick Kiguta wrote:
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.


Dec 10 '05 #2

P: n/a
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.

Dec 10 '05 #3

P: n/a
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
}
}

Dec 11 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.