# 数据结构的C实现_二叉树的非递归遍历和层序遍历

//编译环境G++
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;

//////////////TreeNode///////////////////////////////////////////
typedef struct _TreeNode
{
char key;
struct _TreeNode *Lc;
struct _TreeNode *Rc;
}TreeNode;
////////////////////////////////////////////////////////////////

////////stack//////////////////////////////////////////////////
//Record作为栈结点的一个域，存储树结点的访问信息
typedef struct _StackRecord
{
bool leftvisit;
bool rightvisit;
bool currentvisit;
TreeNode *treenode;
}StackRecord;

//初始化Record
void InitStackRecord(StackRecord &s_record)
{
s_record.leftvisit=false;
s_record.rightvisit=false;
s_record.currentvisit=false;
s_record.treenode=NULL;
}

//栈结点
typedef struct _StackNode
{
StackRecord record;
struct _StackNode *next;
}StackNode;

//栈
typedef struct _Stack
{
StackNode *top;
}Stack;

void InitStack(Stack &s)
{
s.top=NULL;
}

bool isemptyS(Stack &s)
{
if(s.top==NULL)
return 1;
return 0;
}

void Push(Stack &s,StackRecord e)
{
StackNode *node=(StackNode*)malloc(sizeof(StackNode));
node->record=e;
node->next=NULL;
node->next=s.top;
s.top=node;
}

bool Pop(Stack &s,StackRecord &e)
{
if(isemptyS(s)) return false;
else
{
StackNode *node=s.top;
e=node->record;
s.top=node->next;
free(node);
return true;
}
}

void DestroyStack(Stack &s)
{
StackRecord temp;InitStackRecord(temp);
while(Pop(s,temp)){}
}
///////////////////////////////////////////////////////////////////////////

////Queue//////////////////////////////////////////////////////////////////
//队列结点
typedef struct _QNode
{
TreeNode *treenode;
struct _QNode *next;
}QNode;

//队列
typedef struct _Queue
{
QNode *front;
QNode *rear;
}Queue;

void InitQueue(Queue &Q)
{
Q.front=NULL;
Q.rear=NULL;
}

bool isemptyQ(Queue &Q)
{
if(Q.front==NULL)
return 1;
return 0;
}
void EnQueue(Queue &Q,TreeNode *_treenode)
{
//printf("EnQueue: %c\n",_treenode->key);
QNode *s=(QNode*)malloc(sizeof(QNode));
s->treenode=_treenode;
s->next=NULL;
if(isemptyQ(Q))
{
Q.front=s;
Q.rear=s;
return;
}
else
{
Q.rear->next=s;
Q.rear=s;
}
}

bool DeQueue(Queue &Q,TreeNode *&treenode)
{
if(isemptyQ(Q))
return false;
else
{
QNode *node=Q.front;
treenode=node->treenode;
Q.front=node->next;
if(Q.front==NULL)
{
Q.rear=NULL;
}
free(node);
}
return true;
}

void DestroyQueue(Queue &Q)
{
TreeNode *temp;
while(DeQueue(Q,temp)){}
}
/////////////////////////////////////////////////////////////////////////
//非递归先序遍历
void NRPreOrderTravserse(TreeNode *T)
{
Stack stack;
InitStack(stack);
if(T!=NULL)
{
StackRecord record;
InitStackRecord(record);
record.treenode=T;
Push(stack,record);
while(!isemptyS(stack))
{
Pop(stack,record);
//check current
if(!record.currentvisit)
{
printf("%c ",record.treenode->key);
record.currentvisit=true;
Push(stack,record);
continue;
}

//check left
if(!record.leftvisit)
{
record.leftvisit=true;
Push(stack,record);
if(record.treenode->Lc!=NULL)
{
StackRecord leftrecord;
InitStackRecord(leftrecord);
leftrecord.treenode=record.treenode->Lc;
Push(stack,leftrecord);
}
continue;
}
//check right
if(!record.rightvisit)
{
record.rightvisit=true;
Push(stack,record);
if(record.treenode->Rc!=NULL)
{
StackRecord rightrecord;
InitStackRecord(rightrecord);
rightrecord.treenode=record.treenode->Rc;
Push(stack,rightrecord);
}
continue;
}
}
}
DestroyStack(stack);
}

//非递归中序遍历
void NRInOrderTravserse(TreeNode *T)
{
Stack stack;
InitStack(stack);
if(T!=NULL)
{
StackRecord record;
InitStackRecord(record);
record.treenode=T;
Push(stack,record);
while(!isemptyS(stack))
{
Pop(stack,record);
//check left
if(!record.leftvisit)
{
record.leftvisit=true;
Push(stack,record);
if(record.treenode->Lc!=NULL)
{
StackRecord leftrecord;
InitStackRecord(leftrecord);
leftrecord.treenode=record.treenode->Lc;
Push(stack,leftrecord);
}
continue;
}
//check current
if(!record.currentvisit)
{
printf("%c ",record.treenode->key);
record.currentvisit=true;
Push(stack,record);
continue;
}
//check right
if(!record.rightvisit)
{
record.rightvisit=true;
Push(stack,record);
if(record.treenode->Rc!=NULL)
{
StackRecord rightrecord;
InitStackRecord(rightrecord);
rightrecord.treenode=record.treenode->Rc;
Push(stack,rightrecord);
}
continue;
}
}
}
DestroyStack(stack);
}

//非递归后序遍历
void NRPostOrderTravserse(TreeNode *T)
{
Stack stack;
InitStack(stack);
if(T!=NULL)
{
StackRecord record;
InitStackRecord(record);
record.treenode=T;
Push(stack,record);
while(!isemptyS(stack))
{
Pop(stack,record);
//check left
if(!record.leftvisit)
{
record.leftvisit=true;
Push(stack,record);
if(record.treenode->Lc!=NULL)
{
StackRecord leftrecord;
InitStackRecord(leftrecord);
leftrecord.treenode=record.treenode->Lc;
Push(stack,leftrecord);
}
continue;
}

//check right
if(!record.rightvisit)
{
record.rightvisit=true;
Push(stack,record);
if(record.treenode->Rc!=NULL)
{
StackRecord rightrecord;
InitStackRecord(rightrecord);
rightrecord.treenode=record.treenode->Rc;
Push(stack,rightrecord);
}
continue;
}

//check current
if(!record.currentvisit)
{
printf("%c ",record.treenode->key);
record.currentvisit=true;
Push(stack,record);
continue;
}
}
}
DestroyStack(stack);
}

//层序遍历，利用队列
void LevelTravserse(TreeNode *T)
{
Queue Q;
InitQueue(Q);
//EnQueue(Q,T);
if(T!=NULL)
{
EnQueue(Q,T);
while(!isemptyQ(Q))
{
TreeNode *currentTreeNode;
DeQueue(Q,currentTreeNode);

if(currentTreeNode->Lc!=NULL)
{
EnQueue(Q,currentTreeNode->Lc);
}
if(currentTreeNode->Rc!=NULL)
{
EnQueue(Q,currentTreeNode->Rc);
}
printf("%c ",currentTreeNode->key);
}
}
DestroyQueue(Q);
}

int main()
{
TreeNode *tree,*p;
p=(TreeNode*)malloc(sizeof(TreeNode));
p->key='A';p->Lc=NULL;p->Rc=NULL;
tree=p;

p=(TreeNode*)malloc(sizeof(TreeNode));
p->key='B';
tree->Lc=p;

p=(TreeNode*)malloc(sizeof(TreeNode));
p->key='C';
p->Lc=NULL;
p->Rc=NULL;
tree->Lc->Lc=p;

p=(TreeNode*)malloc(sizeof(TreeNode));
p->key='D';
p->Lc=NULL;
p->Rc=NULL;
tree->Lc->Rc=p;

p=(TreeNode*)malloc(sizeof(TreeNode));
p->key='E';
p->Lc=NULL;
tree->Rc=p;

p=(TreeNode*)malloc(sizeof(TreeNode));
p->key='F';
p->Lc=NULL;
p->Rc=NULL;
tree->Rc->Rc=p;

NRPreOrderTravserse(tree);printf("\n");
NRInOrderTravserse(tree);printf("\n");
NRPostOrderTravserse(tree);printf("\n");
LevelTravserse(tree);

return 0;
}

• 本文已收录于以下专栏：

举报原因： 您举报文章：数据结构的C实现_二叉树的非递归遍历和层序遍历 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)