Binary Tree operator overloading and recursion
Posted
by furious.snail
on Stack Overflow
See other posts from Stack Overflow
or by furious.snail
Published on 2010-05-19T07:28:27Z
Indexed on
2010/05/19
7:30 UTC
Read the original article
Hit count: 295
I was wondering how to overload the == operator for a binary tree to compare if two trees have identical data at same nodes. So far this is what I have:
bool TreeType::operator==(const TreeType& otherTree) const
{
if((root == NULL) && (otherTree.root == NULL))
return true; //two null trees are equal
else if((root != NULL) && (otherTree.root != NULL))
{
return((root->info == otherTree.root->info) &&
//this part doesn't actually do anything recursively...
//(root->left == otherTree.root->left) &&
//(root->right == otherTree.root->right))
}
else
return false; //one tree is null the other is not
}
I have a similar function that takes two TreeNode pointers as parameters but I've been stuck on how to convert it to this function.
© Stack Overflow or respective owner