/*********************************************************************************************************************************
文件说明:
【1】实现【二叉排序树的插入操作】
【2】根据数组序列创建一颗二叉排序树
【3】在【二叉排序树】中进行查找
开发环境:
Win10+VS2012
时间地点:
陕西师范大学 文津楼 2017/8/29
作 者:
九 月
**********************************************************************************************************************************/
#include<iostream>
using namespace std;
#define ARRAY_LENGHT 10
typedef int ElemType;
ElemType srcData[ARRAY_LENGHT] = {111,9,8,7,6,5,4,3,2,1};
/*********************************************************************************************************************************
模块说明:
首先定义二叉排序树种的【树节点】
**********************************************************************************************************************************/
typedef struct BSTNode
{
ElemType keyData;
struct BSTNode *lChild;
struct BSTNode *rChild;
}BSTNode,*BSTRootNodePointer;
/*********************************************************************************************************************************
函数原型:
template<typename DataType>void InsertBSTree(BSTRootNodePointer& pRootNode,DataType insertData)
函数说明:
在二叉排序树中插入一个结点
**********************************************************************************************************************************/
template<typename DataType>void InsertBSTree(BSTRootNodePointer& pRootNode,DataType insertData)
{
BSTNode* pInsertDataNode = NULL;
BSTNode* pParent = NULL;
BSTNode* pHead = NULL;
//【1】首先,为准备插入的数据申请一个【树节点】的内存空间
pInsertDataNode = (BSTNode*)std::malloc(sizeof(BSTNode));
if(!pInsertDataNode)
{
std::cout<<"【NOTICE】申请内存出现错误,请检查程序!"<<std::endl;
std::system("pause");
std::exit(1);
}
pInsertDataNode->keyData = insertData; //【1】插入的结点的最终位置都在叶节点
pInsertDataNode->lChild = NULL;
pInsertDataNode->rChild = NULL;
pHead = pRootNode;
while(pHead) //【2】根节点不为空,则进入判断
{
pParent = pHead;
if(insertData<pHead->keyData)
{
pHead = pHead->lChild;
}
else
{
pHead = pHead->rChild;
}
}
//【2】判断结点应该插入到二叉树结点的左子树上还是右子树上
if(insertData<pParent->keyData)
{
pParent->lChild = pInsertDataNode;
}
else
{
pParent->rChild = pInsertDataNode;
}
}
/*********************************************************************************************************************************
函数原型:
template<typename DataType>void CreatBSTree(BSTRootNodePointer& pRootNode,DataType srcDdata,int iLength)
函数说明:
根据输入的数据数组,创建一棵二叉排序树
**********************************************************************************************************************************/
template<typename DataType>void CreatBSTree(BSTRootNodePointer& pRootNode,DataType srcData,int iLength)
{
pRootNode = (BSTRootNodePointer)std::malloc(sizeof(BSTNode)); //【1】创建一个根节点
if(!pRootNode)
{
std::cout<<"【NOTICE】程序在申请内存的时候,出现错误,请检查程序!"<<std::endl;
std::system("pause");
std::exit(1);
}
pRootNode->keyData = srcData[0];
pRootNode->lChild = NULL;
pRootNode->rChild = NULL;
for(int i=1;i<iLength;i++)
{
InsertBSTree<ElemType>(pRootNode,srcData[i]);
}
}
/*********************************************************************************************************************************
函数原型:
void BSTreeInOrder(BSTRootNodePointer pRootNode)
函数说明:
二叉排序树的【中序遍历】
**********************************************************************************************************************************/
void BSTreeInOrder(BSTRootNodePointer pRootNode)
{
BSTNode* pRoot = NULL;
pRoot = pRootNode;
if(pRoot)
{
BSTreeInOrder(pRoot->lChild);
std::cout<<pRoot->keyData<<" ";
BSTreeInOrder(pRoot->rChild);
}
return;
}
/*********************************************************************************************************************************
函数原型:
template<typename DataType>BSTNode* SearchBSTree(BSTRootNodePointer pBSTRoot,DataType searchData)
函数说明:
在【二叉排序树】中进行查找
**********************************************************************************************************************************/
template<typename DataType>BSTNode* SearchBSTree(BSTRootNodePointer pBSTRoot,DataType searchData)
{
BSTNode* pRootTemp = NULL;
pRootTemp = pBSTRoot;
if(!pRootTemp||pRootTemp->keyData == searchData)
{
return pRootTemp;
}
else if(searchData<pRootTemp->keyData)
{
return (SearchBSTree<DataType>(pRootTemp->lChild,searchData));
}
else
{
return (SearchBSTree<DataType>(pRootTemp->rChild,searchData));
}
}
/*********************************************************************************************************************************
模块说明:
控制台应用程序的入口点
**********************************************************************************************************************************/
int main(int argc,char* argv[])
{
std::cout<<"======================================【二叉排序树之前的数据序列】===================================="<<std::endl;
for(int i=0;i<ARRAY_LENGHT;i++)
{
std::cout<<srcData[i]<<" ";
}
std::cout<<std::endl;
BSTRootNodePointer pBSTreeRoot = NULL;
CreatBSTree(pBSTreeRoot,srcData,ARRAY_LENGHT);
std::cout<<"======================================【二叉排序树之前的数据序列】===================================="<<std::endl;
BSTreeInOrder(pBSTreeRoot);
std::cout<<std::endl;
BSTNode* pSearchData = NULL;
pSearchData = SearchBSTree(pBSTreeRoot,111);
if(pSearchData)
{
std::cout<<"【NOTICE】查找成功 = "<<pSearchData->keyData<<std::endl;
}
else
{
std::cout<<"【NOTICE】查找失败!"<<std::endl;
}
std::system("pause");
return 0;
}
【Data_Structure笔记12】查找算法之【基于二叉排序树的查找】
最新推荐文章于 2019-09-30 19:24:43 发布