After looking at this for a while longer, there is another problem that may be part of the recursion problem.
I amm adding nodes to the tree with the following calls:
1 2
|
bst.add( &Node( &HashedString( "firstHash", "firstPlain" ) ) );
bst.add( &Node( &HashedString( "HASH", "plain" ) ) );
|
Each HashedString object can print itself, defined as:
1 2 3 4
|
void HashedString::print()
{
cout << "Plain: " << _plain << ", Hash: " << _hash << "\n";
}
|
When I construct the HashedString objects with the above add calls, the constructor is defined as such:
1 2 3 4 5
|
HashedString::HashedString( string hash, string plain )
{
_hash = hash;
_plain = plain;
}
|
What I don't understand, is when I print the HashedString objects using its print() method, I sometimes get output like this:
Plain: , Hash: firstHash
with the plain string empty. Sometimes both string are displayed properly... What could be causing this? Thanks...
Edit: Here is my bst.add() function, since I believe this to be the problem...
(Note this is called from bst.add( ... ) with _root as a second param)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
void BST::add( Node* newNode, Node* check )
{
//clean this garbage up please
int comparison = newNode->_hs->getHash().compare( check->_hs->getHash() );
cout << "COMP " << comparison << "\n";
cout << "--------\nComparison Nodes: \n";
newNode->_hs->print();
check->_hs->print();
cout << "--------\n";
if ( comparison > 0 && check->right == NULL )
check->right = newNode;
else if ( comparison > 0 )
add( newNode, check->right );
if ( comparison <= 0 && check->left == NULL )
check->left = newNode;
else if ( comparison <= 0 )
add( newNode, check->left );
}
|
Edit: The garbage comment was for me.