如何实现二叉树层次遍历

核心思想:

首先,将二叉树的最祖先节点入队列

然后,循环执行以下步骤,知道队列空。

一:节点出队列

二:该节点如果有左孩子节点,左孩子节点入队列

三:该节点如果有右孩子节点,右孩子节点入队列

void LayerOrder(BiTreeNode *head)
{
	LQueue Q;
	Initiate_Queue(&Q);
	BiTreeNode *p;
	if(head!=NULL) AppendQueue(&Q,head);
	while(QueueNotEmpty(&Q))
	{
		p=QueueDelete(&Q);
		cout<<p->data<<"  ";
		if(p->LChild!=NULL) AppendQueue(&Q,p->LChild);
		if(p->RChild!=NULL) AppendQueue(&Q,p->RChild);	
	}
}



完整代码如下:

#include<iostream>
using namespace std;

typedef struct biTreeNode
{
	char data;
	struct biTreeNode *LChild;
	struct biTreeNode *RChild;
}BiTreeNode;


void Initiate_Tree(BiTreeNode **head)
{
	(*head)=(BiTreeNode *)malloc(sizeof(BiTreeNode));
	(*head)->LChild=NULL;
	(*head)->RChild=NULL;
}

 BiTreeNode *InsertLChild(BiTreeNode *head,char x)
{
	if(head==NULL) return NULL;
	else
	{
	BiTreeNode *p1,*p2;
	p1=head->LChild;
	p2=(BiTreeNode*)malloc(sizeof(BiTreeNode));
	p2->data=x;
	p2->RChild=NULL;
	head->LChild=p2;
	p2->LChild=p1;
	return p2;
	}
}


BiTreeNode* InsertRChild(BiTreeNode *head,char x)
{
	if(head==NULL) return NULL;
	{
	BiTreeNode *p1,*p2;
	p1=head->RChild;
	p2=(BiTreeNode*)malloc(sizeof(BiTreeNode));
	p2->data=x;
	p2->LChild=NULL;
	head->RChild=p2;
	p2->RChild=p1;
	return p2;
	}
}

void DLR(BiTreeNode *head)
{
	if(head==NULL)  return;
	else
	{
	cout<<head->data<<"  ";
	DLR(head->LChild);
	DLR(head->RChild);
	}
}

//==================================================

typedef struct lNode
{
	BiTreeNode *data;
	struct lNode *next;
}LNode;


typedef struct lQueue
{
	LNode *front;
	LNode *rear;
}LQueue;


void Initiate_Queue(LQueue *Q)
{
	Q->front=NULL;
	Q->rear=NULL;
}

void AppendQueue(LQueue *Q,BiTreeNode *head)
{
	LNode *p1;
	p1=(LNode *)malloc(sizeof(LNode));
	p1->next=NULL;
	p1->data=head;
	if(Q->front==NULL)
	{
		Q->front=Q->rear=p1;
	}
	else
	{
		Q->rear->next=p1;
		Q->rear=p1;
	}
}

BiTreeNode * QueueDelete(LQueue *Q)
{
	if(Q->front==NULL) return NULL;
	else
	{
		BiTreeNode *p;
		p=Q->front->data;
		Q->front=Q->front->next;
		return p;
	}
}

int QueueNotEmpty(LQueue *Q)
{
	if(Q->front==NULL) return 0;
	else return 1;
}

//层序遍历
void LayerOrder(BiTreeNode *head)
{
	LQueue Q;
	Initiate_Queue(&Q);
	BiTreeNode *p;
	if(head!=NULL) AppendQueue(&Q,head);
	while(QueueNotEmpty(&Q))
	{
		p=QueueDelete(&Q);
		cout<<p->data<<"  ";
		if(p->LChild!=NULL) AppendQueue(&Q,p->LChild);
		if(p->RChild!=NULL) AppendQueue(&Q,p->RChild);	
	}
}


void main()
{
	BiTreeNode *head,*p,*p1;
	Initiate_Tree(&head);
	head->data='j';
	p=InsertLChild(head,'k');
	p=InsertLChild(p,'a');
	InsertRChild(head,'b');
	cout<<"前序遍历为:"<<endl;
	DLR(head);
	cout<<endl;
	cout<<"层序遍历为:"<<endl;
	LayerOrder(head);
	system("pause");
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值