一.头文件
BTree.h
#include <stdio.h>
typedef char BTDataType;
#define ENDTAG '#'
typedef struct BinaryTreeNode
{
BTDataType _data;
struct BinaryTreeNode* _left;
struct BinaryTreeNode* _right;
}BTNode;
BTNode* BinaryTreeCreate(BTDataType* a);
void BinaryTreePrevOrder(BTNode* root);
void BinaryTreeInOrder(BTNode* root);
void BinaryTreePostOrder(BTNode* root);
void BinaryTreeLevelOrder(BTNode* root);
#endif
queue.h
#ifndef _Queue_H_
#define _Queue_H_
#include "BTree.h"
typedef BTNode * QuDataType;
typedef struct QueueNode {
QuDataType data;
struct QueueNode * next;
}QueueNode;
typedef struct Queue {
QueueNode * front;
QueueNode * rear;
}Queue;
void QueueInit(Queue* pphead);
void QueueDestory(Queue* pphead);
QueueNode * BuyQueueNode(QuDataType x);
void QueuePop(Queue* pq);
void QueuePush(Queue* pq, QuDataType x);
QuDataType QueueFront(Queue* pq);
int QueueIsEmpty(Queue* pq);
#endif /*_Queue_H_*/ //_Queue_H_
二.c文件
BTree.c
#include "BTree.h"
#include "queue.h"
#include <stdlib.h>
BTNode* BinaryTreeCreate(BTDataType* a)
{
BTNode * cur;
static int i = 0;
if (a[i] == ENDTAG)
{
i++;
return NULL;
}
cur = (BTNode *)malloc(sizeof(BTNode));
cur->_data = a[i];
i++;
cur->_left = BinaryTreeCreate(a);
cur->_right = BinaryTreeCreate(a);
return cur;
}
void BinaryTreePrevOrder(BTNode* root)
{
if (root != NULL)
{
printf("%c", root->_data);
BinaryTreePrevOrder(root->_left);
BinaryTreePrevOrder(root->_right);
}
}
void BinaryTreeInOrder(BTNode* root)
{
if (root != NULL)
{
BinaryTreeInOrder(root->_left);
printf("%c", root->_data);
BinaryTreeInOrder(root->_right);
}
}
void BinaryTreePostOrder(BTNode* root)
{
if (root != NULL)
{
BinaryTreePostOrder(root->_left);
BinaryTreePostOrder(root->_right);
printf("%c", root->_data);
}
}
void BinaryTreeLevelOrder(BTNode* root)
{
Queue qu;
BTNode * tmp;
QueueInit(&qu);
QueuePush(&qu, root);
while (!QueueIsEmpty(&qu))
{
tmp = QueueFront(&qu);
printf("%c", tmp->_data);
if (tmp->_left)
{
QueuePush(&qu, tmp->_left);
}
if (tmp->_right)
{
QueuePush(&qu, tmp->_right);
}
QueuePop(&qu);
}
QueueDestory(&qu);
}
queue.c
#include "queue.h"
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
void QueueInit(Queue* pq)
{
pq->front = NULL;
pq->rear = NULL;
}
void QueueDestory(Queue* pq)
{
if (pq->front == NULL)
{
return;
}
while (pq->front)
{
QueuePop(pq);
}
}
QueueNode * BuyQueueNode(QuDataType x)
{
QueueNode * cur = (QueueNode *)malloc(sizeof(QueueNode));
cur->data = x;
cur->next = NULL;
return cur;
}
void QueuePop(Queue* pq)
{
if (pq->front == NULL)
{
return;
}
QueueNode* tmp = pq->front->next;
free(pq->front);
pq->front = tmp;
}
void QueuePush(Queue* pq, QuDataType x)
{
QueueNode * cur = BuyQueueNode(x);
if (pq->front == NULL)
{
pq->front = pq->rear = cur;
}
else
{
pq->rear->next = cur;
pq->rear = cur;
}
}
QuDataType QueueFront(Queue* pq)
{
return pq->front->data;
}
int QueueIsEmpty(Queue* pq)
{
return pq->front == NULL;
}
main.c
#include "BTree.h"
#include "queue.h"
#include<Windows.h>
int main()
{
BTNode * testTree = BinaryTreeCreate("ABDF####CE#GH##I#J###");
BinaryTreePrevOrder(testTree);//先序遍历
putchar('\n');
BinaryTreeInOrder(testTree);//中序遍历
putchar('\n');
BinaryTreePostOrder(testTree);//后序遍历
putchar('\n');
BinaryTreeLevelOrder(testTree);//层序遍历
putchar('\n');
system("pause");
return 0;
}