#define _CRT_SECURE_NO_WARINGS 1
#define QueueSize 1000
#include<stdio.h>
#include<malloc.h>
typedef char DataType;
/*
队列实现
*/
typedef struct CirQueue {
DataType data[QueueSize];
int front, rear;
}CirQueue;
void initCQ(CirQueue* q) {//初始化
q->front = q->rear = QueueSize-1;
}
int EnQueue(CirQueue* q, DataType x) {//入队
if ((q->rear + 1) % QueueSize == q->front) {
return 0;//入队失败
}
q->rear = (q->rear + 1) % QueueSize;//队尾在循环意义加一
q->data[q->rear] = x;
return 1;
}
int DeQueue(CirQueue* q, DataType* ptr) {//出队
if (q->front == q->rear) {
return 0;
}
q->front = (q->front + 1) % QueueSize;
*ptr = q->data[q->front];
return 1;
}
int Empty(CirQueue* q) {
if (q->front == q->rear) {
return 1;//为空
}
else
return 0;
}
/*
二叉树的数据结构实现
*/
typedef struct BiNode {
DataType data;
struct BiNode* lchild, * rchild;
}BiNode;
typedef struct {
BiNode* root;
}BiTree;
BiNode* Creat() {
DataType data;
BiNode* root;
scanf("%c", &data);
if (data == '#') {
root = NULL;
}
else {
root = (BiNode*)malloc(sizeof(BiNode));
root->data= data;
root->lchild = Creat();
root->rchild = Creat();
}
return root;
}
void CreatBiTree(BiTree* tree) {
tree->root = Creat();
}
void initBiTree(BiTree* tree) {
tree->root = NULL;
}
void Destroy(BiNode* root) {
if (root == NULL) {
return;
}
else {
Destroy(root->lchild);
Destroy(root->rchild);
free(root);
}
}
void DestroyBiTree(BiTree* tree) {
Destroy(tree->root);
}
void Pre(BiNode* root) {//前
if (root == NULL) {
return;
}
else {
printf("%c", root->data);
Pre(root->lchild);
Pre(root->rchild);
}
}
void PreTree(BiTree* tree) {
Pre(tree->root);
}
void In(BiNode* root) {//中
if (root == NULL) {
return;
}
else {
In(root->lchild);
printf("%c", root->data);
In(root->rchild);
}
}
void InTree(BiTree* tree) {
In(tree->root);
}
void Post(BiNode* root) {//后
if (root == NULL) {
return;
}
else {
Post(root->lchild);
Post(root->rchild);
printf("%c", root->data);
}
}
void PostTree(BiTree* tree) {
Post(tree->root);
}
void Lever(BiTree* tree) {
BiNode* q = NULL;
CirQueue que;
initCQ(&que);
if (tree->root == NULL) {
return;
}
EnQueue(&que, tree->root);
while (!Empty(&que)) {
DeQueue(&que, &q);
printf("%c", q->data);
if (q->lchild != NULL) {
EnQueue(&que, q->lchild);
}
if (q->rchild != NULL) {
EnQueue(&que, q->rchild);
}
}
}
int main() {
BiTree tree;
initBiTree(&tree);
CreatBiTree(&tree);
PreTree(&tree);
printf("\n");
InTree(&tree);
printf("\n");
PostTree(&tree);
printf("\n");
Lever(&tree);
DestroyBiTree(&tree);
return 0;
}
Lever层序遍历时,p为什么会访问权限冲突