广度优先周游二叉树(层序遍历)是用队列来实现的,从二叉树的第一层(根结点)开始,自上至下逐层遍历;在同一层中,按照从左到右的顺序对结点逐一访问。
按照从根结点至叶结点、从左子树至右子树的次序访问二叉树的结点。算法:
1初始化一个队列,并把根结点入列;
2根节点入队
3循环直到队列空
3.1 q=队列头元素出队
3.2 访问q的数据域
3.3 若q有左孩子 左孩子入队
3.4 若q有右孩子 右孩子入队
代码来自 http://leidiqiu.iteye.com/blog/854087
按照从根结点至叶结点、从左子树至右子树的次序访问二叉树的结点。算法:
1初始化一个队列,并把根结点入列;
2根节点入队
3循环直到队列空
3.1 q=队列头元素出队
3.2 访问q的数据域
3.3 若q有左孩子 左孩子入队
3.4 若q有右孩子 右孩子入队
代码来自 http://leidiqiu.iteye.com/blog/854087
#include <stdio.h>
#include <stdlib.h>
//树节点的定义
struct TNode{
int value;
struct TNode *left;
struct TNode *right;
};
//队列节点的定义
struct QNode{
struct TNode *p;
struct QNode *next;
};
//队列的定义
struct Queue{
struct QNode *front;
struct QNode *rear;
};
//初始化队列
void InitQueue(struct Queue *Q){
Q->front=Q->rear=(struct QNode*)malloc(sizeof(struct QNode));
Q->front->next=NULL;
}
//入队列
void EnQueue(struct Queue *Q, struct TNode *node){
printf("en-node-value:%d\n",node->value);
struct QNode *nd=(struct QNode*)malloc(sizeof(struct QNode));
nd->next=NULL;
nd->p=node;
Q->rear->next=nd;
Q->rear=nd;
}
//出队列,注意判定快为空时,应有 rear=front
struct TNode* DeQueue(struct Queue *Q){
struct QNode *p=Q->front->next;
Q->front->next=Q->front->next->next;
struct TNode *t=p->p;
if(Q->rear==p){
Q->rear=Q->front;
}
free(p);
printf("de-node-value:%d\n",t->value);
return t;
}
//建立二叉树
void createLR(struct TNode *node){
if(node->value<20){
struct TNode *n1=(struct TNode*)malloc(sizeof(struct TNode));
struct TNode *n2=(struct TNode*)malloc(sizeof(struct TNode));
node->left=n1;
n1->value=node->value*2;
node->right=n2;
n2->value=node->value*2+1;
createLR(n1);
createLR(n2);
}else{
node->left=NULL;
node->right=NULL;
}
}
//主程序
int main(int argc, char **argv){
struct TNode *head=(struct TNode*)malloc(sizeof(struct TNode));
head->value=1;
createLR(head);
//输出左、右边,测试树是否正确建立
struct TNode *p=head;
while(p){
printf("%d\n",p->value);
p=p->left;
}
p=head;
while(p){
printf("%d\n",p->value);
p=p->right;
}
printf("\n");
//用一个辅助的链队列,广度遍历树
p=head;
struct Queue *q;
InitQueue(q);
printf("%d\n",p->value);
EnQueue(q,p);
while(q->front->next!=NULL){
p=DeQueue(q);
if(p->left!=NULL){
printf("%d\n",p->left->value);
EnQueue(q,p->left);
}
if(p->right!=NULL){
printf("%d\n",p->right->value);
EnQueue(q,p->right);
}
}
//以下代码没有别的意思,就是测试命令行参数
int i=0;
for(i=0; i<argc; i++){
printf("%s\n",argv[i]);
}
return 0;
}