中序遍历的递归算法和非递归算法。
template <class T>
void recitraverse(struct node<T>* Tree)
{
if(Tree == NULL) return;
itraverse(Tree->left);
visitnode(Tree);
itraverse(Tree->right);
}
中序遍历的非递归算法visit节点时与前序不同。
template <class T>
void itraverse(struct node<T>*tree)
{
if(tree == NULL) return;
MyStack<struct node<T> *> treestack;
treestack.init(20);
while(tree != NULL|| treestack.gettop()!=0)
{
if(tree!=NULL)
{
treestack.push(tree);
tree = tree->left;
}
else
{
tree = treestack.pop();
visitnode(tree);
tree = tree->right;
}
}
}