I have a pretty hefty program I'm trying to get together. I can't post ALL the code, but long story short the three methods I'm currently working on are a breadth-first traversal function, a recursive searching function, and another search function that calls the recursive search function for the binary tree. Below I will post the code for my depth-first traversal method:
CODE
template <class Type>
void BinarySearchTree<Type>::dft(void (*process) (const Type & item))
{
Stack<NodeP>stack;
NodeP node;
if (!empty())
{
stack.push(rootNode);
while(!stack.empty())
{
node=stack.pop();
process(node->data);
if(node->rightNode!=NULL)
{
stack.push(node->rightNode);
}
if(node->leftNode!=NULL)
{
stack.push(node->leftNode);
}
}
}
return;
}
I have been told that my above function is correct, but that my next function is close but slightly incorrect:
CODE
template <class Type>
void BinarySearchTree<Type>::bft(void (*process)(const Type & item))
{
Queue(NodeP)queue;
NodeP node;
if(!empty())
{
queue.enqueue(rootNode);
while(!queue.empty())
{
node=queue.dequeue();
process(node->data);
if(node->rightNode!=NULL)
{
queue.enqueue(node->rightNode);
}
if(node->leftNode!=NULL)
{
queue.enqueue(node->leftNode);
}
}
}
return;
}
This second method compiles, but I've been told the ordering is wrong. Any help is much appreciated.
Jake