您的位置:首页 > 其它

【Data_Structure笔记13】【树】的相关操作

2017-08-31 17:26 381 查看
/*********************************************************************************************************************************
文件说明:
【树与二叉树】
二叉树的概念:
【1】【二叉树】是另一种【树形结构】,其特点是【每个结点】至多只有【两棵子树】(即二叉树中不存在度大于2的结点)。
【2】【二叉树的子树】有【左右之分】,其次序不能任意颠倒。
与【树】相似,【二叉树】也以递归的形式定义。【二叉树】是n(n>=0)个结点的有限集合:
【1】或者为空二叉树,即n=0;
【2】或者由一个【根节点】和【两个互不相交】的被称为【根】的【左子树】和【右子树】组成。
【3】左子树和右子树又分别是一棵二叉树
几种特殊的二叉树:
【1】满二叉树
【2】完全二叉树
【3】二叉排序树
二叉排序树的存储结构:
【1】顺序存储结构
【2】链式存储结构
开发环境:
Win10+VS2012
时间地点:
陕西师范大学 文津楼 2017/8/31
作    者:
九 月
**********************************************************************************************************************************/
#include<iostream>

using namespace std;

#define ARRAY_LENGTH 15

typedef int ElemType;

ElemType srcData[ARRAY_LENGTH] = {100,90,30,40,10,20,50,60,80,70,110,130,120,140,150};
/*********************************************************************************************************************************
模块说明:
定义【二叉排序树】的【结点类型】
**********************************************************************************************************************************/
typedef struct BSTNode
{
ElemType           keyData;                                  //【1】数据域
struct BSTNode*   lChild;                                   //【2】左右孩子指针
struct BSTNode*   rChild;
}BSTNode,*BSTRoot;
/*********************************************************************************************************************************
函数原型:
template<typename InsertDataType>void InsertBSTree(BSTRoot& pRoot,InsertDataType insrtData)
函数说明:
二叉排序树中插入一个新的结点
**********************************************************************************************************************************/
template<typename InsertDataType>void InsertBSTree(BSTRoot& pRoot,InsertDataType insrtData)
{
if(pRoot==NULL)
{
pRoot = (BSTRoot)std::malloc(sizeof(BSTNode));
if(!pRoot)
{
std::cout<<"【NOTICE】成功为根节点分配了内存空间!"<<std::endl;
}
pRoot->keyData = insrtData;
pRoot->lChild  = NULL;
pRoot->rChild  = NULL;
}
else
{
BSTNode* pInsertNode = (BSTNode*)std::malloc(sizeof(BSTNode));
BSTNode* pParentNode = NULL;
BSTRoot  pHead       = NULL;

pInsertNode->keyData = insrtData;
pInsertNode->lChild  = NULL;
pInsertNode->rChild  = NULL;

pHead = pRoot;

while (pHead)
{
pParentNode = pHead;
if(insrtData<pHead->keyData)
{
pHead = pHead->lChild;
}
else
{
pHead = pHead->rChild;
}
}//while

if(insrtData<pParentNode->keyData)
{
pParentNode->lChild = pInsertNode;
}
else
{
pParentNode->rChild = pInsertNode;
}//if
}//if...else
}
/*********************************************************************************************************************************
函数原型:
template<typename DataType>void CreatBSTree(BSTRoot& pRoot,DataType srcData[],int iLength)
函数说明:
根据输入的根节点指针和输入数组构建一棵二叉排序树
**********************************************************************************************************************************/
template<typename DataType>void CreatBSTree(BSTRoot& pRoot,DataType srcData[],int iLength)
{
if(!pRoot)
{
pRoot = (BSTRoot)std::malloc(sizeof(BSTNode));
if(!pRoot)
{
std::cout<<"【NOTICE】内存申请失败,请重新调试程序!"<<std::endl;
std::system("pause");
std::exit(1);
}
pRoot->keyData = srcData[0];
pRoot->lChild  = NULL;
pRoot->rChild  = NULL;
}

for(int i=1;i<iLength;i++)
{
InsertBSTree<DataType>(pRoot,srcData[i]);
}

}
/*********************************************************************************************************************************
函数原型:
【1】先序遍历:void PreOrder(BSTRoot pRoot)
【2】中序遍历:void InOrder(BSTRoot pRoot)
【3】后序遍历:void PostOrder(BSTRoot pRoot)
函数说明:
【二叉排序树】的:
【1】中序遍历---LNR
【2】先序遍历---NLR
【3】后序遍历---LRN
**********************************************************************************************************************************/
void InOrder(BSTRoot pRoot)
{
if(pRoot)
{
InOrder(pRoot->lChild);
std::cout<<pRoot->keyData<<"  ";
InOrder(pRoot->rChild);
}
}
void PreOrder(BSTRoot pRoot)
{
if(pRoot)
{
std::cout<<pRoot->keyData<<"  ";
PreOrder(pRoot->lChild);
PreOrder(pRoot->rChild);
}
}
void PostOrder(BSTRoot pRoot)
{
if(pRoot)
{
PostOrder(pRoot->lChild);
PostOrder(pRoot->rChild);
std::cout<<pRoot->keyData<<"  ";
}
}
/*********************************************************************************************************************************
函数原型:
void PrintLeafNode(BSTRoot pRoot)
函数说明:
输出【二叉排序树】中的【叶子结点】
**********************************************************************************************************************************/
void PrintLeafNode(BSTRoot pRoot)
{
if(pRoot)
{
PrintLeafNode(pRoot->lChild);
if((pRoot->lChild==NULL)&&(pRoot->rChild==NULL))
{
std::cout<<pRoot->keyData<<"  ";
}
PrintLeafNode(pRoot->rChild);
}
}
/*********************************************************************************************************************************
函数原型:
void LeafCount(BSTRoot pRoot,int& iLeafCount)
函数说明:
统计叶子结点的数目
**********************************************************************************************************************************/
void LeafCount(BSTRoot pRoot,int& iLeafCount)
{
if(pRoot)
{
LeafCount(pRoot->lChild,iLeafCount);
if((pRoot->lChild==NULL)&&(pRoot->rChild==NULL))
{
iLeafCount++;
}
LeafCount(pRoot->rChild,iLeafCount);
}
}
/*********************************************************************************************************************************
函数原型:
int BSTreeDepth(BSTRoot pRoot)
函数说明:
求解二叉树的高度
**********************************************************************************************************************************/
int BSTreeDepth(BSTRoot pRoot)
{
int iLeftTHeight = 0;
int iRightTHeigth= 0;
int iTreeHeight  = 0;
if(pRoot)
{
iLeftTHeight = BSTreeDepth(pRoot->lChild);
iRightTHeigth= BSTreeDepth(pRoot->rChild);
iTreeHeight = iLeftTHeight>iRightTHeigth?iLeftTHeight:iRightTHeigth;

return(iTreeHeight+1);
}
else
{
return 0;
}
}
/*********************************************************************************************************************************
函数原型:
int BSTreeDepth(BSTRoot pRoot)
函数说明:
打印二叉树
**********************************************************************************************************************************/
void PrintTree(BSTRoot pRoot,int iLayerNum)
{
if(!pRoot)
{
return;
}
PrintTree(pRoot->rChild,iLayerNum+1);
for(int i=0;i<iLayerNum;i++)
{
std::cout<<"  ";
}
std::cout<<pRoot->keyData<<std::endl;
PrintTree(pRoot->lChild,iLayerNum+1);
}
/******************************************************【Main函数】***************************************************************
模块说明:
控制台应用程序的入口
**********************************************************************************************************************************/
int main(int argc,char* argv[])
{
std::cout<<"==========================================【排序之前的数据】============================================"<<std::endl;
for(int i=0;i<ARRAY_LENGTH;i++)
{
std::cout<<srcData[i]<<"  ";
}
std::cout<<std::endl;

BSTRoot pRoot = NULL;
CreatBSTree<ElemType>(pRoot,srcData,ARRAY_LENGTH);
std::cout<<"==========================================【排序之后的数据】============================================"<<std::endl;
InOrder(pRoot);
std::cout<<std::endl;
std::cout<<"============================================【输出叶子结点】============================================"<<std::endl;
PrintLeafNode(pRoot);
std::cout<<std::endl;
std::cout<<"===========================================【叶子结点的数目】============================================"<<std::endl;
int iLeafCount = 0;
LeafCount(pRoot,iLeafCount);
std::cout<<"【NOTICE】叶子结点的数目 = "<<iLeafCount<<std::endl;
std::cout<<"===============================================【树的高度】============================================"<<std::endl;
int iTreeHight = 0;
iTreeHight = BSTreeDepth(pRoot);
std::cout<<"【NOTICE】二叉树的高度 = "<<iTreeHight<<std::endl;
std::system("pause");
return 0;
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: