#include<iostream>
#include<stack>
#include<deque>
using namespace std;
typedef struct BinTree
{
int data;
BinTree* Lch;
BinTree* Rch;
BinTree(int initData=0)
{
Lch=NULL;
Rch=NULL;
data=initData;
}
}BinTree;
void PreOrderTrav_Stack(BinTree *root)
{
if(root==NULL)
{
cout<<"二叉树为空,程序直接返回\n";
return;
}
stack<BinTree*> stk;
stk.push(root);
while(!stk.empty())
{
BinTree* bt=stk.top();
stk.pop();
cout<<bt->data<<",";
if(bt->Rch)
stk.push(bt->Rch);
if(bt->Lch)
stk.push(bt->Lch);
}
}
void PreOrderTrav_Recur(BinTree *root)
{
if(root!=NULL)
{
cout<<root->data<<",";
PreOrderTrav_Recur(root->Lch);
PreOrderTrav_Recur(root->Rch);
}
}
void InOrderTrav_Stack(BinTree *root)
{
if(root==NULL)
{
cout<<"二叉树为空,程序直接返回\n";
return;
}
stack<BinTree*> stk;
BinTree* t=root;
while(!stk.empty()||t!=NULL)
{
if(t!=NULL)
{
stk.push(t);
t=t->Lch;
}
else
{
t=stk.top();
stk.pop();
cout<<t->data<<",";
t=t->Rch;
}
}
}
void InOrderTrav_Recur(BinTree *root)
{
if(root!=NULL)
{
InOrderTrav_Recur(root->Lch);
cout<<root->data<<",";
InOrderTrav_Recur(root->Rch);
}
}
void PostOrderTrav_Stack(BinTree *root)
{
if(root==NULL)
{
cout<<"二叉树为空,程序直接返回\n";
return;
}
stack<BinTree*> stk;
stack<int> stk_data;
stk.push(root);
while(!stk.empty())
{
BinTree *bt=stk.top();
stk.pop();
stk_data.push(bt->data);
if(bt->Lch)
stk.push(bt->Lch);
if(bt->Rch)
stk.push(bt->Rch);
}
while(!stk_data.empty())
{
int temp=stk_data.top();
cout<<temp<<",";
stk_data.pop();
}
}
void PostOrderTrav_Recur(BinTree *root)
{
if(root!=NULL)
{
PostOrderTrav_Recur(root->Lch);
PostOrderTrav_Recur(root->Rch);
cout<<root->data<<",";
}
}
void LevelOrderTrav(BinTree *root)
{
deque<BinTree*> dq;
dq.push_back(root);
while(!dq.empty())
{
BinTree *temp=dq.front();
dq.pop_front();
cout<<temp->data<<",";
if(temp->Lch)
dq.push_back(temp->Lch);
if(temp->Rch)
dq.push_back(temp->Rch);
}
}
BinTree *BTArray[13];
void BinTreeGenerate(BinTree *root)
{
BTArray[1]=root;
for(int i=2;i<=12;i++)
{
BTArray[i]=new BinTree(i);
}
BTArray[1]->Lch=BTArray[2];BTArray[1]->Rch=BTArray[3];
BTArray[2]->Lch=BTArray[4];BTArray[2]->Rch=BTArray[5];
BTArray[4]->Lch=BTArray[7];BTArray[4]->Rch=BTArray[6];
BTArray[5]->Lch=BTArray[8];BTArray[5]->Rch=BTArray[10];
BTArray[3]->Rch=BTArray[9];
BTArray[9]->Lch=BTArray[11];BTArray[9]->Rch=BTArray[12];
}
void DelTree()
{
for(int i=2;i<=12;i++)
{
delete BTArray[i];
}
}
int main()
{
BinTree *root=new BinTree;
root->data=1;
BinTreeGenerate(root);
cout<<"---------------PreOrder-------------------\n";
cout<<"非递归方式实现先序遍历:\n";
PreOrderTrav_Stack(root);
cout<<endl;
cout<<"递归方式实现先序遍历:\n";
PreOrderTrav_Recur(root);
cout<<endl;
cout<<"---------------InOrder--------------------\n";
cout<<"非递归方式实现中序遍历:\n";
InOrderTrav_Stack(root);
cout<<endl;
cout<<"递归方式实现中序遍历:\n";
InOrderTrav_Recur(root);
cout<<endl;
cout<<"---------------PostOrder------------------\n";
cout<<"非递归方式实现后序遍历:\n";
PostOrderTrav_Stack(root);
cout<<endl;
cout<<"递归方式实现后序遍历:\n";
PostOrderTrav_Recur(root);
cout<<endl;
cout<<"---------------LevelOrder-----------------\n";
cout<<"实现层次遍历:\n";
LevelOrderTrav(root);
cout<<endl;
cout<<"-----------------------------------------\n";
delete root;
DelTree();
return 0;
}
|