#二叉树
#Queue.h
//
// Created by Carry on 2021/10/26.
//
#ifndef EXAM_QUEUE_H
#define EXAM_QUEUE_H
#endif //EXAM_QUEUE_H
#define MaxSize 50
typedef BiTNode ElemType;
typedef struct {
ElemType data[MaxSize];
int front, rear;
} SqQueue;
typedef struct {
ElemType data;
struct linkNode *next;
} linkNode;
typedef struct {
linkNode *front, *rear;
} linkQueue;
void InitQueue(linkQueue *Q) { //带头节点的链式队列
Q->front = Q->rear = (linkNode *) malloc(sizeof(linkNode));
Q->front->next = NULL;
}
int IsEmpty(linkQueue Q) {
if (Q.front == Q.rear)return 1;
else return 0;
}
void EnQueue(linkQueue *Q, ElemType x) {
linkNode *s = (linkNode *) malloc(sizeof(linkNode));
s->data = x;
Q->rear->next = s;
Q->rear = s;
}
int DeQueue(linkQueue *Q, ElemType *x) {
if (Q->rear == Q->front) {
return 0;
}
linkNode *p = Q->front->next;
*x = p->data;
Q->front->next = p->next;
if (Q->rear == p) {
Q->rear = Q->front;
}
free(p);
return 1;
}
Tree.c
//
// Created by Carry on 2021/10/26.
//
#include "stdlib.h"
#include "stdio.h"
typedef int Elemtype;
typedef struct BiTNode {
Elemtype data;
struct BiTNode *lchild, *rchild;
} BiTNode, *BiTree;
#include "Queue.h"
void LevelOrder(BiTree T){
linkQueue *L = (linkQueue *) malloc(sizeof (linkNode));
InitQueue(L);
BiTree p = (BiTree) malloc(sizeof (BiTNode));
EnQueue(L,*T);
while (!IsEmpty(*L)){
DeQueue(L,p);
printf("%d ",p->data);
if(p->lchild){
EnQueue(L,*p->lchild);
}
if (p->rchild) {
EnQueue(L,*p->rchild);
}
}
}
int main() {
BiTree node1 = (BiTree) malloc(sizeof(BiTNode));
node1->data = 3;
BiTNode node2;
BiTNode node3;
BiTNode node4;
node2.data = 4;
node3.data = 8;
node4.data = 10;
node1->lchild = &node2;
node1->rchild = &node3;
node2.lchild = &node4;
node2.rchild = node3.lchild = node3.rchild = node4.rchild = node4.lchild = NULL;
LevelOrder(node1);
return 0;
}
#运行结果




