日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

数据结构与算法之二叉搜索树

發(fā)布時間:2023/12/19 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 数据结构与算法之二叉搜索树 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

  與鏈表不同,樹是一種非線性的數(shù)據(jù)結(jié)構(gòu)。樹中最常用的是二叉樹,二叉樹限制了子樹的數(shù)量,也就是每個結(jié)點的子樹至多2個,并且這兩個子樹是有順序的。而二叉搜索樹(二叉查找樹,二叉排序樹)是指根節(jié)點的關(guān)鍵字大于左子樹的,而小于右子樹,并且,左右子樹也是一顆二叉搜索樹。也就是說中序遍歷一顆二叉搜索樹,它的輸出是從小到大排序好的。

  除了普通的二叉搜索樹之外,還有很多關(guān)于它的變形。

  二叉平衡搜索樹,即即是一顆二叉平衡樹,也是一顆搜索樹,平衡樹即任意一個結(jié)點的左子樹的高度與右子樹的高度之差的絕對值不大于1。

  紅黑樹,也是一種特殊的二叉查找樹,除了具備查找樹的特點,它的每個結(jié)點上還有一個記錄紅黑顏色的元素。

  這里我們主要討論一般的二叉搜索樹。

  二叉搜索樹,其實就是一顆特殊的樹,任何適用于樹的算法都適用于二叉搜索樹。當然它也是圖,任何適用于圖的算法也適用于二叉搜索樹。圖中的深度優(yōu)先搜索(DFS)其實就是樹中的前序遍歷,廣度優(yōu)先搜索(BFS)就是樹中的層序遍歷。當然圖中還有很多其他算法也都適用于樹。

1、二叉樹的數(shù)據(jù)結(jié)構(gòu)

typedef struct binSrcTreeNode{int element;struct binSrcTreeNode *pLeft;struct binSrcTreeNode *pRight; }BSTNode;

2、動態(tài)分配一個二叉樹的結(jié)點

BSTNode* allocNode(int element){BATNode *pNode = (BSTNode*)malloc(sizeof(BSTNode));if(NULL != pNode){pNode->element = element;pNode->pLeft = NULL;pNode->pRight = NULL;}return pNode; }

3、二叉搜索樹的查找

由于二叉搜索樹是已經(jīng)排序了的二叉樹,所以可以通過二分查找來搜索。先判斷是否等于根節(jié)點,如果大于根節(jié)點的關(guān)鍵字,搜索右子樹,小于根節(jié)點的關(guān)鍵字,搜索左子樹。

bool searchInBSTree(const BSTNode *pRoot,int element,BSTNode **pNode){if(NULL == pRoot)return false;*pNode = pRoot;if(pRoot->element == element){return true;}else if(pRoot->element > element)return searchInBSTree(pRoot->pLeft,element,pNode);elsereturn searchInBSTree(pRoot->pRight,element,pNode); }

4、二叉搜索樹的插入

首先查找二叉樹是是否已經(jīng)存在該元素,如果已經(jīng)存在返回false;如果不存在,插入該結(jié)點,插入元素大于根節(jié)點,則插入右子樹,否則,插入左子樹中。

bool insertIntoBSTree(BSTNode **pRoot,int element){if(NULL == pRoot)return false;if(NULL == *pRoot){*pRoot = allocNode(element);return NULL==*pRoot?false:true;}BSTNode **pSrc = NULL;if(searchInBSTree(pRoot,element,pSrc))return false;BSTNode *pNode = allocNode(element);if(element < *pSrc->element)*pSrc->pLeft = pNode;else*pSrc->pRight = pNode;return true; }

5、二叉搜索樹的刪除

二叉搜索樹的刪除分為三種情況:

(1) 刪除結(jié)點為葉子結(jié)點,直接刪除該結(jié)點,再修改其父結(jié)點的指針(注意分是根結(jié)點和不是根節(jié)點)。

(2) 刪除結(jié)點為單支結(jié)點(即只有左子樹或右子樹)。

(3) 刪除結(jié)點的左子樹和右子樹均不空。

bool deleteFromBSTree(BSTNode **pRoot,int element){if(NULL == pRoot || NULL == *pRoot)return false;BSTNode **pSrc = NULL;BSTNode **pParent = NULL;if(searchInBSTree(*pRoot,element,pParent,pSrc)){if(NULL == *pSrc->pLeft && NULL == *pSrc->pRight){free(*pSrc);*pSrc = NULL;return true;}else if(NULL == *pSrc->pLeft){if(*pSrc == *pParent->pLeft)*pParent->pLeft = *pSrc->pRight;else if(*pSrc == *pParent->pRight)*pParent->pRight = *pSrc->pRight;free(*pSrc);return true;}else if(NULL == *pSrc->pRight){if(*pSrc == *pParent->pLeft)*pParent->pLeft = *pSrc->pLeft;else if(*pSrc == *pParent->pRight)*pParent->pRight = *pSrc->pLeft;free(*pSrc);return true;}else{BSTNode *pNode = *pSrc;BSTNode *pChild = *pSrc->pLeft;while(pChild->pRight){pNode = pChild;pChild = pChild->pRight;}if(pNode == *pSrc) pNode->pLeft = pChild->pLeft;else pNode->pRight = pChild->pLeft;if(*pSrc == *pParent->pLeft) *pParent->pLeft = pChild;else *pParent->pRight = pChild;pChild->pLeft = *pSrc->pLeft;pChild->pRight = *pSrc->pRight;return true;}}return false; }

6、二叉搜索樹的前序中序后序遍歷

/* preOrder to traverse the binarySearchTree.*/ void preOrderBinarySearchTree(const BSTNode *pRoot){if(NULL == pRoot)return ;printf("%d ",pRoot->element);preOrderBinarySearchTree(pRoot->pLeft);preOrderBinarySearchTree(pRoot->pRight); }/* inOrder to traverse the binarySearchTree.*/ void inOrderBinarySearchTree(const BSTNode *pRoot){if(NULL == pRoot)return ;inOrderBinarySearchTree(pRoot->pLeft);printf("%d ",pRoot->element);inOrderBinarySearchTree(pRoot->pRight); }/* posOrder to traverse the binarySearchTree.*/ void posOrderBinarySearchTree(const BSTNode *pRoot){if(NULL == pRoot)return ;posOrderBinarySearchTree(pRoot->pLeft);posOrderBinarySearchTree(pRoot->pRight);printf("%d ",pRoot->element); }

?

7、釋放結(jié)點

void freeBinarySearchTree(BSTNode *pRoot){if(NULL == pRoot)return ;freeBinarySearchTree(pRoot->pLeft);freeBinarySearchTree(pRoot->pRight);free(pRoot); }

?

完整代碼詳見:https://github.com/whc2uestc/DataStructure-Algorithm/tree/master/tree

?

轉(zhuǎn)載于:https://www.cnblogs.com/whc-uestc/p/4638349.html

總結(jié)

以上是生活随笔為你收集整理的数据结构与算法之二叉搜索树的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。