1.分析顺序存储二叉树的优缺点
顺序结构存储就是使用数组来存储,一般使用数组只适合表示完全二叉树,因为不是完全二叉树会有空间的浪费。而现实中使用中只有堆才会使用数组来存储。二叉树顺序存储在物理上是一个数组,在逻辑上是一颗二叉树。
普通的二叉树是不适合用数组来存储的,因为可能会存在大量的空间浪费。而完全二叉树更适合使用顺序结构存储。现实中我们通常把堆(一种二叉树)使用顺序结构的数组来存储,需要注意的是这里的堆和操作系统虚拟进程地址空间中的堆是两回事,一个是数据结构,一个是操作系统中管理内存的一块区域分段
头文件
#pragma once
typedef char BTDataType;
typedef struct BTNode
{
struct BTNode * _pLeft;
struct BTNode * _pRight;
BTDataType _data;
}BTNode;
// 二叉树的创建
BTNode* CreateBinTree(BTDataType* array, int size, BTDataType invalid);
// 二叉树的拷贝
BTNode* CopyBinTree(BTNode* pRoot);
// 二叉树的销毁
void DestroyBinTree(BTNode** pRoot);
// 递归:前序遍历
void PreOrder(BTNode* pRoot);
void BinaryTreeSwap(BTNode** pleft, BTNode ** pright);
// 递归:中序遍历
void InOrder(BTNode* pRoot);
// 递归:后序遍历
void PostOrder(BTNode* pRoot);
// 层序遍历
void LevelOrder(BTNode* pRoot);
// 获取二叉树中节点个数
int GetBinTreeSize(BTNode* pRoot);
// 获取二叉树中第K层节点个数
int GetKLevelNodeCount(BTNode* pRoot, int K);
// 获取二叉树中叶子节点个数
int GetLeafCount(BTNode* pRoot);
// 获取二叉树深度(高度)
int GetBinTreeHeight(BTNode* pRoot);
// 检测值为x的元素是否在二叉树中,在返回该节点的地址,否则返回NULL
BTNode* BinaryTreeFind(BTNode* root, BTDataType x);
// 二叉树的镜像
void Mirror(BTNode* pRoot);
// 判断二叉树是否是完全二叉树
int BinaryTreeComplete(BTNode* root);
#include <stdio.h>
#include "0526.h"
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include "0515.h"
BTNode* BuyNewNode1(BTDataType data)
{
BTNode* root = (BTNode*)malloc(sizeof(BTNode));
if (root == NULL)
{
assert(0);
return NULL;
}
root->_data = data;
root->_pLeft = NULL;
root->_pRight = NULL;
return root;
}
// 二叉树的创建
BTNode * _CreateBinTree(BTDataType * array, int size, int* index,BTDataType invalid)
{
BTNode* pRoot = NULL;
if ((*index) < size && invalid != array[*index] )
{
pRoot = BuyNewNode1(array[*index]);
//创建左子树;
++(*index);
pRoot->_pLeft = _CreateBinTree(array, size, index,invalid);
//创建右子树
++(*index);
pRoot->_pRight = _CreateBinTree(array, size, index,invalid);
}
return pRoot;
}
BTNode* CreateBinTree(BTDataType* array, int size, BTDataType invalid) {
int index = 0;
return _CreateBinTree(array, size, &index, invalid);
}
// 二叉树的拷贝
BTNode* CopyBinTree(BTNode* pRoot)
{
BTNode* newpRoot = NULL;
if (pRoot == NULL)
return NULL;
newpRoot = BuyBinTreeNode(pRoot->_data);
newpRoot->_pLeft = CopyBinTree(pRoot->_pLeft);
newpRoot->_pRight = CopyBinTree(pRoot->_pRight);
return newpRoot;
}
// 二叉树的销毁
void DestroyBinTree(BTNode** pRoot)
{
if (*pRoot)
{
//递归删除左子树
DestroyBinTree(&((*pRoot)->_pLeft));
//递归删除右子树
DestroyBinTree(&((*pRoot)->_pRight));
free(*pRoot);
*pRoot = NULL;
}
}
// 递归:前序遍历
void PreOrder(BTNode* pRoot)
{
if (pRoot)
{
printf("%c ", pRoot->_data);
PreOrder(pRoot->_pLeft);
PreOrder(pRoot->_pRight);
}
}
// 递归:中序遍历
void InOrder(BTNode* pRoot)
{
if (pRoot)
{
InOrder(pRoot->_pLeft);
printf("%c ", pRoot->_data);
InOrder(pRoot->_pRight);
}
}
// 递归:后序遍历
void PostOrder(BTNode* pRoot)
{
if (pRoot)
{
PostOrder(pRoot->_pLeft);
PostOrder(pRoot->_pRight);
printf("%c ", pRoot->_data);
}
}
// 层序遍历
void LevelOrder(BTNode* pRoot)
{
if(pRoot == NULL)
{
return NULL;
}
Queue q;
QueueInit(&q);
QueuePush(&q, pRoot);
while(!QueueEmpty(&q))
{
pRoot = QueueFront(&q);
QueuePop(&q);
printf("%c ", pRoot->_data);
if(pRoot->_pLeft!=NULL)
QueuePush(&q, pRoot->_pLeft);
if(pRoot->_pRight!=NULL)
QueuePush(&q, pRoot->_pRight);
}
QueueDestroy(&q);
}
// 获取二叉树中节点个数
int GetBinTreeSize(BTNode* pRoot)
{
if (pRoot)
return GetBinTreeSize(pRoot->_pLeft) + GetBinTreeSize(pRoot->_pRight) + 1;
else
return 0;
}
// 获取二叉树中第K层节点个数
int GetKLevelNodeCount(BTNode* pRoot, int K)
{
if (pRoot == NULL || K <= 0)
{
return 0;
}
if (K == 1)
{
return 1;
}
//此处不能用--K,否则left的--k会影响到right的k
//因此,此处应该用k-1
return GetKLevelNodeCount(pRoot->_pLeft, K-1) + GetKLevelNodeCount(pRoot->_pRight, K-1) ;
}
// 获取二叉树中叶子节点个数
int GetLeafCount(BTNode* pRoot)
{
if (pRoot == NULL)
{
return 0;
}
if (pRoot->_pLeft == NULL && pRoot->_pRight == NULL)
{
return 1;
}
return GetLeafCount(pRoot->_pLeft) + GetLeafCount(pRoot->_pRight);
}
// 获取二叉树深度(高度)
int GetBinTreeHeight(BTNode* pRoot)
{
if (pRoot == NULL)
{
return 0;
}
int height = 1;
while (pRoot->_pLeft || pRoot->_pRight)
{
height++;
if (pRoot->_pLeft)
pRoot = pRoot->_pLeft;
if(pRoot->_pRight)
pRoot = pRoot->_pRight;
}
return height;
}
// 检测值为x的元素是否在二叉树中,在返回该节点的地址,否则返回NULL
BTNode* BinaryTreeFind(BTNode* root, BTDataType x)
{
if (root == NULL)
{
return NULL;
}
if(root->_data == x)
{
return root;
}
BTNode* tmp = NULL;
//递归从左子树往下找
if (tmp = BinaryTreeFind(root->_pLeft, x))
{
return tmp;
}
//左子树从右子树找,右子树找到直接返回
return BinaryTreeFind(root->_pRight, x);
}
void BinaryTreeSwap(BTNode** pleft, BTNode ** pright)
{
BTNode * tmpNode = *pleft;
*pleft = *pright;
*pright = tmpNode;
}
// 二叉树的镜像
void Mirror(BTNode* pRoot)
{
if (pRoot == NULL)
{
return NULL;
}
BinaryTreeSwap(pRoot->_pLeft, pRoot->_pRight);
Mirror(pRoot->_pLeft);
Mirror(pRoot->_pRight);
}
// 判断二叉树是否是完全二叉树,需要借助队列来判断
int BinaryTreeComplete(BTNode* root)
{
if (root == NULL)
{
return 1;
}
Queue q;
QueueInit(&q);
QueuePush(&q, root);
while(root != NULL)
{
root = QueueFront(&q);
QueuePop(&q);
if (root != NULL)
{
QueuePush(&q, root->_pLeft);
QueuePush(&q, root->_pRight);
}
}
while(!QueueEmpty(&q))
{
root = QueueFront(&q);
QueuePop(&q);
if (root == NULL)
{
return -1;
}
}
return 1;
}
int main()
{
char* arr = "ABD###CE##F";
//int index = 0;
BTNode *root = CreateBinTree(arr, strlen(arr),'#');
LevelOrder(root);
int tmp = GetKLevelNodeCount(root, 1);
int tmp2 = GetLeafCount(root);
int height = GetBinTreeHeight(root);
printf("%d", height);
PreOrder(root);
return 0;
}