PS:看到网上N多都是有tag域的 这个暂时没发现什么bug 所以恳请哪位大神发现了帮忙指正一下!!3Q啦!!
template<class T>
void BSTree<T>::PostorderTraversal( ostream &os ){
BSTNode<T> *p = NULL;
stack<BSTNode<T> *> st;
st.push( _root );
while( !(st.empty()) )
{
while( (p = st.top()) != NULL ) st.push( p->lchild );
// 空指针退栈.
st.pop();
if( !st.empty() )
{
//
// Rightwards one step.
p = st.top();
st.push( p->rchild );
//
// Output if satisfied the conditions.
if( st.top() == NULL ){
// 空指针退栈.
st.pop();
p = st.top();
st.pop();
os << p->_elem << " ";
}
if( !st.empty() ){
// 若是右子树出来的,一直退栈.
for( BSTNode<T> *pParent = st.top(); !st.empty() && p == pParent->rchild; ){
p = st.top();
st.pop();
os << p->_elem << " ";
if( !st.empty() )
pParent = st.top();
}
}
// 若输出后,则必须继续向右走一步.
if( !st.empty() && p == st.top()->lchild ){
p = st.top();
st.push( p->rchild );
}
}
}
}
890

被折叠的 条评论
为什么被折叠?



