二叉树的遍历
对于一个二叉树定义如下:
typedef char ElemType;
#define END '#'
typedef struct BtNode
{
BtNode *leftchild;
BtNode *rightchild;
ElemType data;
}BtNode, *BinaryTree;
我们需要实现以下的遍历:
1.前序遍历:
1.1递归
1.2非递归
2.中序遍历:
2.1递归
2.2非递归
3.后序遍历:
3.1递归
3.2非递归
4.层次遍历:
5.工字型遍历:
TO1.前序遍历(以“根左右”的形式遍历一遍二叉树)输出:A B C D E F G H
To1.1:递归
void PreOrder(BtNode *p)
{
if (p != NULL)
{
cout << p->data << " ";
PreOrder(p->leftchild);
PreOrder(p->rightchild);
}
}
To1.2:非递归
void NicePreOrder_2(BtNode *ptr)
{
if (NULL == ptr) return;
stack<BtNode *> st;
st.push(ptr);
while (!st.empty())
{
ptr = st.top(); st.pop();
cout << ptr->data << " ";
if (ptr->rightchild != NULL)
{
st.push(ptr->rightchild);
}
if (ptr->leftchild != NULL)
{
st.push(ptr->leftchild);
}
}
cout << endl;
}
void NicePreOrder(BtNode *ptr)
{
if (NULL == ptr) return;
stack<BtNode *> st;
while (ptr != NULL || !st.empty())
{
while (ptr != NULL)
{
cout << ptr->data << " ";
st.push(ptr);
ptr = ptr->leftchild;
}
ptr = st.top(); st.pop();
ptr = ptr->rightchild;
}
cout << endl;
}
TO2.中序遍历(以“左根右”的形式遍历一遍二叉树)输出:C B E D F A G H
To2.1:递归
void InOrder(BtNode *p)
{
if (p != NULL)
{
InOrder(p->leftchild);
cout << p->data << " ";
InOrder(p->rightchild);
}
}
To2.2:非递归
void NiceInOrder(BtNode * ptr)
{
if (NULL == ptr) return;
stack<BtNode *> st;
while (ptr != NULL || !st.empty())
{
while (ptr != NULL)
{
st.push(ptr);
ptr = ptr->leftchild;
}
ptr = st.top(); st.pop();
cout << ptr->data << " ";
ptr = ptr->rightchild;
}
cout << endl;
}
TO3.后序遍历(以“左右根”的形式遍历一遍二叉树)输出:C E F D B H G A
To3.1:递归
void PastOrder(BtNode *p)
{
if (p != NULL)
{
PastOrder(p->leftchild);
PastOrder(p->rightchild);
cout << p->data << " ";
}
}
To3.2:非递归
void NicePastOrder(BtNode * ptr)
{
if (NULL == ptr) return;
stack<BtNode *> st;
BtNode *tag = NULL;
while (ptr != NULL || !st.empty())
{
while (ptr != NULL)
{
st.push(ptr);
ptr = ptr->leftchild;
}
ptr = st.top(); st.pop();
if (ptr->rightchild == NULL || ptr->rightchild == tag)
{
cout << ptr->data << " ";
tag = ptr;
ptr = NULL;
}
else
{
st.push(ptr);
ptr = ptr->rightchild;
}
}
cout << endl;
}
TO4.层次遍历(一层一层的遍历一遍二叉树)输出:A B G C D H E F
void LevelOrder(BtNode *ptr)
{
if (NULL == ptr) return;
queue<BtNode *> qt;
qt.push(ptr);
while (!qt.empty())
{
ptr = qt.front(); qt.pop();
cout << ptr->data << " ";
if (ptr->leftchild != NULL)
{
qt.push(ptr->leftchild);
}
if (ptr->rightchild != NULL)
{
qt.push(ptr->rightchild);
}
}
cout << endl;
}
TO5.工字型遍历(以s型方式遍历一遍二叉树)输出:A G B C D H F E
void NiceGongOrder(BtNode *ptr)
{
if (NULL == ptr) return;
stack<BtNode*> st;
stack<BtNode*> qt;
int tag = 1;
qt.push(ptr);
while (!st.empty() || !qt.empty())
{
if (1 == tag)
{
while (!qt.empty())
{
ptr = qt.top(); qt.pop();
cout << ptr->data << " ";
if (ptr->leftchild != NULL)
{
st.push(ptr->leftchild);
}
if (ptr->rightchild != NULL)
{
st.push(ptr->rightchild);
}
}
tag = 0;
}
if (0 == tag)
{
while (!st.empty())
{
ptr = st.top(); st.pop();
cout << ptr->data << " ";
if (ptr->rightchild != NULL)
{
qt.push(ptr->rightchild);
}
if (ptr->leftchild != NULL)
{
qt.push(ptr->leftchild);
}
}
tag = 1;
}
}
cout << endl;
}