先序遍历
先序遍历的递归过程为
(1)访问根结点
(2)先序遍历根结点的左子树
(3)先序遍历根结点的右子树
举例:
代码:
void PreOrder(BTree root)
{
if (root != NULL)
{
printf("%c ", root->data);
PreOrder(root->lchild);
PreOrder(root->rchild);
}
}
中序遍历
中序遍历的递归过程为
(1)中序遍历根结点的左子树
(2)访问根结点
(3)中序遍历根结点的右子树
举例:
代码:
void InOrder(BTree root)
{
if (root != NULL)
{
InOrder(root->lchild);
printf("%c ", root->data);
InOrder(root->rchild);
}
}
后序遍历
后序遍历的递归过程为
(1)后序遍历二叉树的左子树
(2)后序遍历二叉树的右子树
(3)访问根结点。
举例:
代码:
void PostOrder(BTree root)
{
if (root != NULL)
{
PostOrder(root->lchild);
PostOrder(root->rchild);
printf("%c ", root->data);
}
}
层次遍历
(1)根结点入队列
(2)根结点出队列,根结点的左子树、右子树相继入队列
(3)根结点的左子树结点出队列,左子树结点的左子树、右子树相继入队列
(4)…
举例:
代码:
void LevelOrder(BTree root)
{
Queue myQueue;
QueueInit(&myQueue);
BTree temp=root;
//根节点入队
PushQueue(&myQueue, root);
while (!QueueIsEmpty(&myQueue))//队列不为空
{
temp = GetQueueFrontValue(&myQueue);
printf("%c ", temp->data);
PopQueue(&myQueue);
if (temp ->lchild != NULL)
{
PushQueue(&myQueue, temp->lchild);
}
if (temp->rchild != NULL)
{
PushQueue(&myQueue, temp->rchild);
}
}
}
完整代码:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include <Windows.h>
typedef char DataType;
#define MAXSIZE 20
//二叉树结构体
typedef struct Tree
{
DataType data;
struct Tree* lchild, * rchild;//指向左右孩子的指针域
}Tree,*BTree;
//顺序队列结构体
typedef struct CycQueue
{
BTree data[MAXSIZE];
int front, tail;//队列的头指针和尾指针
}Queue;
//初始化循环队列
void QueueInit(Queue* myQueue)
{
for (int i = 0; i < MAXSIZE; i++)
{
myQueue->data[i] = 0;
}
myQueue->front = myQueue->tail = 0;
}
//入队列
void PushQueue(Queue* myQueue, BTree data)
{
//判断队列是否已满
if ((myQueue->tail + 1) % MAXSIZE == myQueue->front)
{
printf("此时队列已满不能进行入队操作!\n");
exit(-1);
}
myQueue->data[myQueue->tail] = data;
myQueue->tail = (myQueue->tail + 1) % MAXSIZE;
}
//判断队列是否为空
bool QueueIsEmpty(Queue* myQueue)
{
if (myQueue->front == myQueue->tail)
return true;
else
return false;
}
//出队列
void PopQueue(Queue* myQueue)
{
if (QueueIsEmpty(myQueue))
{
printf("此时队列已经为空不能进行出队操作!\n");
exit(-1);
}
myQueue->data[myQueue->front] = NULL;
myQueue->front = (myQueue->front + 1) % MAXSIZE;
}
//取队头元素
BTree GetQueueFrontValue(Queue* myQueue)
{
if(QueueIsEmpty(myQueue))
{
printf("此时队列已经为空不能进行取队头元素操作!\n");
exit(-1);
}
return myQueue->data[myQueue->front];
}
//二叉树的创建
int flag=0;
BTree CreateTree()
{
BTree root;//树的根节点
root = NULL;
char data;
char temp;
if (flag == 0)
{
printf("请输入一个根节点:");
flag = 1;
}
scanf_s("%c", &data,1);
temp = getchar();
if (data == '#')
{
return NULL;
}
else
{
root = (BTree)malloc(sizeof(Tree));
if (root == NULL)
return NULL;
root->data = data;
printf("请输入%c的左孩子:", root->data);
root->lchild = CreateTree();
printf("请输入%c的右孩子:", root->data);
root->rchild = CreateTree();
}
return root;
}
//先序遍历递归算法
void PreOrder(BTree root)
{
if (root != NULL)
{
printf("%c ", root->data);
PreOrder(root->lchild);
PreOrder(root->rchild);
}
}
//中序遍历递归算法
void InOrder(BTree root)
{
if (root != NULL)
{
InOrder(root->lchild);
printf("%c ", root->data);
InOrder(root->rchild);
}
}
//后续遍历递归算法
void PostOrder(BTree root)
{
if (root != NULL)
{
PostOrder(root->lchild);
PostOrder(root->rchild);
printf("%c ", root->data);
}
}
//二叉树的层次遍历
void LevelOrder(BTree root)
{
Queue myQueue;
QueueInit(&myQueue);
BTree temp=root;
//根节点入队
PushQueue(&myQueue, root);
while (!QueueIsEmpty(&myQueue))//队列不为空
{
temp = GetQueueFrontValue(&myQueue);
printf("%c ", temp->data);
PopQueue(&myQueue);
if (temp ->lchild != NULL)
{
PushQueue(&myQueue, temp->lchild);
}
if (temp->rchild != NULL)
{
PushQueue(&myQueue, temp->rchild);
}
}
}
void test_Tree()
{
BTree root;
root = CreateTree();
printf("先序遍历递归算法遍历序列:");
PreOrder(root);
printf("\n");
printf("中序遍历递归算法遍历序列:");
InOrder(root);
printf("\n");
printf("后序遍历递归算法遍历序列:");
PostOrder(root);
printf("\n");
printf("二叉树层次遍历算法遍历序列:");
LevelOrder(root);
printf("\n");
}
int main(void)
{
test_Tree();
system("pause");
return EXIT_SUCCESS;
}
运行截图: