排序二叉树 SortBinaryTree
排序二叉樹 SortBinaryTree
??????????? 排序二叉樹是比較基本但是重要的算法,它在許多實際編碼中都不可缺少,還有不少算法和數據結構都基于此。比如,二叉查找樹,平衡二叉樹,紅黑樹等等。??
?????? SortBinaryTree的源代碼見: https://github.com/duankai/SortBinaryTree/
?????? 下面,介紹SortBinaryTree。
1.??????SortBinaryTree的節點結構:
struct TREE_NODE_T { AnyType * pcData; TREE_NODE_T<AnyType> * pstParent; TREE_NODE_T<AnyType> * pstLeftChild; TREE_NODE_T<AnyType> * pstRightChild; };2.??????構造函數
SortBinaryTree(int(* cmpFunc)(AnyType * d1, AnyType * d2)) {m_pstRoot = NULL;m_pstSmallestNode = NULL;m_cmpFunc = cmpFunc; };?其中,cmpFunc是AnyData的比較函數。
3.??????樹的插入
??????? 樹的插入采用遞歸的方法,首先尋找插入節點的合適位置,然后插入。
bool InsertTreeNode(TREE_NODE_T<AnyType> * pstNode, TREE_NODE_T<AnyType> * pstTempRoot){if (!pstNode){return false;}if (NULL != pstTempRoot){if (m_cmpFunc(pstNode->pcData ,pstTempRoot->pcData) > 0){if (NULL != pstTempRoot->pstRightChild){return InsertTreeNode(pstNode, pstTempRoot->pstRightChild);}else{pstTempRoot->pstRightChild = pstNode;pstNode->pstParent = pstTempRoot;}}if (m_cmpFunc(pstNode->pcData ,pstTempRoot->pcData) < 0){if (NULL != pstTempRoot->pstLeftChild){return InsertTreeNode(pstNode, pstTempRoot->pstLeftChild);}else{pstTempRoot->pstLeftChild = pstNode;pstNode->pstParent = pstTempRoot;}}if (m_cmpFunc(pstNode->pcData ,pstTempRoot->pcData) == 0){return false;}}else{m_pstRoot = pstNode;}return true;};
4.??????查找
TREE_NODE_T<AnyType> * SearchNode(void * pvData, TREE_NODE_T<AnyType> * pstRoot){if (NULL == pstRoot || NULL == pvData){return NULL;}if (m_cmpFunc(pstRoot->pcData ,(AnyType *)pvData) == 0){return pstRoot;}else if (m_cmpFunc((AnyType *)pvData, pstRoot->pcData) < 0){return SearchNode(pvData, pstRoot->pstLeftChild);}else if (m_cmpFunc((AnyType *)pvData, pstRoot->pcData) > 0){return SearchNode(pvData, pstRoot->pstRightChild);}else{return NULL;}};
5.??????刪除最小節點
??? 首先,用中序遍歷的方法得到最小的節點,然后刪除。
AnyType * DeleteSmallestNode(){GetSmallestNode(m_pstRoot);if (NULL == m_pstSmallestNode){return NULL;}AnyType * pcData = m_pstSmallestNode->pcData;if (m_pstSmallestNode == m_pstRoot){m_pstRoot = m_pstRoot->pstRightChild;}SetPointerThisNull(m_pstSmallestNode);free(m_pstSmallestNode);m_pstSmallestNode = NULL;return pcData;};
6.??????析構函數
???????? 析構函數先用后續遍歷節點,再對其進行刪除。
7.??????刪除節點
???????? 刪除節點的思路分為以下幾種情況。
???????? 1)刪除的是葉子節點,那么直接刪除。
???????? 2)刪除的節點左(右)子樹為空,則用右(左)子樹替代要刪除的節點。
???????? 3)刪除的節點左右子樹都不為空,則用中序方法尋找此節點的前驅,用前驅替代此節點并刪除。
bool DeleteTreeNode(void * pvData){if (NULL == pvData){return false;}TREE_NODE_T<AnyType> * pstTreeNode = SearchNode(pvData, GetRootNode());if (!pstTreeNode){return false;}if (pstTreeNode->pstLeftChild != NULL && pstTreeNode->pstRightChild != NULL){TREE_NODE_T<AnyType> * pstForeNode = GetForeNodeMiddleOrder(pstTreeNode);if (!pstForeNode){return false;}memcpy(pstTreeNode->pcData, pstForeNode->pcData, sizeof(AnyType));SetPointerThisNull(pstForeNode);free(pstForeNode);pstForeNode = NULL;return true;}if (pstTreeNode->pstLeftChild == NULL && pstTreeNode->pstRightChild != NULL){if (m_pstRoot == pstTreeNode){m_pstRoot = pstTreeNode->pstRightChild;pstTreeNode->pstRightChild->pstParent = NULL;}else{if (pstTreeNode == pstTreeNode->pstParent->pstLeftChild){pstTreeNode->pstParent->pstLeftChild = pstTreeNode->pstRightChild;}if (pstTreeNode == pstTreeNode->pstParent->pstRightChild){pstTreeNode->pstParent->pstRightChild = pstTreeNode->pstRightChild;}}}if (pstTreeNode->pstRightChild == NULL && pstTreeNode->pstLeftChild != NULL){if (m_pstRoot == pstTreeNode){m_pstRoot = pstTreeNode->pstLeftChild;pstTreeNode->pstLeftChild->pstParent = NULL;}else{if (pstTreeNode == pstTreeNode->pstParent->pstLeftChild){pstTreeNode->pstParent->pstLeftChild = pstTreeNode->pstLeftChild;}if (pstTreeNode == pstTreeNode->pstParent->pstRightChild){pstTreeNode->pstParent->pstRightChild = pstTreeNode->pstLeftChild;}}}SetPointerThisNull(pstTreeNode);free(pstTreeNode);pstTreeNode = NULL;return true;};
總結:
?????? 除了SortBinaryTree提供的以上方法,大家還可以在此基礎上進行適合自己項目的針對性擴展。由于做此排序二叉樹是在寫定時器時用來查找最先到時間的那個定時器,從而避免了對每個定時器進行掃描。因此,SortBinaryTree提供了一個刪除最小節點的方法。
?????? 后續,將繼續開源項目中抽象出的工具代碼,請大家關注github上的源代碼以及CSDN博客上的講解。同時,也歡迎各路高手留下您寶貴的意見和建議。
總結
以上是生活随笔為你收集整理的排序二叉树 SortBinaryTree的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 32位操作系统,为什么最大支持4G存储空
- 下一篇: java通过url读取网络图片