数据结构——二叉树的层序遍历

层序遍历需用到队列功能,基本思想是将树的根结点入队,出队遍历;分别将出队结点的左右子树入队,继续出队遍历,直至对空。

上代码

头文件

#ifndef BINARYTREE_H
#define BINARYTREE_H
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
typedef struct node Node;//重定义类型名称 
typedef Node Bintree;//重定义树类型名称 
typedef int Elementtype;//重定义数据类型名称 
struct node{//声明结构体原型 
	Elementtype value;
	Node *left;
	Node *right;
};

typedef struct stnode stNode;
typedef Node* Element;
struct stnode{
	Element value;
	stNode *next;
};

typedef struct _queue{
	stNode *front;
	stNode *rear;
}Queue;
#endif
Bintree *creat();//创建树 
Node *initNode();//初始化结点 
void clean(Bintree **pTr);//清空树,需要改变指针内容所有传入二级指针 
bool isempty(Bintree *pTr);//判树空 

void Levelordertraversal(Bintree *pTr);//层序遍历 

/*队列操作函数*/
typedef stNode quNode;
Queue *creatQue();
int isemptyQue(Queue *pQu);
void enQue(Queue *pQu,Element value);
Element deQue(Queue *pQu);
void cleanQue(Queue **pQu); 

函数实现 

#include "binarytree.h"
Bintree *creat(){
	Bintree *pTr=NULL;
	Node *p=initNode();
	pTr=p;
	p->left=initNode();
	p->right=initNode();
	Node *q=p->right;
	p=p->left;
	p->left=initNode();
	p->right=initNode();
	q->left=initNode();
	q->right=initNode();
	return pTr;
}
Node *initNode(){
	Node *pNode=(Node*)malloc(sizeof(Node));
	scanf("%d",&pNode->value);
	pNode->left=pNode->right=NULL;
}
bool isempty(Bintree *pTr){
	return pTr==NULL;
}
void clean(Bintree **pTr){
	if(*pTr){
		clean(&(*pTr)->left);
		clean(&(*pTr)->right);
		free(*pTr);
		*pTr=NULL;
	}
}

void Levelordertraversal(Bintree *pTr){//层序遍历 
	Bintree *ptemp=pTr;
	Queue *pQu=creatQue();
	if(ptemp)enQue(pQu,ptemp);
	while(!isemptyQue(pQu)){
		ptemp=deQue(pQu);
		printf("%-2d",ptemp->value);
		enQue(pQu,ptemp->left);
		enQue(pQu,ptemp->right);
	}
	cleanQue(&pQu); 
}


/*队列操作函数*/
Queue *creatQue(){
	Queue *pQu=(Queue*)malloc(sizeof(Queue));
	pQu->front=pQu->rear=NULL;
	return pQu;
} 
int isemptyQue(Queue *pQu){
	return pQu->front==NULL;
}
void enQue(Queue *pQu,Element value){
	if(value){
		quNode *pNode=(quNode*)malloc(sizeof(quNode));
		pNode->value=value;
		pNode->next=NULL;
		if(isemptyQue(pQu)){
			pQu->front=pQu->rear=pNode;
		}else{
			pQu->rear->next=pNode;
			pQu->rear=pNode;
		}
	}	
}
Element deQue(Queue *pQu){
	Element ptemp=NULL;
	if(!isemptyQue(pQu)){
		ptemp=pQu->front->value;
		quNode *p=pQu->front;
		if(pQu->front==pQu->rear){
			pQu->front=pQu->rear=NULL;
		}else{
			pQu->front=pQu->front->next;
		}
		free(p);
	}
	return ptemp;
}
void cleanQue(Queue **pQu){
	while(!isemptyQue(*pQu)){
		deQue(*pQu);
	}
	free(*pQu);
	*pQu=NULL;
}

main函数

#include <stdio.h>
#include <stdlib.h>
#include "binarytree.h"
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
	Bintree *pTr=creat();
    Levelordertraversal(pTr);printf("\n\n");
	
	clean(&pTr);
	if(isempty(pTr))printf("树已清空\n");
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值