数据结构-----层次遍历二叉树算法(利用队列实现)

一、层次遍历

       从上至下,从左至右:每访问一个节点后,将该节点的左子树的节点指针进队,然后再将该节点的右子树的节点指针进队;出队时,正好先出来的是左子树的根节点的指针。

二、代码实现

#include<stdio.h>
#include<stdlib.h>

typedef char EType;

struct BinaryTreeNode//定义节点
{  
    EType data;  
    struct BinaryTreeNode *LChild;  
    struct BinaryTreeNode *RChild;  
};  
typedef BinaryTreeNode BinaryTree; 

typedef struct QType  
{  
    BinaryTreeNode *ptr;
}QType; 

typedef struct Queue
{
	QType *element;//循环线性队列,0号空间不使用(为了区分队满与队空),注意这不是链式队列
	int front;
	int rear;
	int MaxSize;
}Queue;

void CreatBiTree(BinaryTreeNode **BT);
bool IsEmpty(Queue &Q);
bool IsFull(Queue &Q);
bool GetFront(Queue &Q,QType &result);
bool EnQueue(Queue &Q,QType &x);
bool DeQueue(Queue &Q,QType &result);
void LevelOrder_LtoR_UtoD(BinaryTreeNode *BT);
void LevelOrder_RtoL_UtoD(BinaryTreeNode *BT);

int main()
{
	BinaryTreeNode *BT = NULL;
	CreatBiTree(&BT);
	printf("从上至下,从左至右遍历二叉树输出为:");
	LevelOrder_LtoR_UtoD(BT);
	printf("\n");
	printf("从上至下,从右至左遍历二叉树输出为:");
	LevelOrder_RtoL_UtoD(BT);
	printf("\n");
	return 0;
}

void CreatQueue(Queue &Q,int MaxQueueSize)
{
	Q.MaxSize = MaxQueueSize;
	Q.element = new QType[Q.MaxSize+1];
	Q.front = 0;
	Q.rear = 0;
}

bool IsEmpty(Queue &Q)
{
	if(Q.front == Q.rear)
		return true;
	return false;
}

bool IsFull(Queue &Q)
{
	if(Q.front == (Q.rear+1)%(Q.MaxSize+1))
		return true;
	return false;
}

bool GetFront(Queue &Q,QType &result)
{
	if(IsEmpty(Q))
		return false;
	result = Q.element[(Q.front+1)%(Q.MaxSize+1)];
	return true;
}

bool EnQueue(Queue &Q,QType &x)//进队操作:rear向后移
{
	if(IsFull(Q))
		return false;
	Q.rear = (Q.rear+1)%(Q.MaxSize+1);
	Q.element[Q.rear] = x;
	return true;
}

bool DeQueue(Queue &Q,QType &result)//出队操作:front向后移
{
	if(IsEmpty(Q))
		return false;
	Q.front = (Q.front+1)%(Q.MaxSize+1);
	result = Q.element[Q.front];
	return true;
}

void CreatBiTree(BinaryTreeNode **BT)//以前序遍历方法输入节点的data,构造一棵二叉树
{  
    EType tem;  
      
    scanf("%c",&tem);  
    if(' ' == tem)  
    {  
        *BT = NULL;  
    }  
    else  
    {  
        *BT = new BinaryTreeNode;  
        (*BT)->data = tem;  
        CreatBiTree(&(*BT)->LChild);//递归
        CreatBiTree(&(*BT)->RChild);  
    }  
}  

void LevelOrder_LtoR_UtoD(BinaryTreeNode *BT)
{
	Queue Q;
	QType temp;
	BinaryTreeNode *p;
	int MaxQueueSize = 50;
	CreatQueue(Q,MaxQueueSize);
	p = BT;
	temp.ptr = p;
	EnQueue(Q,temp);//先将根节点进队
	while(p)//二叉树为空就直接结束
	{
		if(!DeQueue(Q,temp))节点出队,当队空时,出队操作返回false,程序return
			return;//这是这个循环的出口,如果一个二叉树不为空,最终都是在这里跳出循环
		p = temp.ptr;
		printf("%c\t",p->data);

		if(p->LChild)//若该节点的左孩子存在,则将其进队
		{
			temp.ptr = p->LChild;
			EnQueue(Q,temp);
		}
        if(p->RChild)//若该节点的右孩子存在,则将其进队
		{
			temp.ptr = p->RChild;
			EnQueue(Q,temp);
		}
	}
}

void LevelOrder_RtoL_UtoD(BinaryTreeNode *BT)
{
    Queue Q;
	QType temp;
	BinaryTreeNode *p;
	int MaxQueueSize = 50;
	CreatQueue(Q,MaxQueueSize);
	p = BT;
	temp.ptr = p;
	EnQueue(Q,temp);
	while(p)
	{
		if(!DeQueue(Q,temp))
			return;
		p = temp.ptr;
		printf("%c\t",p->data);

		if(p->RChild)
		{
			temp.ptr = p->RChild;
			EnQueue(Q,temp);
		}
		if(p->LChild)
		{
			temp.ptr = p->LChild;
			EnQueue(Q,temp);
		}
	}
}
三、 效果展示

建立这样一棵二叉树


所以按照前序遍历输入应该是:“AB_D_ _CE_ _ _”(其中“_”代表空格)

那么运行结果为:


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值