I don't understand what value I'm supposed to be checking against so that the function checks whether it's there or not. My question is what do i need to compare for it to evaluate true or false?
I get that it has to check whether a value is on the left or right and the value can't be a nullptr. why can't i use n->getLeftside() or n->getRightside() to check if the value is on respective sides?
template<class Object>
bool BinaryTree<Object>::isOnLeftSide(const Object& value)
{
bool is_ok = false;
if(root)
{
BinaryTreeNode<Object> *left_node = root->getLeftSide();
if (left_node) // if there is no left node the object cannot be found
{
if (left_node->getElement() == value) // check whether the object matches
is_ok = true;
else // if the object does not match it might be deeper in the tree
{
BinaryTree<Object> bt{left_node}; // this is a new tree with the left node as root
is_ok = bt.isOnLeftSide(value); // check the next level of the tree. This is recursion!
}
}
}
return is_ok;
}
I see the need of the nested if statements and why it works. although you lost me on BinaryTree<Object> bt{left_node};
I tried to find a way that is easier for me to understand and would work similarly, so i tried this is_ok = root->getRightSide()->getLeftSide();
but it fails at the last left node.
why does it take issue with the last left node but not the first left node yet is able to keep going right...only if the last node is not asserted?
A temporary binary tree is created that uses on the current node for recursive traversing. It requires that the tree does not delete the nodes when destroyed. Maybe not the best approach.
so i tried this
That will crash if getRightSide() returns null. The whole expression however checks only the existance of the left branch not whether the value in question exists. You need to compare the element.