公共头文件common.h
#pragma once
#include<stdio.h>
#include<string.h>
#include<assert.h>
#include<stdlib.h>
typedef char BTDatatype;
typedef struct BinaryTreeNode
{
BTDatatype _data;
struct BinaryTreeNode* _left;
struct BinaryTreeNode* _right;
}BTNode;
typedef BTNode* QDataType;
typedef struct QueueNode
{
QDataType _data;
struct QueueNode* _next;
}QueueNode;
typedef struct Queue
{
QueueNode* _head;
QueueNode* _tail;
}Queue;
二叉树相关接口的介绍BTree.h
#ifndef __BTREE_H__
#define __BTREE_H__
#include"common.h"
//通过前序遍历的数组"ABD##E#H##CF##G##"构建二叉树
BTNode* BinaryTreeCreate(BTDatatype* array, int* pIndex);
void BinaryTreeDestory(BTNode** root);
int BinaryTreeSize(BTNode* root);
int BinaryTreeLeafSize(BTNode* root);
int BinaryTreeLevelSize(BTNode* root, int k);
BTNode* BinaryTreeFind(BTNode* root, BTDatatype x);
//遍历
void BinaryTreePrevOrder(BTNode* root);
void BinaryTreeInOrder(BTNode* root);
void BinaryTreePostOrder(BTNode* root);
//层序遍历
void BinaryTreeLevelOrder(BTNode* root);
//判断二叉树是否为完全二叉树
int BinaryTreeComplete(BTNode* root);
void BTreeTest();
#endif //__BTREE_H__
队列头文件Queue.h(为实现二叉树的层序遍历)
#pragma once
#include"common.h"
void QueueInit(Queue* pq);
void QueueDestory(Queue* pq);
void QueuePush(Queue* pq,QDataType x);
void QueuePop(Queue* pq);
QDataType QueueFront(Queue* pq);
int QueueEmpty(Queue* pq);
int QueueSize(Queue* pq);
void QueuePrint(Queue* pq);
void QueueTest();
队列接口的实现Queue.c
#include"common.h"
#include"Queue.h"
//队列的初始化
void QueueInit(Queue* pq)
{
assert(pq);
pq->_head = (QueueNode*)malloc(sizeof(QueueNode));
assert(pq->_head);
pq->_tail = pq->_head;
pq->_head->_next = NULL;
}
void QueueDestory(Queue* pq)
{
assert(pq);
QueueNode* cur = pq->_head;
while (cur)
{
QueueNode* next = cur->_next;
free(cur);
cur = next;
}
}
void QueuePush(Queue* pq, QDataType x)
{
assert(pq);
QueueNode* newnode = (QueueNode*)malloc(sizeof(QueueNode));
assert(newnode);
newnode->_data = x;
newnode->_next = NULL;
pq->_tail->_next = newnode;
pq->_tail = newnode;
}
int QueueEmpty(Queue* pq)
{
return pq->_head != pq->_tail;
}
int QueueSize(Queue* pq)
{
int count = 0;
if (QueueEmpty(pq) == 0)
return 0;
else
{
QueueNode* cur = pq->_head->_next;
while (cur)
{
++count;
cur = cur->_next;
}
}
return count;
}
void QueuePop(Queue* pq)
{
QueueNode*cur = NULL;
assert(pq);
if (QueueEmpty(pq) == 0)
return;
cur = pq->_head->_next;//保存队头的第一个元素
if (pq->_tail == cur)//若队列中只有一个元素,让尾指针指向头指针
{
pq->_tail = pq->_head;
}
pq->_head->_next = cur->_next;//删除第一个元素
free(cur);
}
QDataType QueueFront(Queue* pq)
{
assert(pq);
return pq->_head->_next->_data;
}
void QueuePrint(Queue* pq)
{
assert(pq);
QueueNode* cur = pq->_head->_next;
while (cur)
{
printf("%d ", cur->_data);
cur = cur->_next;
}
printf("\n");
}
void QueueTest()
{
Queue q;
//QueueInit(&q);
//QueuePush(&q, 1);
//QueuePush(&q, 2);
//QueuePush(&q, 3);
////printf("%d\n",QueueFront(&q));
////QueuePop(&q);
//printf("%d\n", QueueEmpty(&q));
//printf("%d\n", QueueSize(&q));
//QueuePrint(&q);
QueueDestory(&q);
}
二叉树相关接口的实现BTree.h
#define _CRT_SECURE_NO_WARNINGS
#include"common.h"
#include"BTree.h"
#include"Queue.h"
//通过前序遍历的数组"ABD##E#H##CF##G##"构建二叉树
BTNode* BinaryTreeCreate(BTDatatype* array, int* pIndex)
{
if (array[(*pIndex)] == '#')
{
(*pIndex)++;
return NULL;
}
BTNode* root = (BTNode*)malloc(sizeof(BTNode));
root->_left = NULL;
root->_right = NULL;
root->_data = array[(*pIndex)];
(*pIndex)++;
root->_left = BinaryTreeCreate(array, pIndex);
root->_right = BinaryTreeCreate(array, pIndex);
return root;
}
void BinaryTreeDestory(BTNode** root)
{
}
int BinaryTreeSize(BTNode* root)
{
if (root == NULL)
return 0;
return BinaryTreeSize(root->_left) + BinaryTreeSize(root->_right) + 1;
}
//int BinaryTreeLeafSize(BTNode* root)
//{
// if (root == NULL)
// return 0;
// int LeftLeaf = 0;
// int RightLeaf = 0;
//
// if (root->_left == NULL && root->_right == NULL)
// return 1;
// LeftLeaf = BinaryTreeLeafSize(root->_left);
// RightLeaf = BinaryTreeLeafSize(root->_right);
//
// return LeftLeaf + RightLeaf;
//}
int BinaryTreeLeafSize(BTNode* root)
{
if (root == NULL)
return 0;
if (root->_left == NULL && root->_right == NULL)
return 1;
return BinaryTreeLeafSize(root->_left) + BinaryTreeLeafSize(root->_right);
}
int BinaryTreeLevelSize(BTNode* root, int k)
{
if (root == NULL)
return 0;
if (k == 1)
return 1;
return BinaryTreeLevelSize(root->_left, k - 1)
+ BinaryTreeLevelSize(root->_right, k - 1);
}
BTNode* BinaryTreeFind(BTNode* root, BTDatatype x)
{
BTNode* ret = NULL;
if (root == NULL)
return NULL;
if (root->_data == x)
return root;
ret = BinaryTreeFind(root->_left, x);
if (ret)
return ret;
ret = BinaryTreeFind(root->_right, x);
if (ret)
return ret;
return NULL;
}
//遍历
void BinaryTreePrevOrder(BTNode* root)
{
if (root == NULL)
return;
printf("%c ", root->_data);
BinaryTreePrevOrder(root->_left);
BinaryTreePrevOrder(root->_right);
}
void BinaryTreeInOrder(BTNode* root)
{
if (root == NULL)
return;
BinaryTreeInOrder(root->_left);
printf("%c ", root->_data);
BinaryTreeInOrder(root->_right);
}
void BinaryTreePostOrder(BTNode* root)
{
if (root == NULL)
return;
BinaryTreePostOrder(root->_left);
BinaryTreePostOrder(root->_right);
printf("%c ", root->_data);
}
//层序遍历
void BinaryTreeLevelOrder(BTNode* root)
{
Queue q;
QueueInit(&q);
if (root != NULL)
{
QueuePush(&q, root);
while (QueueEmpty(&q) != 0)
{
BTNode* front = QueueFront(&q);
printf("%c ", front->_data);
QueuePop(&q);
if (front->_left)
QueuePush(&q, front->_left);
if (front->_right)
QueuePush(&q, front->_right);
}
printf("\n");
}
}
//判断二叉树是否为完全二叉树
int BinaryTreeComplete(BTNode* root)
{
Queue q;
QueueInit(&q);
if (root != NULL)
{
QueuePush(&q, root);
while (QueueEmpty(&q) != 0)
{
BTNode* front = QueueFront(&q);
QueuePop(&q);
if (front == NULL)
break;
QueuePush(&q, front->_left);
QueuePush(&q, front->_right);
}
while (QueueEmpty(&q) != 0)
{
BTNode* front = QueueFront(&q);
if (front != NULL)
{
QueueDestory(&q);
return 0;
}
QueuePop(&q);
}
}
return 1;
}
void BTreeTest()
{
char array[100] = "ABD##E#H##CF##G##";
//构造二叉树
int Index = 0;
BTNode* tree = BinaryTreeCreate(array, &Index);
printf("%d\n", BinaryTreeSize(tree));
printf("%d\n", BinaryTreeLevelSize(tree,3));
printf("%d\n", BinaryTreeLeafSize(tree));
BinaryTreePrevOrder(tree);
printf("\n");
BinaryTreeInOrder(tree);
printf("\n");
BinaryTreePostOrder(tree);
printf("\n");
BinaryTreeLevelOrder(tree);
printf("%d\n", BinaryTreeComplete(tree));
}