题目:
用递归和非递归方式,分别按照二叉树先序、中序和后序打印所有的节点。我们约定:先序遍历顺序为根、左、右;中序遍历顺序为左、根、右;后序遍历顺序为左、右、根。
程序测试的二叉树结构如下图所示:
这个结构的二叉树,程序的输入:AB#D##C#E##
进行输入,采用先序建树!
递归的三种遍历的源码如下:
#include<iostream>
using namespace std;
typedef struct BiTNode
{
char data;
struct BiTNode* lchild;
struct BiTNode* rchild;
}BiTNode, *BiTree;
void createBiTree(BiTree &T)
{
char c;
cin>>c;
if('#' == c)
T = NULL;
else
{
T = new BiTNode;
T->data = c;
createBiTree(T->lchild);
createBiTree(T->rchild);
}
}
void preOrderRecur(BiTree T)
{
if(T != NULL)
{
cout<<T->data<<" ";
preOrderRecur(T->lchild);
preOrderRecur(T->rchild);
}
}
void inOrderRecur(BiTree T)
{
if(T != NULL)
{
inOrderRecur(T->lchild);
cout<<T->data<<" ";
inOrderRecur(T->rchild);
}
}
void postOrderRecur(BiTree T)
{
if(T != NULL)
{
postOrderRecur(T->lchild);
postOrderRecur(T->rchild);
cout<<T->data<<" ";
}
}
int main()
{
BiTree T;
createBiTree(T);
cout<<"pre-order: ";
preOrderRecur(T);
cout<<endl;
cout<<"in-order: ";
inOrderRecur(T);
cout<<endl;
cout<<"post-order: ";
postOrderRecur(T);
cout<<endl;
return 0;
}
下面的源码是三种遍历的非递归算法:
#include<iostream>
#include<stack>
using namespace std;
typedef struct BiTNode
{
char data;
struct BiTNode* lchild;
struct BiTNode* rchild;
}BiTNode, *BiTree;
void createBiTree(BiTree &T)
{
char c;
cin>>c;
if('#' == c)
T = NULL;
else
{
T = new BiTNode;
T->data = c;
createBiTree(T->lchild);
createBiTree(T->rchild);
}
}
void preOrderUnRecur(BiTree T)
{
BiTree p = T;
cout<<"pre-order: ";
if(p != NULL)
{
stack<BiTree> st;
st.push(p);
while(st.empty() != true)
{
p = st.top();
st.pop();
cout<<p->data<<" ";
if(p->rchild != NULL)
st.push(p->rchild);
if(p->lchild != NULL)
st.push(p->lchild);
}
}
cout<<endl;
}
void inOrderUnRecur(BiTree T)
{
BiTree p = T;
cout<<"in-order: ";
if(p != NULL)
{
stack<BiTree> st;
while(st.empty() != true || p != NULL)
{
if(p != NULL)
{
st.push(p);
p = p->lchild;
}
else
{
p = st.top();
st.pop();
cout<<p->data<<" ";
p = p->rchild;
}
}
}
cout<<endl;
}
//非递归后序遍历的第一种方法
void postOrderUnRecur1(BiTree T)
{
BiTree p = T;
cout<<"post-order 1: ";
if(p != NULL)
{
stack<BiTree> st1, st2;
st1.push(p);
while(st1.empty() != true)
{
p = st1.top();
st1.pop();
st2.push(p);
if(p->lchild != NULL)
st1.push(p->lchild);
if(p->rchild != NULL)
st1.push(p->rchild);
}
while(st2.empty() != true)
{
BiTree q;
q = st2.top();
st2.pop();
cout<<q->data<<" ";
}
}
cout<<endl;
}
//非递归后序遍历的第二种方法
void postOrderUnRecur2(BiTree T)
{
BiTree p = T;
cout<<"post-order 2: ";
if(p != NULL)
{
stack<BiTree> st;
st.push(p);
BiTree pre = NULL;
while(st.empty() != true)
{
pre = st.top();
if(pre->lchild != NULL && p != pre->lchild && p != pre->rchild)
st.push(pre->lchild);
else if(pre->rchild != NULL && p != pre->rchild)
st.push(pre->rchild);
else
{
BiTree output= st.top();
st.pop();
cout<<output->data<<" ";
p = pre;
}
}
}
cout<<endl;
}
int main()
{
BiTree T;
createBiTree(T);
preOrderUnRecur(T);
inOrderUnRecur(T);
postOrderUnRecur1(T);
postOrderUnRecur2(T);
return 0;
}
再补充一个非递归的层序遍历,源码如下:
#include<iostream>
#include<queue>
using namespace std;
typedef struct BiTNode
{
char data;
struct BiTNode* lchild;
struct BiTNode* rchild;
}BiTNode, *BiTree;
void createBiTree(BiTree &T)
{
char c;
cin>>c;
if('#' == c)
T = NULL;
else
{
T = new BiTNode;
T->data = c;
createBiTree(T->lchild);
createBiTree(T->rchild);
}
}
void levelOrder(BiTree T)
{
BiTree p = T;
cout<<"level-order: ";
queue<BiTree> que;
que.push(p);
while(que.empty() != true)
{
p = que.front();
que.pop();
cout<<p->data<<" ";
if(p->lchild != NULL)
que.push(p->lchild);
if(p->rchild != NULL)
que.push(p->rchild);
}
cout<<endl;
}
int main()
{
BiTree T;
createBiTree(T);
levelOrder(T);
return 0;
}