Original Post
This binary tree is kicking my butt to hell and back. I'm trying to write a find routine that returns the data associated with the node in a pointer, so that I can modify the data without reinserting it into the tree (is this the way to go?). The problem is, I just can't seem to win. If the return type is a pointer, than thats fine if I'm using a pointer but blows up when I try to use just uhh.. non-pointers. But if I use pointers, than the search type is just wrong because its searching for a pointer and not a value. Heres my search functions: The second find is what the first find uses to find the node, so that the first find can return the data. But both of these break on me: Trying to .Find(5) doesn't work, because 5 isn't a pointer, its a static value and the template type (T) is declared as int *. Then this one: That one doesn't work because its trying to convert an int to an int *, because T is declared as an int and not an int *. So how can I make it do what I want to do? I can't figure it out, I've been playing with it for the last few hours. The only thing I can think of is to convert it to a map, and use a Key, but I want to make that as a seperate structure at a later time. Right now I just want this tree to work.
T *Find(T Search)
{
CBinaryTreeNode<T> *pNode = Find(m_pRoot, Search);
if(pNode == NULL)
return NULL;
else
return pNode->GetData();
}
CBinaryTreeNode<T> *Find(CBinaryTreeNode<T> *pNode, T Search)
{
if(pNode == NULL)
return NULL;
else if(pNode->GetData() == Search)
return pNode;
else if(pNode->GetData() < Search)
return Find(pNode->m_pLeft, Search);
else
return Find(pNode->m_pRight, Search);
}
CBinaryTree<int *> PointerTree;
PointerTree.Add(new int(5));
PointerTree.Add(new int(6));
PointerTree.Add(new int(7));
int *pInteger = PointerTree.Find(5);
CBinaryTree<int> IntegerTree;
IntegerTree.Add(7);
IntegerTree.Add(13);
IntegerTree.Add(666);
int *pInteger = IntegerTree.Find(7);