I'm trying to implement the non recursive traversal in a binary tree. I am having trouble getting started with my stack, and have no idea where to begin here. I have written a stack to hold int's before, but never to hold nodes(or whatever it needs to hold...pointers?) Here is the structure I need to stack:
struct TNode {
int datum;
TNode* leftPtr;
TNode* rightPtr;
}; // TNode
// ===========
Here is my inorder code:
void BinaryTree::inOrderNonRec(TNode* nodePtr)
{
while((nodePtr != NULL)||(!stack.IsEmpty( )))
if(nodePtr != NULL)
{
stack.push(nodePtr);
nodePtr = nodePtr->leftPtr;
}
else
{
nodePtr = stack.top( );
stack.pop( );
cout << nodePtr->datum << " ";
nodePtr = nodePtr->rightPtr;
}
cout << endl;
}
Any help would be appreciated.