求一数据结构题目答案!!!!!!!!!!!!!!!!!!!!!!!!!!

来源:百度知道 编辑:UC知道 时间:2024/05/30 13:20:27
请问怎样利用C++语言设计出对二叉树进行中序遍历的非第归算法!
因为小弟初学数据结构,希望高手能给出答案做参考.答的好的可以再+分.THANKS!

参考资料《数据结构与算法》,作者:许卓群等,里面涉及的具体函数的定义要自己添加~~
对二叉树进行中序遍历的非递归算法如下:
template<class T>
void BinaryTree<T>::InOrderWithoutRecurision(BinaryTreeNode<T> *root)
{
using std::stack;
stack< BinaryTreeNode<T> *> aStack;
BinaryTreeNode<T> *pointer = root;
while(!aStack.empty()||pointer)
{
if(pointer)
{
aStack.push(pointer);
pointer=pointer->leftchild();

}
else
{
pointer=aStack.top();
Visit(pointer->value());
pointer=pointer->rightchild();
aStack.pop();

}
}
}