一、Sysutil.h//系统头文件
#ifndef _SYSUTIL_H_
#define _SYSUTIL_H_
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<string.h>
#include<stdbool.h>
#pragma warning (disable:4996)
#include<vld.h> //内存泄漏工具的头文件
#include<windows.h>
#endif
二、Tree.h//二叉树实现函数的文件
#include "Sysutil.h"
typedef char BTDataType;
typedef struct BinaryTreeNode
{
BTDataType _data;
struct BinaryTreeNode* _left;
struct BinaryTreeNode* _right;
}BTNode;
// 通过前序遍历的数组"ABD##E#H##CF##G##"构建二叉树
BTNode* BinaryTreeCreate(BTDataType* a, int n, int* pi);
// 二叉树销毁
void BinaryTreeDestory(BTNode** root);
// 二叉树节点个数
int BinaryTreeSize(BTNode* root);
// 二叉树叶子节点个数
int BinaryTreeLeafSize(BTNode* root);
// 二叉树第k层节点个数
int BinaryTreeLevelKSize(BTNode* root, int k);
// 二叉树查找值为x的节点
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);
BTNode* BinaryTreeCreate(BTDataType* a, int n, int* pi)
{
while (n>0)
{
if (a[*pi] == '#' || a[*pi] == '\0')
{
return NULL;
}
else
{
BTNode *t = (BTNode*)malloc(sizeof(BTNode));
assert(t != NULL);
t->_data = a[*pi];
(*pi)++;
n--;
t->_left = BinaryTreeCreate(a, n, pi);
(*pi)++;
n--;
t->_right = BinaryTreeCreate(a,n, pi);
return t;
}
}
}
void BinaryTreeDestory(BTNode** root)
{
if ((*root)!=NULL)
{
BinaryTreeDestory(&((*root)->_left));
BinaryTreeDestory(&((*root)->_right));
free(*root);
*root=NULL;
}
}
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;
else{
if (root->_left == NULL&&root->_right == NULL)
return 1;
return BinaryTreeLeafSize(root->_left) + BinaryTreeLeafSize(root->_right);
}
}
int BinaryTreeLevelKSize(BTNode* root, int k)
{
if (root == NULL||k<1)
return 0;
if (k == 1)
return 1;
return (BinaryTreeLevelKSize(root->_left,k-1) + BinaryTreeLevelKSize(root->_right,k-1));
}
BTNode* BinaryTreeFind(BTNode* root, BTDataType x)
{
BTNode* p;
if (root == NULL || root->_data == x)
return root;
p = BinaryTreeFind(root->_left, x);
if (p != NULL) //说明在左树找到了
return p;
return BinaryTreeFind(root->_right, x);
}
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);
}
}
#include"Queue.h"
void BinaryTreeLevelOrder(BTNode* root)
{
if (root != NULL)
{
LinkQueue Q;
LinkQueueInit(&Q);
LinkQueueEn(&Q, root);
while (!LinkQueueEmpty(&Q))
{
BTNode *p = LinkQueueFront(&Q);
LinkQueueDe(&Q);
printf("%c ", p->_data);
if (p->_left != NULL)
LinkQueueEn(&Q, p->_left);
if (p->_right != NULL)
LinkQueueEn(&Q, p->_right);
}
}
}
#include"Queue.h"
int BinaryTreeComplete(BTNode* root)
{
LinkQueue Q;
BTNode* cur;
int tag = 0;
LinkQueueInit(&Q);
LinkQueueEn(&Q, root);
while (!LinkQueueEmpty(&Q))
{
cur = LinkQueueFront(&Q);
//有右子树没有左子树则不是完全二叉树
if (cur->_right&& !cur->_left){
return 0;
}
if (cur->_left){
LinkQueueEn(&Q, cur->_left);
}
if (tag && (cur->_left || cur->_right)){
return 0;
}
if (cur->_right){
LinkQueueEn(&Q, cur->_right);
}
else {
//若该节点没有右子树就将该标签置为1
tag = 1;
}
LinkQueueDe(&Q);
}
}
三、Queue.h//队列实现函数,这里是为了在层次访问的时候使用。
#ifndef _QUEUE_H_
#define _QUEUE_H_
#include"Sysutil.h"
struct BTNode;
#define QueueElemType struct BTNode*
//链队列
typedef struct LinkQueueNode
{
QueueElemType data;
struct LinkQueueNode *next;
}LinkQueueNode;
typedef struct LinkQueue
{
LinkQueueNode *head;
LinkQueueNode *tail; //指向队尾节点
}LinkQueue;
void LinkQueueInit(LinkQueue *pq);
void LinkQueueDestroy(LinkQueue *pq);
void LinkQueueEn(LinkQueue *pq, QueueElemType x);
void LinkQueueDe(LinkQueue *pq);
void LinkQueueShow(LinkQueue *pq);
QueueElemType LinkQueueBack(LinkQueue *pq);
QueueElemType LinkQueueFront(LinkQueue *pq);
bool LinkQueueEmpty(LinkQueue *pq)
{
return pq->head == NULL;
}
void LinkQueueInit(LinkQueue *pq)
{
assert(pq);
pq->head = pq->tail = NULL;
}
void LinkQueueEn(LinkQueue *pq, QueueElemType x)
{
assert(pq);
LinkQueueNode *s = (LinkQueueNode *)malloc(sizeof(LinkQueueNode));
assert(s != NULL);
s->data = x;
s->next = NULL;
if (pq->head == NULL)
pq->head = pq->tail = s;
else
{
pq->tail->next = s;
pq->tail = s;
}
}
void LinkQueueDe(LinkQueue *pq)
{
assert(pq);
if (pq->head != NULL)
{
LinkQueueNode *p = pq->head;
pq->head = p->next;
if (pq->head == NULL)
pq->tail = NULL;
free(p);
}
}
void LinkQueueShow(LinkQueue *pq)
{
assert(pq != NULL);
LinkQueueNode *p = pq->head;
while (p != NULL)
{
printf("%d<--", p->data);
p = p->next;
}
printf("Nil.\n");
}
QueueElemType LinkQueueBack(LinkQueue *pq)
{
assert(pq && pq->head);
return pq->tail->data;
}
QueueElemType LinkQueueFront(LinkQueue *pq)
{
assert(pq && pq->head);
return pq->head->data;
}
void LinkQueueDestroy(LinkQueue *pq)
{
assert(pq);
while (pq->head != NULL)
{
LinkQueueNode *p = pq->head;
pq->head = p->next;
free(p);
}
pq->head = pq->tail = NULL;
}
#endif
四、TestMain.c//主函数
#include "Tree.h"
int main()
{
char *str = "ABD##E#H##CF##G##";
int index = 0;
int len = strlen(str);
BTNode* bt=BinaryTreeCreate(str, len, &index);
int size = BinaryTreeSize(bt);
printf("%d\n", size);
int leafsize = BinaryTreeLeafSize(bt);
printf("%d\n", leafsize);
int kNum=BinaryTreeLevelKSize(bt, 3);
printf("%d\n", kNum);
BTNode *s=BinaryTreeFind(bt, 'A');
printf("%c\n", s->_data);
printf("vlr:");
BinaryTreePrevOrder(bt);
printf("\n");
printf("lvr:");
BinaryTreeInOrder(bt);
printf("\n");
printf("lrv:");
BinaryTreePostOrder(bt);
printf("\n");
printf("level:");
BinaryTreeLevelOrder(bt);
printf("\n");
int JC=BinaryTreeComplete(bt);
printf("%d\n", JC);
BinaryTreeDestory(&bt);
system("pause");
return 0;
}