-指向BTree结构体的指针
* 返回值:树的深度
******************************************************************************/
int GetDepth(BTree *pBTree)
{
int depth = 0;
int leftDepth = 0;
int rightDepth = 0;
if (pBTree)
{
leftDepth = GetDepth(pBTree->lchild); // 获取左子树的深度
rightDepth = GetDepth(pBTree->rchild); // 获取右子树的深度
depth = leftDepth > rightDepth leftDepth + 1: rightDepth + 1;
}
return depth;
}
/*****************************************************************************
* 方法名:IsLeaf
* 描述: 判断该结点是否为叶子结点
* 参数: node--结点
* 返回值:1--表示叶子结点,0--表示非叶子结点
******************************************************************************/
int IsLeaf(BTree node)
{
if (node.lchild == NULL && node.rchild == NULL)
{
return 1;
}
return 0;
}
/*****************************************************************************
* 方法名:TraverseLeafNodes
* 描述: 遍历所有的叶子结点
* 参数: pBTree--指向BTree结构体的指针
******************************************************************************/
void TraverseLeafNodes(BTree *pBTree)
{
if (pBTree != NULL)
{
if (1 == IsLeaf(*pBTree))
{
printf("%c", pBTree->data);
}
else
{
TraverseLeafNodes(pBTree->lchild);
TraverseLeafNodes(pBTree->rchild);
}
}
}
//
// 判断一棵二叉树是否为平衡二叉树
// 平衡二叉树的定义: 如果任意节点的左右子树的深度相差不超过1,那这棵树就是平衡二叉树
// 算法思路:递归判断每个节点的左右子树的深度是否相差大于1,如果大于1,说明该二叉树不
// 是平衡二叉树,否则继续递归判断
int IsBalanceBinaryTree(BTree *pBTree)
{
int leftDepth = 0;
int rightDepth = 0;
int distance = 0;
if (pBTree != NULL)
{
leftDepth = GetDepth(pBTree->lchild); // 获取左子树的深度
rightDepth = GetDepth(pBTree->rchild); // 获取右子树的深度
distance = leftDepth > rightDepth leftDepth - rightDepth : rightDepth - leftDepth;
return distance > 1 0 : IsBalanceBinaryTree(pBTree->lchild) && IsBalanceBinaryTree(pBTree->rchild);
}
}
//
// 获取叶子结点的个数
int GetLeafCount(BTree *pBTree)
{
int count = 0;
if (pBTree != NULL)
{
if (IsLeaf(*pBTree))
{
count++;
}
else
{
count = GetLeafCount(pBTree->lchild) + GetLeafCount(pBTree->rchild);
}
}
return count;
}
//
// 获取度为1的结点的个数
int GetCountOfOneDegree(BTree *pBTree)
{
int count = 0;
if (pBTree != NULL)
{
if ((pBTree->lchild != NULL && pBTree->rchild == NULL) || (pBTree->lchild == NULL && pBTree->rchild != NULL))
{
count++;
}
count += GetCountOfOneDegree(pBTree->lchild) + GetCountOfOneDegree(pBTree->rchild);
}
return count;
}
//
// 获取度为2的结点的个数
int GetCountOfTwoDegree(BTree *pBTree)
{
int count = 0;
if (pBTree != NULL)
{
if (pBTree->lchild != NULL && pBTree->rchild != NULL)
{
count++;
}
count += GetCountOfTwoDegree(pBTree->lchild) + GetCountOfTwoDegree(pBTree->rchild);
}
return count;
}
//
// 获取二叉树的结点的总数
int GetNodesCount(BTree *pBTree)
{
int count = 0;
if (pBTree != NULL)
{
count++;
count += GetNodesCount(pBTree->lchild) + GetNodesCount(pBTree->rchild);
}
return count;
}
//
// 交换左右子树
void SwapLeftRightSubtree(BTree **pBTree)
{
BTree *tmp = NULL;
if (*pBTree != NULL)
{
// 交换当前结点的左右子树
tmp = (*pBTree)->lchild;
(*pBTree)->lchild = (*pBTree)->rchild;
(*pBTree)->rchild = tmp;
SwapLeftRightSubtree(&(*pBTree)->lchild);
SwapLeftRightSubtree(&(*pBTree)->rchild);
}
}
//
// 判断值e是否为二叉树中某个结点的值,返回其所在的层数,返回0表示不在树中
int GetLevelByValue(BTree *pBTree, ElemType e)
{
int leftDepth = 0;
int rightDepth = 0;
int level = 0;
if (pBTree->data == e)//这里的1是相对于以pBTree为根节点的层数值。
{
return 1;
}
i