数据结构中先序遍历二叉树的完整C++代码

有好的发邮箱[email protected]
谢谢

template<class T>
void BinaryTree<T>::PreOrderWithoutRecursion(BinaryTreeNode<T> *root) {
using std::stack; // 使用STL中的栈
stack<BinaryTreeNode<T>* > aStack;
BinaryTreeNode<T> *pointer = root;
aStack.push(pointer);
while (!aStack.empty()) {
pointer=aStack.top(); // 获得栈顶元素
aStack.pop(); // 栈顶元素退栈
Visit(pointer->value()); // 访问当前结点
if (pointer->rightchild() != NULL) // 非空右孩子入栈
aStack.push(pointer->rightchild());
if (pointer->leftchild() != NULL) // 非空左孩子入栈
aStack.push(pointer->leftchild ()) ;
}
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-01-01
邮箱!!! 我给你

邮件已经发送 注意查收
相似回答