I have a class implementing binary search tree and one of my private methods is method bool find(Node<Key, Info> * &node, Key _key);, where node stands for a pointer to a node, we start searching from and _key stands for a unique for every node key.
My method is implemented as follows:
template<typename Key, typename Info>
bool BST<Key, Info>::find(Node<Key, Info>* &node, Key _key)
{
if (node)
{
if (node->key == _key)
{
return true;
}
else
{
find(node->left, _key);
find(node->right, _key);
return false;
}
}
else return false;
}
And it doesn't return true, even if the element with the given key exists. I added a printing command just before return statement and it executes so my function seems to find the given node, but I guess my understanding is wrong and it still somehow returns false.
Solved
The solution to my problem seems to be found :)
template<typename Key, typename Info>
bool BST<Key, Info>::find(Node<Key, Info>* &node, Key _key)
{
if (node)
{
if (node->key == _key)
{
return true;
}
else if(_key<node->key)
return find(node->left, _key);
else
return find(node->right, _key);
}
else return false;
}
findin yourelsebranch? Try to follow your code logically on some simple example.