二叉树的遍历实验报告C语言,二叉树遍历实现

/**

* 按先序创建二叉树.

* 依次输入二叉树的结点值(一个字符)

* 空格表示空树

*/

Status CreateBiTree( BiThrTree &T)

{// 先序创建二叉树

char ch;

scanf(\"%c\",&ch);

if (ch == ' ')

{

T = NULL;

}

else

{

T = (BiThrNode *)malloc(sizeof(BiThrNode));

T->data = ch;

T->visited = 0;

if (!T)

{

return OVERFLOW;

}

CreateBiTree(T->pLChild);

CreateBiTree(T->pRChild);

}

return OK;

}

/**

* 先序遍历二叉树

* mod = true, 递归

* mod = false, 非递归

*/

Status PreOrderTraverse( BiThrTree T, Status(* visit)(TElemType e),bool mod = false)

{// 先序遍历

if (T)

{

if (mod)

{//递归

if (visit(T->data))

{

if (PreOrderTraverse(T->pLChild,visit,mod))

{

if (PreOrderTraverse(T->pRChild,visit,mod))

{

return OK;

}

}

}

}

else

{//非递归

SqStack S;

InitStack(S);

Push(S,T);

BiThrTree p;

while (!StackEmpty(S))

{

while (GetTop(S,p) && p)

{

if (!visit(p->data))

{

return ERROR;

}

Push(S,p->pLChild);

}//while

Pop(S,p);

if (!StackEmpty(S))

{

Pop(S,p);

Push(S,p->pRChild);

}

}//while

return OK;

}

return ERROR;

}

return OVERFLOW;

}//PreOrderTraverse

/**

* 中序遍历二叉树

* mod = true, 递归

* mod = false, 非递归

*/

Status InOrderTraverse( BiThrTree T, Status(* visit)(TElemType e),bool mod = false)

{// 中序遍历

if (T)

{

if (mod)

{

if (InOrderTraverse(T->pLChild,visit,mod))

{

if (visit(T->data))

{

if (InOrderTraverse(T->pRChild,visit,mod))

{

return OK;

}

}

}

}

else

{//非递归

SqStack S;

InitStack(S);

Push(S,T);

BiThrTree p;

while (!StackEmpty(S))

{

while(GetTop(S,p) && p)

{

Push(S,p->pLChild);

}

Pop(S,p);

if (!StackEmpty(S))

{

Pop(S,p);

if( !visit(p->data) )

{

return ERROR;

}

Push(S,p->pRChild);

}

}

return OK;

}

return ERROR;

}

return OVERFLOW;

}

/**

* 后序遍历二叉树

* mod = true, 递归

* mod = false, 非递归

*/

Status PostOrderTraverse( BiThrTree T, Status(* visit)(TElemType e),bool mod = false)

{// 后序遍历

if (T)

{

if (mod)

{//递归

if (PostOrderTraverse(T->pLChild,visit,mod))

{

if (PostOrderTraverse(T->pRChild,visit,mod))

{

if (visit(T->data))

{

return OK;

}

}

}

}

else

{//非递归

SqStack S;

InitStack(S);

Push(S,T);

BiThrTree p;

while(!StackEmpty(S))

{

while(GetTop(S,p) && p)

{

Push(S,p->pLChild);

if (p->pLChild && p->pLChild->visited == 1)

{//左子树被访问过

Pop(S,p);

Push(S,NULL);

}

}

Pop(S,p);

if (!StackEmpty(S))

{

GetTop(S,p);

{

if (!p->pRChild || p->pRChild->visited == 1)

{//右子树为空或被访问过

if (!visit(p->data))

{

return ERROR;

}

p->visited = 1;

Pop(S,p);

}

else

{

Push(S,p->pRChild);

}

}

}

}

return OK;

}

return ERROR;

}

return OVERFLOW;

}

/*

* 对二叉树进行层序遍历

*/

Status LeverOrderTraverse( BiThrTree T, Status(* visit)(TElemType e))

{// 层序遍历

LinkQueue Q;

InitQueue( Q );

if(T)

{

EnQueue(Q,T);

}

QElemType p;

while( !QueueEmpty(Q))

{

DeQueue(Q,p);

if( !(visit)(p->data))

{

return ERROR;

}

if(p->pLChild)

{

EnQueue(Q,p->pLChild); //左孩子入队列

}

if( p->pRChild )

{

EnQueue(Q,p->pRChild); // 右孩子入队列

}

}

return OK;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值