遍历二叉树
来源:互联网 发布:c语言matlab混编 编辑:程序博客网 时间:2024/04/28 00:10
/*BiTree.h 遍历二叉树*/
// 先序遍历二叉树
void Preorder (BiTree T)
{
if(T)
{
cout<<T->data<<" ";//输出根结点
Preorder(T->lchild);//遍历左子树
Preorder(T->rchild);//遍历右子树
}
}
//中序遍历二叉树
void Inorder(BiTree T)
{
if(T)
{
Inorder(T->lchild);//遍历左子树
cout<<T->data<<" ";//输出根结点
Inorder(T->rchild);//遍历右子树
}
}
//后序遍历二叉树
void Priorder(BiTree T)
{
if(T)
{
Priorder(T->lchild);
Priorder(T->rchild);
cout<<T->data<<" ";//输出根结点
}
}
//层次遍历二叉树
Status layer(BiTree T){
LinkQueue Q;
BiTree p;
InitQueue(Q);
if(T)EnQueue(Q, T);
while(!QueueEmpty(Q))
{
DeQueue(Q,p);
cout<<p->data<<" ";
if(p->lchild) EnQueue(Q, p->lchild);
if(p->rchild) EnQueue(Q, p->rchild);
}
return TRUE;
}//layer
//任务书之中序遍历
Status InOrder_iter(BiTree T)
{
TaskNode S;
LinkTaskStack L;
BiTree p=T;
S.ptr=T;
S.task=Travel;
InitStack(L);
if(T)
Push(L,S);//新任务根结点进栈
while(!StackEmpty(L))
{
Pop(L,S);
if(S.task==Visit) //访问任务
cout<<S.ptr->data<<" ";
else if(S.task==Travel)//遍历任务
{
if(S.ptr)
{
p=S.ptr;
S.ptr=p->rchild;
Push(L,S);//右子树进栈
S.ptr=p;
S.task=Visit;
Push(L,S);//根结点进栈
S.ptr=p->lchild;
S.task=Travel;
Push(L,S);//左子树进栈
}
}else {
return FALSE;
}
}
return TRUE;
}
//任务书之先序遍历
Status Preorder_iter(BiTree T)
{
TaskNode S;
LinkTaskStack L;
BiTree p=T;
S.ptr=T;
S.task=Travel;
InitStack(L);
if(T)
Push(L,S);//新任务根结点进栈
while(!StackEmpty(L))
{
Pop(L,S);
if(S.task==Visit) //访问任务
cout<<S.ptr->data<<" ";
else if(S.task==Travel)//遍历任务
{
if(S.ptr)
{
p=S.ptr;
S.ptr=p->rchild;
Push(L,S);//右子树进栈
S.ptr=p->lchild;
Push(L,S);//左子树进栈
S.task=Visit;
S.ptr=p;
Push(L,S);//根结点进栈
}
}else {
return FALSE;
}
}
return TRUE;
}//Preoder_iter
//任务书之后序遍历
Status PriOrder_iter(BiTree T)
{
TaskNode S;
LinkTaskStack L;
BiTree p=T;
S.ptr=T;
S.task=Travel;
InitStack(L);
if(T)
Push(L,S);//新任务根结点进栈
while(!StackEmpty(L))
{
Pop(L,S);
if(S.task==Visit) //访问任务
cout<<S.ptr->data<<" ";
else if(S.task==Travel)//遍历任务
{
if(S.ptr)
{
p=S.ptr;
S.task=Visit;
Push(L,S);//根结点进栈
S.task=Travel;
S.ptr=p->rchild;
Push(L,S);//右子树进栈
S.ptr=p->lchild;
Push(L,S);//左子树进栈
}
}else {
return FALSE;
}
}
return TRUE;
}//PriOrder
//路径之中序算法
/*
*进栈:结点的左子树存在,则该结点的地址进栈。
*出栈:结点的左右子树均为空,则将栈顶元素出栈。
*访问:结点的左子树为空或出栈的结点(第二次遇到)。
*/
Status Inorder_I(BiTree T)
{
BiTree e;
LinkStack L;//
InitStack(L);
if(T) Push(L,T);
while(!StackEmpty(L))
{
while(GetTop(L,e)&&e)Push(L,e->lchild);//向左走到尽头
Pop(L,e);//空指针退栈
if(!StackEmpty(L))//访问结点,向右一步
{
Pop(L,e);
cout<<e->data<<" ";
Push(L,e->rchild);
}
}
return TRUE;
}//Inorder_I
//路径之先序算法
Status PreOrder_I(BiTree T)
{
BiTree e;
LinkStack L;//
InitStack(L);
if(T) Push(L,T);
while(!StackEmpty(L))
{
while(GetTop(L,e)&&e)//向左走到尽头
{
cout<<e->data<<" ";//访问结点
Push(L,e->lchild);
}
Pop(L,e);//空指针退栈
if(!StackEmpty(L))//向右一步
{
Pop(L,e);
Push(L,e->rchild);
}
}
return TRUE;
}//PreOrder
//路径之后序遍历
Status PriOrder_I(BiTree T)
{
LinkStack L;
InitStack(L);
BiTree e,t=NULL;
if(T!=NULL)
Push(L,T);
while(!StackEmpty(L))
{
while(GetTop(L,e)&&e)//向左走到尽头
{
Push(L,e->lchild);
}
Pop(L,e);
if(!StackEmpty(L))
{
GetTop(L,e);
if(e->rchild==NULL)//当前结点的右子树为空则进行访问
{
cout<<e->data<<" ";//访问结点
Pop(L,e);t=e;
}
GetTop(L,e);
while(e->rchild==t)
{
cout<<e->data<<" ";//访问结点
Pop(L,e);t=e;
GetTop(L,e);
}
if(!StackEmpty(L))
{
GetTop(L,e);
Push(L,e->rchild);
}
}
}
return TRUE;
}
// 先序遍历二叉树
void Preorder (BiTree T)
{
if(T)
{
cout<<T->data<<" ";//输出根结点
Preorder(T->lchild);//遍历左子树
Preorder(T->rchild);//遍历右子树
}
}
//中序遍历二叉树
void Inorder(BiTree T)
{
if(T)
{
Inorder(T->lchild);//遍历左子树
cout<<T->data<<" ";//输出根结点
Inorder(T->rchild);//遍历右子树
}
}
//后序遍历二叉树
void Priorder(BiTree T)
{
if(T)
{
Priorder(T->lchild);
Priorder(T->rchild);
cout<<T->data<<" ";//输出根结点
}
}
//层次遍历二叉树
Status layer(BiTree T){
LinkQueue Q;
BiTree p;
InitQueue(Q);
if(T)EnQueue(Q, T);
while(!QueueEmpty(Q))
{
DeQueue(Q,p);
cout<<p->data<<" ";
if(p->lchild) EnQueue(Q, p->lchild);
if(p->rchild) EnQueue(Q, p->rchild);
}
return TRUE;
}//layer
//任务书之中序遍历
Status InOrder_iter(BiTree T)
{
TaskNode S;
LinkTaskStack L;
BiTree p=T;
S.ptr=T;
S.task=Travel;
InitStack(L);
if(T)
Push(L,S);//新任务根结点进栈
while(!StackEmpty(L))
{
Pop(L,S);
if(S.task==Visit) //访问任务
cout<<S.ptr->data<<" ";
else if(S.task==Travel)//遍历任务
{
if(S.ptr)
{
p=S.ptr;
S.ptr=p->rchild;
Push(L,S);//右子树进栈
S.ptr=p;
S.task=Visit;
Push(L,S);//根结点进栈
S.ptr=p->lchild;
S.task=Travel;
Push(L,S);//左子树进栈
}
}else {
return FALSE;
}
}
return TRUE;
}
//任务书之先序遍历
Status Preorder_iter(BiTree T)
{
TaskNode S;
LinkTaskStack L;
BiTree p=T;
S.ptr=T;
S.task=Travel;
InitStack(L);
if(T)
Push(L,S);//新任务根结点进栈
while(!StackEmpty(L))
{
Pop(L,S);
if(S.task==Visit) //访问任务
cout<<S.ptr->data<<" ";
else if(S.task==Travel)//遍历任务
{
if(S.ptr)
{
p=S.ptr;
S.ptr=p->rchild;
Push(L,S);//右子树进栈
S.ptr=p->lchild;
Push(L,S);//左子树进栈
S.task=Visit;
S.ptr=p;
Push(L,S);//根结点进栈
}
}else {
return FALSE;
}
}
return TRUE;
}//Preoder_iter
//任务书之后序遍历
Status PriOrder_iter(BiTree T)
{
TaskNode S;
LinkTaskStack L;
BiTree p=T;
S.ptr=T;
S.task=Travel;
InitStack(L);
if(T)
Push(L,S);//新任务根结点进栈
while(!StackEmpty(L))
{
Pop(L,S);
if(S.task==Visit) //访问任务
cout<<S.ptr->data<<" ";
else if(S.task==Travel)//遍历任务
{
if(S.ptr)
{
p=S.ptr;
S.task=Visit;
Push(L,S);//根结点进栈
S.task=Travel;
S.ptr=p->rchild;
Push(L,S);//右子树进栈
S.ptr=p->lchild;
Push(L,S);//左子树进栈
}
}else {
return FALSE;
}
}
return TRUE;
}//PriOrder
//路径之中序算法
/*
*进栈:结点的左子树存在,则该结点的地址进栈。
*出栈:结点的左右子树均为空,则将栈顶元素出栈。
*访问:结点的左子树为空或出栈的结点(第二次遇到)。
*/
Status Inorder_I(BiTree T)
{
BiTree e;
LinkStack L;//
InitStack(L);
if(T) Push(L,T);
while(!StackEmpty(L))
{
while(GetTop(L,e)&&e)Push(L,e->lchild);//向左走到尽头
Pop(L,e);//空指针退栈
if(!StackEmpty(L))//访问结点,向右一步
{
Pop(L,e);
cout<<e->data<<" ";
Push(L,e->rchild);
}
}
return TRUE;
}//Inorder_I
//路径之先序算法
Status PreOrder_I(BiTree T)
{
BiTree e;
LinkStack L;//
InitStack(L);
if(T) Push(L,T);
while(!StackEmpty(L))
{
while(GetTop(L,e)&&e)//向左走到尽头
{
cout<<e->data<<" ";//访问结点
Push(L,e->lchild);
}
Pop(L,e);//空指针退栈
if(!StackEmpty(L))//向右一步
{
Pop(L,e);
Push(L,e->rchild);
}
}
return TRUE;
}//PreOrder
//路径之后序遍历
Status PriOrder_I(BiTree T)
{
LinkStack L;
InitStack(L);
BiTree e,t=NULL;
if(T!=NULL)
Push(L,T);
while(!StackEmpty(L))
{
while(GetTop(L,e)&&e)//向左走到尽头
{
Push(L,e->lchild);
}
Pop(L,e);
if(!StackEmpty(L))
{
GetTop(L,e);
if(e->rchild==NULL)//当前结点的右子树为空则进行访问
{
cout<<e->data<<" ";//访问结点
Pop(L,e);t=e;
}
GetTop(L,e);
while(e->rchild==t)
{
cout<<e->data<<" ";//访问结点
Pop(L,e);t=e;
GetTop(L,e);
}
if(!StackEmpty(L))
{
GetTop(L,e);
Push(L,e->rchild);
}
}
}
return TRUE;
}