【Data_Structure筆記12】尋找演算法之【基於二叉排序樹的尋找】__演算法

來源:互聯網
上載者:User
/*********************************************************************************************************************************檔案說明:        【1】實現【二叉排序樹的插入操作】【2】根據數組序列建立一顆二叉排序樹【3】在【二叉排序樹】中進行尋找開發環境:         Win10+VS2012時間地點:        陝西師範大學 文津樓 2017/8/29作    者:        九 月**********************************************************************************************************************************/#include<iostream>using namespace std;#define ARRAY_LENGHT 10typedef 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;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.