判断一个树是否为二叉树需得满足一下两个条件:
1.每个结点的度都不大于2;
2.每个结点的孩子结点次序不能任意颠倒;
即一个二叉树中的每个结点只能含有0、1或2个孩子,而且每个孩子有左右之分。
下面来讨论一下二叉树的基本操作:
头文件BTNode.h
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include<assert.h>
#include<string.h>
#define MAX_SIZE 10
typedef char BTDataType;
typedef struct BinTreeNode
{
struct BinTreeNode* _Pleft;
struct BinTreeNode* _Pright;
BTDataType _data;
} BTNode, *PBTNode;
// 构建二叉树的结点
PBTNode BuyBinTreeNode(BTDataType data);
// 创建二叉树
void CreateBinTree(PBTNode* PRoot, const BTDataType* array, int size, BTDataType invalid);
// 拷贝二叉树
PBTNode CopyBinTree(PBTNode PRoot);
// 销毁二叉树
void DestroyBinTree(PBTNode *PRoot);
// 前序遍历递归
void PreOrder(PBTNode PRoot);
// 中序遍历递归
void InOrder(PBTNode PRoot);
// 后续遍历递归
void PostOrder(PBTNode PRoot);
// 二叉树的镜像递归
void MirrorBinTree(PBTNode PRoot);
// 求二叉树中结点的个数
int BinTreeSize(PBTNode PRoot);
// 获取二叉树中叶子结点的个数
int GetLeafCount(PBTNode PRoot);
// 求二叉树中K层结点的个数
int GetKLevelNode(PBTNode PRoot, int K);
// 求二叉树的高度
int Height(PBTNode PRoot);
功能模块BTNode.c
#include"BTNode.h"
PBTNode BuyBinTreeNode(BTDataType data) //构建二叉树的结点
{
PBTNode PNewNode = (PBTNode)malloc(sizeof(BTNode));
if (NULL == PNewNode)
{
assert(0);
return NULL;
}
PNewNode->_Pleft = NULL;
PNewNode->_Pright = NULL;
PNewNode->_data = data;
return PNewNode;
}
void _CreateBinTree(PBTNode* pRoot, const BTDataType* array, int size, int* index, BTDataType invalid) // 创建二叉树
{
if (array[*index] == invalid)
{
(*pRoot) = NULL;
return;
}
else if ((*index) >= size)
{
(*pRoot) = NULL;
return;
}
else
{
//创建根结点
(*pRoot) = BuyBinTreeNode(array[*index]);
//创建根结点的左子树
++(*index);
_CreateBinTree(&(*pRoot)->_Pleft, array, size, index, invalid);
//创建根结点的右子树
++(*index);
_CreateBinTree(&(*pRoot)->_Pright, array, size, index, invalid);
}
}
void CreateBinTree(PBTNode* pRoot, const BTDataType* array, int size, BTDataType invalid)
{
int index = 0;
assert(pRoot);
_CreateBinTree(pRoot, array, size, &index, invalid);
}
PBTNode CopyBinTree(PBTNode PRoot) //拷贝二叉树
{
PBTNode PNewRoot = NULL;
if (PRoot)
{
PNewRoot = BuyBinTreeNode(PRoot->_data);
//拷贝左子树
if (PRoot->_Pleft)
PNewRoot->_Pleft = CopyBinTree(PRoot->_Pleft);
//拷贝右子树
if (PRoot->_Pright)
PNewRoot->_Pright = CopyBinTree(PRoot->_Pright);
}
return PNewRoot;
}
void DestroyBinTree(PBTNode *PRoot) //销毁二叉树
{
assert(PRoot);
if (NULL == (*PRoot))
{
return;
}
DestroyBinTree(&(*PRoot)->_Pleft);
DestroyBinTree(&(*PRoot)->_Pright);
free(*PRoot);
(*PRoot) = NULL;
}
void PreOrder(PBTNode PRoot) //前序遍历递归
{
if (PRoot)
{
printf("%c", PRoot->_data);
PreOrder(PRoot->_Pleft);
PreOrder(PRoot->_Pright);
}
}
void InOrder(PBTNode PRoot) //中序遍历递归
{
if (PRoot)
{
PreOrder(PRoot->_Pleft);
printf("%c", PRoot->_data);
PreOrder(PRoot->_Pright);
}
}
void PostOrder(PBTNode PRoot) // 后序遍历递归
{
if (PRoot)
{
PreOrder(PRoot->_Pleft);
PreOrder(PRoot->_Pright);
printf("%c", PRoot->_data);
}
}
void MirrorBinTree(PBTNode PRoot) //二叉树的镜像递归
{
if (PRoot)
{
PBTNode PTemp = PRoot->_Pleft;
PRoot->_Pleft = PRoot->_Pright;
PRoot->_Pright = PTemp;
MirrorBinTree(PRoot->_Pleft);
MirrorBinTree(PRoot->_Pright);
}
}
int BinTreeSize(PBTNode PRoot) //求二叉树中结点的个数
{
if (NULL == PRoot)
return 0;
return BinTreeSize(PRoot->_Pleft) + BinTreeSize(PRoot->_Pright) + 1;
}
int GetLeafCount(PBTNode PRoot) //获取二叉树中叶子结点的个数
{
if (NULL == PRoot)
return 0;
if (NULL == PRoot->_Pleft&&NULL == PRoot->_Pright)
return 1;
return GetLeafCount(PRoot->_Pleft) + GetLeafCount(PRoot->_Pright);
}
int GetKLevelNode(PBTNode PRoot, int k) //求二叉树中K层结点的个数
{
if (NULL == PRoot || k < 1)
return 0;
if (1 == k)
return 1;
return GetKLevelNode(PRoot->_Pleft, k - 1) + GetKLevelNode(PRoot->_Pright, k - 1);
}
int Height(PBTNode PRoot) //求二叉树的高度
{
if (NULL == PRoot)
return 0;
int leftHeight = Height(PRoot->_Pleft);
int rightHeight = Height(PRoot->_Pright);
return leftHeight > rightHeight ? leftHeight + 1 : rightHeight + 1;
}
测试模块 test.c
#include"BTNode.h"
void TestBinTree()
{
const char* str = "ABD###CE##F";
PBTNode PRoot = NULL;
PBTNode PNewTree = NULL;
BTDataType invalid = '#';
CreateBinTree(&PRoot, str, strlen(str), invalid);
printf("PreOrder:");
PreOrder(PRoot);
printf("\n");
//LevelOrder(PRoot);
printf("\n");
PNewTree = CopyBinTree(PRoot);
printf("PreOrder:");
PreOrder(PNewTree);
printf("\n");
printf("二叉树结点的个数为:%d\n", BinTreeSize(PRoot));
printf("二叉树叶子结点的个数为:%d\n", GetLeafCount(PRoot));
printf("二叉树的高度为:%d\n", Height(PRoot));
DestroyBinTree(&PRoot);
DestroyBinTree(&PNewTree);
}
int main()
{
TestBinTree();
system("pause");
return 0;
}
以上就是二叉树的一些基本操作,有建议的大佬们欢迎沟通。