void PreOrderBinaryTree_Nor()
{
if (NULL == _pRoot)
return;
cout << "PreOrderBinaryTree_Nor:" << endl;
stack<BinaryTreeNode<T>*> s;
s.push(_pRoot);
while (!s.empty())
{
BinaryTreeNode<T>* pCur = s.top();
cout << pCur->_data << " ";
s.pop();
if (NULL != pCur->_pRightChild)
s.push(pCur->_pRightChild);
if (NULL != pCur->_pLeftChild)
s.push(pCur->_pLeftChild);
}
cout << endl;
}
void InOrderBinaryTree_Nor()
{
if (NULL == _pRoot)
return;
cout << "InOrderBinaryTree_Nor:" << endl;
stack<BinaryTreeNode<T>*> s;
BinaryTreeNode<T>* pCur = _pRoot;
while (pCur || !s.empty())
{
while(pCur)
{
s.push(pCur);
pCur = pCur->_pLeftChild;
}
BinaryTreeNode<T>* pTop = s.top();
cout << pTop->_data << " ";
s.pop();
pCur = pTop->_pRightChild;
}
cout << endl;
}
void PostOrderBinaryTree_Nor()
{
if (NULL == _pRoot)
return;
cout << "PostOrderBinaryTree_Nor:" << endl;
stack<BinaryTreeNode<T>*> s;
BinaryTreeNode<T>* pCur = _pRoot;
BinaryTreeNode<T>* prev = NULL;
while (pCur || !s.empty())
{
while (pCur)
{
s.push(pCur);
pCur = pCur->_pLeftChild;
}
BinaryTreeNode<T>* pTop;
pTop = s.top();
if (NULL == pTop->_pRightChild || pTop->_pRightChild == prev)
{
cout << pTop->_data<<" ";
prev = pTop;
s.pop();
}
else
{
pCur = pTop->_pRightChild;
}
}
cout << endl;
}
void LevelOrderBinaryTree()
{
if (NULL == _pRoot)
return;
cout << "LevelOrderBinaryTree:" << endl;
queue<BinaryTreeNode<T>*> q;
q.push(_pRoot);
while (!q.empty())
{
BinaryTreeNode<T>* pCur = q.front();
cout << pCur->_data << " ";
if (NULL != pCur->_pLeftChild)
q.push(pCur->_pLeftChild);
if (NULL != pCur->_pRightChild)
q.push(pCur->_pRightChild);
q.pop();
}
cout << endl;
}