二叉树的构建及遍历
先序遍历:1->2->3->4->5->6->7->8
中序遍历:3->2->5->4->1->6->8->7
后序遍历:3->5->4->2->8->7->6->1
层次遍历:1->2->6->3->4->7->5->8
/** ==测试用例==
* 先序输入:1 2 3 0 0 4 5 0 0 0 6 0 7 8 0 0 0
* 中序输入:15 0 3 0 2 5 4 0 1 0 0 0 6 8 7 0
* 层次输入;1 2 6 3 4 0 7 0 0 5 0 8 0 0 0 0 0
*/
/*
*本次实验我需要完成的任务
*建树:先序递归,中序递归,层次非递归
*遍历:先序递归,中序递归,后序递归,层次非递归,中序非递归
*/
#include <iostream>
#include <queue>
#include <stack>
using namespace std;
typedef int TElemType;
//============二叉链表存储结构=================
typedef struct BiTNode
{
TElemType data;
struct BiTNode *lchild, *rchild;
}BiTNode, *BiTree;
//================初始化二叉树=====================
void initBiTree(BiTree &T)
{
T = NULL;
}
//================销毁二叉树========================
void destoryBiTree(BiTree &T)
{
if(T)
{
if(T->lchild)//有左子树
destoryBiTree(T->lchild);//销毁左子树
if(T->rchild)
destoryBiTree(T->rchild);
free(T);//释放根节点
T = NULL;//空指针赋NULL
}
}
//==================递归建树=========================
//XXXXXXXXXXXXXX递归建树之先序建树XXXXXXXXXXXXXXXXXXX
void preCreateBiTree(BiTree &T)
{//先序建树
//输入0表示空树
TElemType ch;
cin>>ch;
if(!ch)
{
T = NULL;
}
else
{
T = new BiTNode;
if(!T)
{
cout<<"内存申请失败"<<endl;
exit(0);
}
T->data = ch;
preCreateBiTree(T->lchild);
preCreateBiTree(T->rchild);
}
}
void FInCreateBiTree(BiTree &T, int cc[], int low, int high)
{//中序递归建树
if(low <= high)
{
int mid = (low + high) / 2;
if(cc[mid])
{
T = new BiTNode;
T->data = cc[mid];
T->lchild = T->rchild = NULL;
FInCreateBiTree(T->lchild, cc, low, mid - 1);
FInCreateBiTree(T->rchild, cc, mid + 1, high);
}
}
}
//==================递归建树=========================
//===================非递归建树======================
//XXXXXXXXXXXXXXX非递归之层次建树XXXXXXXXXXXXXXXXXXXX
void LevelCreateBiTree(BiTree &T)
{//层次建树
TElemType cl, cr;
BiTree pp;
queue<BiTree> Q;
cin>>cl;
if(!cl)
{//建立了空树
T= NULL;
return;
}
T = new BiTNode;
T->data = cl;
Q.push(T);
while(!Q.empty())
{
pp = Q.front();
Q.pop();
cin>>cl>>cr;
if(cl)
{
pp->lchild = new BiTNode;
pp->lchild->data = cl;
Q.push(pp->lchild);
}
else
pp->lchild = NULL;
if(cr)
{
pp->rchild = new BiTNode;
pp->rchild->data = cr;
Q.push(pp->rchild);
}
else
pp->rchild = NULL;
}
}
//==============此函数用于输出节点========================
void Visit(TElemType e)
{
cout<<e<<" ";
}
//=============递归遍历======================================
//XXXXXXXXXXXXXXXXXXXX递归遍历之先序遍历XXXXXXXXXXXXXXXXXXXXX
void PreOrderTraverse(BiTree T, void (*Visit)(TElemType))
{//先序遍历递归算法
if(T)
{
Visit(T->data);//1.先遍历根结点
PreOrderTraverse(T->lchild, Visit);//2.遍历左子树
PreOrderTraverse(T->rchild, Visit);//3.遍历右子树
}
}
//XXXXXXXXXXXXXXXXXXXXXX递归遍历之中序遍历XXXXXXXXXXXXXXXXXXXX
void InOrderTraverse(BiTree T, void (*Visit) (TElemType))
{//中序遍历递归算法
if(T)
{
InOrderTraverse(T->lchild, Visit);
Visit(T->data);
InOrderTraverse(T->rchild, Visit);
}
}
//XXXXXXXXXXXXXXXXXXXXXXX递归遍历之后序遍历XXXXXXXXXXXXXXXXXXX
void PostOrderTraverse(BiTree T, void (*Visit) (TElemType))
{//后序遍历递归算法
if(T)
{
PostOrderTraverse(T->lchild, Visit);
PostOrderTraverse(T->rchild, Visit);
Visit(T->data);
}
}
//================递归遍历====================================
//===================非递归遍历===============================
//XXXXXXXXXXXXXXXXXX非递归遍历之层次遍历XXXXXXXXXXXXXXXXX
void LevelOrderTraverse(BiTree T, void(*Visit) (TElemType))
{//层次遍历
queue<BiTree> Q;
BiTree ptr;
Q.push(T);
while(!Q.empty())
{
ptr = Q.front();
Q.pop();
Visit(ptr->data);
if(ptr->lchild)
Q.push(ptr->lchild);
if(ptr->rchild)
Q.push(ptr->rchild);
}
}
//XXXXXXXXXXXXXXXXXXXXXX非递归遍历之中序遍历XXXXXXXXXXXXX
void FInOrderTraverse(BiTree T)
{//非递归中序遍历
stack<BiTree> S;
BiTree pp = T;
while(pp || !S.empty())
{
if(pp)
{
S.push(pp);
pp = pp->lchild;
}
else
{
pp = S.top();
S.pop();
Visit(pp->data);
pp = pp->rchild;
}
}
}
void menu_create()
{
cout<<"---------------------===请选择建树方式===-------------------"<<endl;
cout<<" 1.先序递归建树"<<endl;
cout<<" 2.中序递归建树"<<endl;
cout<<" 3.层次非递归建树"<<endl;
}
void menu_traverse()
{
cout<<"---------------------===请选择比遍历方式===-------------------"<<endl;
cout<<" 1.先序递归遍历"<<endl;
cout<<" 2.中序递归遍历"<<endl;
cout<<" 3.后序递归遍历"<<endl;
cout<<" 4.中序非递归遍历"<<endl;
cout<<" 5.层次非递归遍历"<<endl;
}
void menu()
{
cout<<"****************本程序主菜单********************"<<endl;
cout<<"* *"<<endl;
cout<<"* 1.建树 *"<<endl;
cout<<"* 2.遍历 *"<<endl;
cout<<"* 3.销毁二叉树 *"<<endl;
cout<<"* 4.退出 *"<<endl;
cout<<"* *"<<endl;
cout<<"************************************************"<<endl;
}
void solve_create(BiTree &T)
{
int tt,n;
int *cc;
menu_create();
cin>>tt;
switch(tt)
{
case 1:
{
cout<<"输入先序构树序列"<<endl;
preCreateBiTree(T);
cout<<"先序构树成功"<<endl;
break;
}
case 2:
{
cout<<"输入满二叉的所有节点数:"<<endl;
cin>>n;
cc = new int[n];
if(!cc){cout<<"申请动态空间失败"<<endl;exit(0);}
cout<<"现在可以输入"<<n<<"个节点了"<<endl;
for(int i = 0; i < n; i++)
cin>>cc[i];
FInCreateBiTree(T,cc, 0, n);
cout<<"中序构树成功"<<endl;
break;
}
case 3:
{
cout<<"输入层次构树序列"<<endl;
LevelCreateBiTree(T);
cout<<"层次构树成功"<<endl;
break;
}
default:cout<<"选项错误,本次操作失败"<<endl;
}
}
void solve_traverse(BiTree T)
{
int n;
menu_traverse();
cin>>n;
switch(n)
{
case 1:PreOrderTraverse(T, Visit);break;
case 2:InOrderTraverse(T, Visit);break;
case 3:PostOrderTraverse(T, Visit);break;
case 4:FInOrderTraverse(T);break;
case 5:LevelOrderTraverse(T,Visit);break;
default :cout<<"输入无此选项,本次操作失败"<<endl;
}
cout<<endl;
}
void solve()
{
BiTree T;
int sec;
initBiTree(T);//初始化根节点
do
{
menu();
cin>>sec;
switch(sec)
{
case 1:solve_create(T);break;
case 2:solve_traverse(T);break;
case 3:destoryBiTree(T);
case 4:cout<<"恭喜!此次操作胜利完成!"<<endl;break;
default:cout<<"无此选项,请重新选择"<<endl;
}
system("PAUSE");
}while(sec != 4);
}
int main()
{
solve();
return 0;
}
*本次实验我需要完成的任务
*建树:先序递归,中序递归,层次非递归
*遍历:先序递归,中序递归,后序递归,层次非递归,中序非递归
*/
#include <iostream>
#include <queue>
#include <stack>
using namespace std;
typedef int TElemType;
//============二叉链表存储结构=================
typedef struct BiTNode
{
TElemType data;
struct BiTNode *lchild, *rchild;
}BiTNode, *BiTree;
//================初始化二叉树=====================
void initBiTree(BiTree &T)
{
T = NULL;
}
//================销毁二叉树========================
void destoryBiTree(BiTree &T)
{
if(T)
{
if(T->lchild)//有左子树
destoryBiTree(T->lchild);//销毁左子树
if(T->rchild)
destoryBiTree(T->rchild);
free(T);//释放根节点
T = NULL;//空指针赋NULL
}
}
//==================递归建树=========================
//XXXXXXXXXXXXXX递归建树之先序建树XXXXXXXXXXXXXXXXXXX
void preCreateBiTree(BiTree &T)
{//先序建树
//输入0表示空树
TElemType ch;
cin>>ch;
if(!ch)
{
T = NULL;
}
else
{
T = new BiTNode;
if(!T)
{
cout<<"内存申请失败"<<endl;
exit(0);
}
T->data = ch;
preCreateBiTree(T->lchild);
preCreateBiTree(T->rchild);
}
}
void FInCreateBiTree(BiTree &T, int cc[], int low, int high)
{//中序递归建树
if(low <= high)
{
int mid = (low + high) / 2;
if(cc[mid])
{
T = new BiTNode;
T->data = cc[mid];
T->lchild = T->rchild = NULL;
FInCreateBiTree(T->lchild, cc, low, mid - 1);
FInCreateBiTree(T->rchild, cc, mid + 1, high);
}
}
}
//==================递归建树=========================
//===================非递归建树======================
//XXXXXXXXXXXXXXX非递归之层次建树XXXXXXXXXXXXXXXXXXXX
void LevelCreateBiTree(BiTree &T)
{//层次建树
TElemType cl, cr;
BiTree pp;
queue<BiTree> Q;
cin>>cl;
if(!cl)
{//建立了空树
T= NULL;
return;
}
T = new BiTNode;
T->data = cl;
Q.push(T);
while(!Q.empty())
{
pp = Q.front();
Q.pop();
cin>>cl>>cr;
if(cl)
{
pp->lchild = new BiTNode;
pp->lchild->data = cl;
Q.push(pp->lchild);
}
else
pp->lchild = NULL;
if(cr)
{
pp->rchild = new BiTNode;
pp->rchild->data = cr;
Q.push(pp->rchild);
}
else
pp->rchild = NULL;
}
}
//==============此函数用于输出节点========================
void Visit(TElemType e)
{
cout<<e<<" ";
}
//=============递归遍历======================================
//XXXXXXXXXXXXXXXXXXXX递归遍历之先序遍历XXXXXXXXXXXXXXXXXXXXX
void PreOrderTraverse(BiTree T, void (*Visit)(TElemType))
{//先序遍历递归算法
if(T)
{
Visit(T->data);//1.先遍历根结点
PreOrderTraverse(T->lchild, Visit);//2.遍历左子树
PreOrderTraverse(T->rchild, Visit);//3.遍历右子树
}
}
//XXXXXXXXXXXXXXXXXXXXXX递归遍历之中序遍历XXXXXXXXXXXXXXXXXXXX
void InOrderTraverse(BiTree T, void (*Visit) (TElemType))
{//中序遍历递归算法
if(T)
{
InOrderTraverse(T->lchild, Visit);
Visit(T->data);
InOrderTraverse(T->rchild, Visit);
}
}
//XXXXXXXXXXXXXXXXXXXXXXX递归遍历之后序遍历XXXXXXXXXXXXXXXXXXX
void PostOrderTraverse(BiTree T, void (*Visit) (TElemType))
{//后序遍历递归算法
if(T)
{
PostOrderTraverse(T->lchild, Visit);
PostOrderTraverse(T->rchild, Visit);
Visit(T->data);
}
}
//================递归遍历====================================
//===================非递归遍历===============================
//XXXXXXXXXXXXXXXXXX非递归遍历之层次遍历XXXXXXXXXXXXXXXXX
void LevelOrderTraverse(BiTree T, void(*Visit) (TElemType))
{//层次遍历
queue<BiTree> Q;
BiTree ptr;
Q.push(T);
while(!Q.empty())
{
ptr = Q.front();
Q.pop();
Visit(ptr->data);
if(ptr->lchild)
Q.push(ptr->lchild);
if(ptr->rchild)
Q.push(ptr->rchild);
}
}
//XXXXXXXXXXXXXXXXXXXXXX非递归遍历之中序遍历XXXXXXXXXXXXX
void FInOrderTraverse(BiTree T)
{//非递归中序遍历
stack<BiTree> S;
BiTree pp = T;
while(pp || !S.empty())
{
if(pp)
{
S.push(pp);
pp = pp->lchild;
}
else
{
pp = S.top();
S.pop();
Visit(pp->data);
pp = pp->rchild;
}
}
}
void menu_create()
{
cout<<"---------------------===请选择建树方式===-------------------"<<endl;
cout<<" 1.先序递归建树"<<endl;
cout<<" 2.中序递归建树"<<endl;
cout<<" 3.层次非递归建树"<<endl;
}
void menu_traverse()
{
cout<<"---------------------===请选择比遍历方式===-------------------"<<endl;
cout<<" 1.先序递归遍历"<<endl;
cout<<" 2.中序递归遍历"<<endl;
cout<<" 3.后序递归遍历"<<endl;
cout<<" 4.中序非递归遍历"<<endl;
cout<<" 5.层次非递归遍历"<<endl;
}
void menu()
{
cout<<"****************本程序主菜单********************"<<endl;
cout<<"* *"<<endl;
cout<<"* 1.建树 *"<<endl;
cout<<"* 2.遍历 *"<<endl;
cout<<"* 3.销毁二叉树 *"<<endl;
cout<<"* 4.退出 *"<<endl;
cout<<"* *"<<endl;
cout<<"************************************************"<<endl;
}
void solve_create(BiTree &T)
{
int tt,n;
int *cc;
menu_create();
cin>>tt;
switch(tt)
{
case 1:
{
cout<<"输入先序构树序列"<<endl;
preCreateBiTree(T);
cout<<"先序构树成功"<<endl;
break;
}
case 2:
{
cout<<"输入满二叉的所有节点数:"<<endl;
cin>>n;
cc = new int[n];
if(!cc){cout<<"申请动态空间失败"<<endl;exit(0);}
cout<<"现在可以输入"<<n<<"个节点了"<<endl;
for(int i = 0; i < n; i++)
cin>>cc[i];
FInCreateBiTree(T,cc, 0, n);
cout<<"中序构树成功"<<endl;
break;
}
case 3:
{
cout<<"输入层次构树序列"<<endl;
LevelCreateBiTree(T);
cout<<"层次构树成功"<<endl;
break;
}
default:cout<<"选项错误,本次操作失败"<<endl;
}
}
void solve_traverse(BiTree T)
{
int n;
menu_traverse();
cin>>n;
switch(n)
{
case 1:PreOrderTraverse(T, Visit);break;
case 2:InOrderTraverse(T, Visit);break;
case 3:PostOrderTraverse(T, Visit);break;
case 4:FInOrderTraverse(T);break;
case 5:LevelOrderTraverse(T,Visit);break;
default :cout<<"输入无此选项,本次操作失败"<<endl;
}
cout<<endl;
}
void solve()
{
BiTree T;
int sec;
initBiTree(T);//初始化根节点
do
{
menu();
cin>>sec;
switch(sec)
{
case 1:solve_create(T);break;
case 2:solve_traverse(T);break;
case 3:destoryBiTree(T);
case 4:cout<<"恭喜!此次操作胜利完成!"<<endl;break;
default:cout<<"无此选项,请重新选择"<<endl;
}
system("PAUSE");
}while(sec != 4);
}
int main()
{
solve();
return 0;
}