您的位置:首页 > 理论基础 > 数据结构算法

二叉树-遍历

2015-10-12 20:45 344 查看
#include<iostream>
#include<malloc.h>
#include<stdio.h>
#define  MAXSIZE  20  //最多结点个数
using namespace std;

/*二叉链表*/
typedef struct bnode{
char data;
struct bnode *lchild,*rchild;
}Bnode,* BTree;

/*顺序栈*/
typedef BTree DataType;
typedef struct{
DataType data[MAXSIZE];
int top;
}SeqStack,* PSeqStack;

/*循环队列*/
typedef struct{
DataType data[MAXSIZE];
int front,rear;
}SeqQueue,* PSeqQueue;

PSeqStack Init_SeqStack(void);				//建空栈
void Push_SeqStack(PSeqStack S,DataType x);	//入栈
int Empty_SeqStack(PSeqStack S);			//判断栈是否为空
void Pop_SeqStack(PSeqStack S,DataType * y);//出栈
void Destroy_SeqStack(PSeqStack * S);		//销毁栈

PSeqQueue Init_SeqQueue(void);				//循环队列初始化
int Empty_SeqQueue(PSeqQueue Q);			//判断队空
void In_SeqQueue(PSeqQueue Q,DataType x);	//入队
void Out_SeqQueue(PSeqQueue Q,DataType * y);//出队
void Destory_SeqQueue(PSeqQueue * Q);		//销毁队列

BTree CreateBinTree(void);//先序创建二叉树

void PreOrder(BTree t);	//递归先序遍历
void InOrder(BTree t);		//递归中序遍历
void PostOrder(BTree t);	//递归后序遍历

void LevelOrder(BTree t);	//层序遍历

void NRPreOrder(BTree t);	//非递归先序遍历
void NRInOrder(BTree t);	//非递归中序遍历
void NRPostOrder(BTree t);	//非递归后序遍历

/*主函数*/
int main(){

cout<<"\n请按先序次序输入各结点的值,以#表示空树(输入时可连续输入):\n";
BTree T=CreateBinTree();
if(!T){
cout<<"\n未建立树,请先建树!";
return 0;
}
cout<<"\n\n递归先序遍历:";
PreOrder(T);
cout<<"\n递归中序遍历:";
InOrder(T);
cout<<"\n递归后序遍历:";
PostOrder(T);

cout<<"\n\n层序遍历:";
LevelOrder(T);

cout<<"\n\n非递归先序遍历:";
NRPreOrder(T);
cout<<"\n非递归中序遍历:";
NRInOrder(T);
cout<<"\n非递归后序遍历:";
NRPostOrder(T);

return 1;
}

/******************************顺序栈的一些基本操作******************************/
/*建空栈*/
PSeqStack Init_SeqStack(void){
PSeqStack S;
S=(PSeqStack)malloc(sizeof(SeqStack));
if(S){
S->top = -1;//表示空栈
}
return S;
}
/*入栈*/
void Push_SeqStack(PSeqStack S,DataType x){
if(S->top == MAXSIZE-1){
cout<<"栈满不能入栈!";
}else{
S->top++;
S->data[S->top]=x;
}
}
/*判断栈是否为空*/
int Empty_SeqStack(PSeqStack S){
if(S->top == -1)
return 1;
else
return 0;
}
/*出栈*/
void Pop_SeqStack(PSeqStack S,DataType * y){
if(Empty_SeqStack(S)){
cout<<"栈空不能出栈!";
}else{
* y=S->data[S->top];
S->top--;
}
}
/*销毁栈*/
void Destroy_SeqStack(PSeqStack * S){
if(* S)
free(* S);
* S=NULL;
}

/******************************循环队列的一些基本操作******************************/
/*循环队列初始化*/
PSeqQueue Init_SeqQueue(void){
PSeqQueue Q;
Q=(PSeqQueue)malloc(sizeof(SeqQueue));
if(Q){
Q->front=0;
Q->rear=0;
}
return Q;
}
/*判断队空*/
int Empty_SeqQueue(PSeqQueue Q){
if(Q && Q->front==Q->rear)
return 1;
else
return 0;
}
/*入队*/
void In_SeqQueue(PSeqQueue Q,DataType x){
if(Q->front == (Q->rear+1)%MAXSIZE){
cout<<"队满不能入队!";
return;
}else{
Q->rear=(Q->rear+1)%MAXSIZE;
Q->data[Q->rear]=x;
}
}
/*出队*/
void Out_SeqQueue(PSeqQueue Q,DataType * y){
if(Empty_SeqQueue(Q)){
cout<<"队空不能出队!";
return;
}else{
Q->front=(Q->front+1)%MAXSIZE;
* y=Q->data[Q->front];
}
}
/*销毁队列*/
void Destory_SeqQueue(PSeqQueue * Q){
if(* Q)
free(* Q);
* Q=NULL;
}

/*先序创建二叉树*/
BTree CreateBinTree(void){
BTree t;
char ch=getchar();
if(ch=='#')
t=NULL;//读入#时,将相应节点指针置空
else{
t=(Bnode *)malloc(sizeof(Bnode));
t->data=ch;
t->lchild=CreateBinTree();//构造二叉树的左子树
t->rchild=CreateBinTree();//构造二叉树的右子树
}
return t;
}

/******************************递归******************************/
void PreOrder(BTree t){//递归先序遍历
if(t){
cout<<t->data<<" ";
PreOrder(t->lchild);
PreOrder(t->rchild);
}
}

void InOrder(BTree t){//递归中序遍历
if(t){
InOrder(t->lchild);
cout<<t->data<<" ";
InOrder(t->rchild);
}
}

void PostOrder(BTree t){//递归后序遍历
if(t){
PostOrder(t->lchild);
PostOrder(t->rchild);
cout<<t->data<<" ";
}
}

/******************************层序遍历******************************/
void LevelOrder(BTree t){
BTree p;
PSeqQueue Q=Init_SeqQueue();
if(t)
In_SeqQueue(Q,t);

while(!Empty_SeqQueue(Q)){
Out_SeqQueue(Q,&p);
cout<<p->data<<" ";
if(p->lchild)
In_SeqQueue(Q,p->lchild);
if(p->rchild)
In_SeqQueue(Q,p->rchild);
}
Destory_SeqQueue(&Q);
}

/******************************非递归******************************/
void NRPreOrder(BTree t){//非递归先序遍历
BTree p=t;
PSeqStack S=Init_SeqStack();

while(p || !Empty_SeqStack(S)){
if(p){
cout<<p->data<<" ";
Push_SeqStack(S,p);
p=p->lchild;
}else{
Pop_SeqStack(S,&p);
p=p->rchild;
}
}
Destroy_SeqStack(&S);
}

void NRInOrder(BTree t){//非递归中序遍历
BTree p=t;
PSeqStack S=Init_SeqStack();

while(p || !Empty_SeqStack(S)){
if(p){
Push_SeqStack(S,p);
p=p->lchild;
}else{
Pop_SeqStack(S,&p);
cout<<p->data<<" ";
p=p->rchild;
}
}
Destroy_SeqStack(&S);
}

void NRPostOrder(BTree t){//非递归后序遍历
BTree p=t;
PSeqStack S1=Init_SeqStack();
PSeqStack S2=Init_SeqStack();

while(p || !Empty_SeqStack(S2)){
if(p){
Push_SeqStack(S1,p);

Push_SeqStack(S2,p);
p=p->rchild;
}else{
Pop_SeqStack(S2,&p);
p=p->lchild;
}
}
while(!Empty_SeqStack(S1)){
Pop_SeqStack(S1,&p);
cout<<p->data<<" ";
}
Destroy_SeqStack(&S1);
Destroy_SeqStack(&S2);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息