以下是实现静态模拟二叉树并分别先序、中序、后序遍历的代码
自己画的二叉树图_(:3」∠)_如下:
# include <stdio.h>
# include <malloc.h>
# include <stdlib.h>
#define MAXSIZE 100;
//静态模拟二叉树
//并分别构造先序、中序、后序函数遍历
typedef struct BinaryTree//二叉树结构体
{
char data;
struct BinaryTree * leftChild;
struct BinaryTree * rightChild;
} BinaryTree, * PBinaryTree;
PBinaryTree create_BinaryTree();//创建一个静态二叉树
void preTraverse_BinaryTree(PBinaryTree);//先序遍历二叉树
void inTraverse_BinaryTree(PBinaryTree);//中序遍历二叉树
void posTraverse_BinaryTree(PBinaryTree);//后序遍历二叉树
void levelTraverse_BinaryTree(PBinaryTree);//层次遍历二叉树
//主函数main入口
int main (void)
{
PBinaryTree tree = create_BinaryTree();
printf("此树的先序遍历为:");
preTraverse_BinaryTree(tree);
printf("\n");
printf("此树的中序遍历为:");
inTraverse_BinaryTree(tree);
printf("\n");
printf("此树的后序遍历为:");
posTraverse_BinaryTree(tree);
printf("\n");
printf("此树的层次遍历为:");
levelTraverse_BinaryTree(tree);
printf("\n");
return 0;
}//main
PBinaryTree create_BinaryTree()
{
//创建二叉树元素
PBinaryTree a = (PBinaryTree)malloc(sizeof(BinaryTree));
PBinaryTree b = (PBinaryTree)malloc(sizeof(BinaryTree));
PBinaryTree c = (PBinaryTree)malloc(sizeof(BinaryTree));
PBinaryTree d = (PBinaryTree)malloc(sizeof(BinaryTree));
PBinaryTree e = (PBinaryTree)malloc(sizeof(BinaryTree));
PBinaryTree f = (PBinaryTree)malloc(sizeof(BinaryTree));
PBinaryTree g = (PBinaryTree)malloc(sizeof(BinaryTree));
PBinaryTree h = (PBinaryTree)malloc(sizeof(BinaryTree));
//给二叉树元素赋值
a->data = 'A';
b->data = 'B';
c->data = 'C';
d->data = 'D';
e->data = 'E';
f->data = 'F';
g->data = 'G';
h->data = 'H';
//创建二叉树数据关系
a->leftChild = b;
a->rightChild = c;
b->leftChild = d;
b->rightChild = NULL;
c->leftChild = g;
c->rightChild = f;
d->leftChild = NULL;
d->rightChild = e;
e->leftChild = NULL;
e->rightChild = NULL;
f->leftChild = h;
f->rightChild = NULL;
g->leftChild = NULL;
g->rightChild = NULL;
h->leftChild = NULL;
h->rightChild = NULL;
return a;
}//create_BinaryTree
//先序遍历二叉树
void preTraverse_BinaryTree(PBinaryTree pBinaryTree)
{
if(NULL != pBinaryTree)
{
printf("%c ",pBinaryTree->data);
if(pBinaryTree->leftChild)
{
preTraverse_BinaryTree(pBinaryTree->leftChild);
}
if(pBinaryTree->rightChild)
{
preTraverse_BinaryTree(pBinaryTree->rightChild);
}
}
return;
}//preTraverse_BinaryTree
//中序遍历二叉树
void inTraverse_BinaryTree(PBinaryTree pBinaryTree)
{
if(NULL != pBinaryTree)
{
if(pBinaryTree->leftChild)
{
inTraverse_BinaryTree(pBinaryTree->leftChild);
}
printf("%c ",pBinaryTree->data);
if(pBinaryTree->rightChild)
{
inTraverse_BinaryTree(pBinaryTree->rightChild);
}
}
return;
}//inTraverse_BinaryTree
//后序遍历二叉树
void posTraverse_BinaryTree(PBinaryTree pBinaryTree)
{
if(NULL != pBinaryTree)
{
if(pBinaryTree->leftChild)
{
posTraverse_BinaryTree(pBinaryTree->leftChild);
}
if(pBinaryTree->rightChild)
{
posTraverse_BinaryTree(pBinaryTree->rightChild);
}
printf("%c ",pBinaryTree->data);
}
return;
}//posTraverse_BinaryTree
//层次遍历二叉树
void levelTraverse_BinaryTree(PBinaryTree pBinaryTree)
{
int front, rear;
PBinaryTree queue[100];
front = rear = 0;
PBinaryTree q;
if(NULL != pBinaryTree)
{
rear = (rear+1) % MAXSIZE;
queue[rear] = pBinaryTree;
while(front != rear)
{
front = (front+1) % MAXSIZE;
q = queue[front];
printf("%c ",q->data);
if(NULL != q->leftChild)
{
rear = (rear+1) % MAXSIZE;
queue[rear] = q->leftChild;
}
if(NULL != q->rightChild)
{
rear = (rear+1) % MAXSIZE;
queue[rear] = q->rightChild;
}
}
}
return;
}
2019/12/6 更新层次遍历
(tips:如有发现错误和问题欢迎提醒和指正,Thanks♪(・ω・)ノ~)