头文件binarytree.h:
#pragma once
//创建二叉树结构体
typedef int BTDataType;
typedef struct BinaryTreeNode{
struct BinaryTreeNode* left;
struct BinaryTreeNode* right;
BTDataType data;
}BTNode;
//二叉树节点的创建
BTNode* BuyBinaryTree(BTDataType data);
//二叉树的创建
BTNode* CreatBinaryTree(int array[], int size, int* index, int flag);
//二叉树的前序遍历
void PreOrder(BTNode* root);
//二叉树的中序遍历
void InOrder(BTNode* root);
//二叉树的后序遍历
void PostOrder(BTNode* root);
//二叉树节点个数
int BinaryTreeSize(BTNode* root);
//二叉树叶子节点的个数
int BinaryTreeLeafSize(BTNode* root);
//二叉树第k层节点个数
int BinaryTreeLevelKSize(BTNode* root, int k);
//二叉树的高度
int BinaryTreeHeight(BTNode* root);
//二叉树查找值为x的节点
BTNode* BinaryTreeFind(BTNode* root, BTDataType x);
//二叉树的销毁
void BinaryTreeDestory(BTNode** root);
实现文件binarytree.c:
#include <stdio.h>
#include <assert.h>
#include <malloc.h>
#include "binarytree.h"
//二叉树节点的创建
BTNode* BuyBinaryTree(BTDataType data) {
BTNode* newNode = (BTNode*)malloc(sizeof(BTNode));
if(newNode == NULL) {
assert(0);
return 0;
}
newNode->left = NULL;
newNode->right = NULL;
newNode->data = data;
return newNode;
}
//二叉树的创建
BTNode* _CreatBinaryTree(int array[], int size, int* index, int flag) {
if (size == 0) {
return NULL;
}
BTNode* newNode = NULL;
if (*index < size && array[*index] != flag) {
newNode = BuyBinaryTree(array[*index]);
(*index)++;
newNode->left = _CreatBinaryTree(array, size, index, flag);
(*index)++;
newNode->right = _CreatBinaryTree(array, size, index, flag);
}
return newNode;
}
//二叉树的前序遍历
void PreOrder(BTNode* root) {
if (root == NULL) {
return;
}
printf("%d ", root->data);
PreOrder(root->left);
PreOrder(root->right);
}
//二叉树的中序遍历
void InOrder(BTNode* root) {
if (root == NULL) {
return;
}
InOrder(root->left);
printf("%d ", root->data);
InOrder(root->right);
}
//二叉树的后序遍历
void PostOrder(BTNode* root) {
if (root == NULL) {
return;
}
PostOrder(root->left);
PostOrder(root->right);
printf("%d ", root->data);
}
//二叉树节点个数
int BinaryTreeSize(BTNode* root) {
if (root == NULL) {
return 0;
}
return 1 + BinaryTreeSize(root->left) + BinaryTreeSize(root->right);
}
//二叉树叶子节点的个数
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);
}
//二叉树第k层节点个数
//约定根节点为第一层
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);
}
//二叉树的高度
int BinaryTreeHeight(BTNode* root) {
if (root == NULL) {
return 0;
}
int left = BinaryTreeHeight(root->left);
int right = BinaryTreeHeight(root->right);
return left > right ? left + 1 : right + 1;
}
//二叉树查找值为x的节点
BTNode* BinaryTreeFind(BTNode* root, BTDataType x) {
if (root == NULL) {
return NULL;
}
if (root->data == x) {
return root;
}
BTNode* left = BinaryTreeFind(root->left, x);
if (left != NULL) {
return left;
}
return BinaryTreeFind(root->right, x);
}
//二叉树的销毁
void BinaryTreeDestory(BTNode** root) {
if (*root == NULL) {
return;
}
if (*root)
{
BinaryTreeDestory(&((*root)->left));
BinaryTreeDestory(&((*root)->right));
free(*root);
root = NULL;
}
}