数据结构--二叉树--层次遍历二叉树(链式遍历--队列)

#define CHAR /* 字符型 */
#include <stdio.h> /* EOF(=^Z或F6),NULL */
#include <stdlib.h>
#include<math.h> /* floor(),ceil(),abs() */
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
typedef int Status; /* Status是函数的类型,其值是函数结果状态代码,如OK等 */

#ifdef CHAR
typedef char TElemType;
TElemType Nil=' '; /* 字符型以空格符为空 */
#endif
#ifdef INT
typedef int TElemType;
TElemType Nil=0; /* 整型以0为空 */
#endif

typedef struct BiTNode
{
	TElemType data;
	struct BiTNode *lchild,*rchild; /* 左右孩子指针 */
}BiTNode,*BiTree;

typedef BiTree QElemType; /* 设队列元素为二叉树的指针类型 */

typedef struct QNode//队列节点结构体
{
	QElemType data;
	struct QNode *next;
}QNode,*QueuePtr;


typedef struct //队列
{
	QueuePtr front,rear; /* 队头、队尾指针 */
}LinkQueue;

Status InitBiTree(BiTree *T)
{ /* 操作结果: 构造空二叉树T */
	*T=NULL;
	return OK;
}

void CreateBiTree(BiTree *T)
{
	TElemType ch;
#ifdef CHAR
	scanf("%c",&ch);
#endif
#ifdef INT
	scanf("%d",&ch);
#endif
	if(ch==Nil) /* 空 */
		*T=NULL;
	else
	{
		*T=(BiTree)malloc(sizeof(BiTNode));
		if(!*T)
			exit(OVERFLOW);
		(*T)->data=ch; /* 生成根结点 */
		CreateBiTree(&(*T)->lchild); /* 构造左子树 */
		CreateBiTree(&(*T)->rchild); /* 构造右子树 */
	}
}

Status InitQueue(LinkQueue *Q)
{ /* 构造一个空队列Q */
	(*Q).front=(*Q).rear=(QueuePtr)malloc(sizeof(QNode));
	if(!(*Q).front)
		exit(OVERFLOW);
	(*Q).front->next=NULL;
	return OK;
}

Status QueueEmpty(LinkQueue Q)
{ /* 若Q为空队列,则返回TRUE,否则返回FALSE */
	if(Q.front==Q.rear)
		return TRUE;
	else
		return FALSE;
}

Status EnQueue(LinkQueue *Q,QElemType e)
{ /* 插入元素e为Q的新的队尾元素 */
	QueuePtr p=(QueuePtr)malloc(sizeof(QNode));
	if(!p) /* 存储分配失败 */
		exit(OVERFLOW);
	p->data=e;
	p->next=NULL;
	(*Q).rear->next=p;
	(*Q).rear=p;
	return OK;
}
Status DeQueue(LinkQueue *Q,QElemType *e)
{ /* 若队列不空,删除Q的队头元素,用e返回其值,并返回OK,否则返回ERROR */
	QueuePtr p;
	if((*Q).front==(*Q).rear)
		return ERROR;
	p=(*Q).front->next;
	*e=p->data;
	(*Q).front->next=p->next;
	if((*Q).rear==p)
		(*Q).rear=(*Q).front;
	free(p);
	return OK;
}

void LevelOrderTraverse(BiTree T,Status(*Visit)(TElemType))
{ /* 初始条件:二叉树T存在,Visit是对结点操作的应用函数 */
	/* 操作结果:层序递归遍历T(利用队列),对每个结点调用函数Visit一次且仅一次 */
	LinkQueue q;
	QElemType a;
	if(T)
	{
		InitQueue(&q);
		EnQueue(&q,T);
		while(!QueueEmpty(q))
		{
			DeQueue(&q,&a);
			Visit(a->data);
			if(a->lchild!=NULL)
				EnQueue(&q,a->lchild);
			if(a->rchild!=NULL)
				EnQueue(&q,a->rchild);
		}
		printf("\n");
	}
}
Status visitT(TElemType e)
{
#ifdef CHAR
	printf("%c ",e);
#endif
#ifdef INT
	printf("%d ",e);
#endif
	return OK;
}
void main()
{
	BiTree T;
	InitBiTree(&T);
#ifdef CHAR
	printf("请先序输入二叉树(如:ab三个空格表示a为根结点,b为左子树的二叉树)\n");
#endif
#ifdef INT
	printf("请先序输入二叉树(如:1 2 0 0 0表示1为根结点,2为左子树的二叉树)\n");
#endif
	CreateBiTree(&T);
	printf("层次遍历二叉树:\n");
	LevelOrderTraverse(T,visitT);
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值