关于C++类库KYLib: 用C语言实现平衡二叉树(AVL tree)的源码

平衡二叉树(AVL tree)调整算法请参见我的博文: <关于平衡二叉树(AVL tree)旋转后平衡标志调整的计算公式>

若要在 C++ 中使用则只要将 KYAVLTreeC.c 改为 KYAVLTreeC.cpp 即可。

 

用C语言实现平衡二叉树(AVL tree)头文件如下:

// ======================================= // Unit : AVL tree (KYAVLTreeC.h) // Version: 2.1.0.0 (build 2011.03.04) // Author : Kyee Ye // Email : kyee_ye(at)126.com // Copyright (C) Kyee workroom // ======================================= #ifndef _KYAVLTreeC_H_ #define _KYAVLTreeC_H_ // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /* 类型定义 */ // 布尔类型 {0: False, 1: True} #ifndef _KYBOOL_DEFINED_ typedef char Bool; #define _KYBOOL_DEFINED_ #endif // AVL 树结点类型 typedef struct { void* Item; // 项数据 void* Data; // 自定义数据 } TKYAVLNode, *PKYAVLNode; // OnCompare 结点比较事件 // 若返回值 result == 0 则表示 ANode1 等于 ANode2 // 若返回值 result > 0 则表示 ANode1 大于 ANode2 // 若返回值 result < 0 则表示 ANode1 小于 ANode2 typedef long (*TKYAVL_OnCompare)(const TKYAVLNode* ANode1, const TKYAVLNode* ANode2); // OnDeletion 结点删除事件 typedef void (*TKYAVL_OnDeletion)(void* ATree, TKYAVLNode* ANode); // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /* 公用函数, 注: 为了提高效率不检查 void* ATree 是否合法, 此参数需要外部来维护 */ // 创建/释放 AVL 树 void* KYAVLTree_Create(TKYAVL_OnCompare OnCompare, TKYAVL_OnDeletion OnDeletion); void KYAVLTree_Free(void* ATree); // 读取 AVL 树属性 void* KYAVLTree_Data(void* ATree); // default: NULL long KYAVLTree_Count(void* ATree); // default: 0 long KYAVLTree_MaxCount(void* ATree); // default: 0 Bool KYAVLTree_CanDuplicate(void* ATree); // default: False // 设置 AVL 树属性 void KYAVLTree_SetData(void* ATree, void* AData); void KYAVLTree_SetMaxCount(void* ATree, long AMaxCount); void KYAVLTree_SetCanDuplicate(void* ATree, Bool ACanDuplicate); // 清除 AVL 树的所有结点, 激发 OnDeletion 事件 void KYAVLTree_Clear(void* ATree); // 添加 AVL 结点, 同时: {Node->Item == AItem, Node->Data == Data} TKYAVLNode* KYAVLTree_Add(void* ATree, void* AItem, void* AData); // 删除值为 AItem 和 Data 的 Compare(...) == 0 第一个 AVL 结点, 激发 OnDeletion 事件 Bool KYAVLTree_Delete(void* ATree, const void* AItem, const void* AData); // 移除指定 AVL 结点, 激发 OnDeletion 事件 Bool KYAVLTree_Remove(void* ATree, TKYAVLNode* ANode); // 搜索值为 AItem 和 Data 的 Compare(...) == 0 第一个 AVL 结点 TKYAVLNode* KYAVLTree_Find(void* ATree, const void* AItem, const void* AData); Bool KYAVLTree_Search(void* ATree, void** ARetItem, void** ARetData, const void* AItem, const void* AData); // 查找最近一个 AVL 结点 // 若 ANearest == NULL 则表示项值大于树中的最后一个结点值; // 若返回值为 true, 则表示找到项值的结点, 否则项值在 ANearest 结点之前 Bool KYAVLTree_FindNearest(void* ATree, const void* AItem, const void* AData, PKYAVLNode* ANearest); // 判断 AVL 结点是否存在 Bool KYAVLTree_Existed(void* ATree, TKYAVLNode* ANode); //Bool KYAVLTree_Existed(void* ATree, const void* AItem, const void* AData) // { return KYAVLTree_Find(ATree, AItem, AData) != NULL; } // 取 AVL 树的结点 TKYAVLNode* KYAVLTree_Root(void* ATree); // 树的根结点, default: NULL TKYAVLNode* KYAVLTree_Last(void* ATree); // 树的尾结点, default: NULL TKYAVLNode* KYAVLTree_First(void* ATree); // 树的首结点, default: NULL // 取 AVL 结点的属性 long KYAVLTree_Level(TKYAVLNode* ANode); // 结点所在的层号, Level(NULL) = -1 long KYAVLTree_Height(TKYAVLNode* ANode); // 结点的子树高度, Height(NULL) = 0 char KYAVLTree_Balance(TKYAVLNode* ANode); // 结点的子树平衡标志 // 取 AVL 结点的左右子结点及父结点 TKYAVLNode* KYAVLTree_Left(TKYAVLNode* ANode); // 左子结点, Left(NULL) = NULL TKYAVLNode* KYAVLTree_Right(TKYAVLNode* ANode); // 右子结点, Right(NULL) = NULL TKYAVLNode* KYAVLTree_Parent(TKYAVLNode* ANode); // 父结点, Parent(NULL) = NULL // 取 AVL 结点的前后结点 TKYAVLNode* KYAVLTree_Prior(TKYAVLNode* ANode); // 前一结点, Prior(NULL) = NULL TKYAVLNode* KYAVLTree_Next(TKYAVLNode* ANode); // 下一结点, Next(NULL) = NULL #endif

 

用C语言实现平衡二叉树(AVL tree)源码如下:

// ======================================= // Unit : AVL tree (KYAVLTreeC.c) // Version: 2.1.0.0 (build 2011.03.04) // Author : Kyee Ye // Email : kyee_ye(at)126.com // Copyright (C) Kyee workroom // ======================================= #include #include "KYAVLTreeC.h" // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /* 类型定义 */ // 空值 #ifndef NULL #define NULL 0 #endif // 布尔假 #ifndef False #define False 0 #endif // 布尔真 #ifndef True #define True 1 #endif // AVL 树结点项类型 typedef struct _KYAVLItem { TKYAVLNode Node; // 结点 struct _KYAVLItem* Parent; // 父结点项 struct _KYAVLItem* Left; // 左子结点项 struct _KYAVLItem* Right; // 右子结点项 struct _KYAVLItem* Prior; // 前一结点项 struct _KYAVLItem* Next; // 下一结点项 char Balance; // 平衡标志: Height(Left) - Height(Right) Bool Deleting; // 正在删除 } *PKYAVLItem; typedef struct _KYAVLItem TKYAVLItem; // AVL 树类型 typedef struct { void* Data; // 自定义数据 TKYAVLItem* Root; // 根结点 TKYAVLItem* Head; // 首结点 TKYAVLItem* Tail; // 尾结点 long Count; // 结点个数 long MaxCount; // 结点最大个数, 默认值为 0 表示无限制 Bool CanDuplicate; // 是否允许重复, 默认值为 False // 事件 TKYAVL_OnCompare OnCompare; TKYAVL_OnDeletion OnDeletion; } TKYAVLTree, *PKYAVLTree; // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /* 平衡调整函数 */ // 左右结点调整的平衡增量 static const char _Delta_Balance[2] = {1, -1}; // 调整结点并返回调整后的子树根结点(Left->Left) static TKYAVLItem* L_AdjustLL(TKYAVLTree* ATree, TKYAVLItem* AParent, TKYAVLItem* ATo, TKYAVLItem* ANode) { // 初始化 TKYAVLItem* pGrandparent= AParent->Parent; TKYAVLItem* pRight = ATo->Right; // 修改链接 ATo->Parent = pGrandparent; ATo->Right = AParent; AParent->Parent = ATo; AParent->Left = pRight; // 修改平衡值 AParent->Balance = _Delta_Balance[False] - ATo->Balance; ATo->Balance -= _Delta_Balance[False]; //ANode->Balance = ANode->Balance; // 修改子结点 if (pRight != NULL) pRight->Parent = AParent; // 修改根结点 if (pGrandparent == NULL) ATree->Root = ATo; else if (pGrandparent->Left == AParent) pGrandparent->Left = ATo; else pGrandparent->Right = ATo; // 返回结果 return ATo; } // 调整结点并返回调整后的子树根结点(Left->Right) static TKYAVLItem* L_AdjustLR(TKYAVLTree* ATree, TKYAVLItem* AParent, TKYAVLItem* ATo, TKYAVLItem* ANode) { // 初始化 TKYAVLItem* pGrandparent= AParent->Parent; TKYAVLItem* pRight = ANode->Right; TKYAVLItem* pLeft = ANode->Left; // 修改链接 ANode->Parent = pGrandparent; ANode->Left = ATo; ANode->Right = AParent; ATo->Parent = ANode; ATo->Right = pLeft; AParent->Parent = ANode; AParent->Left = pRight; // 修改平衡值 if (ANode->Balance == _Delta_Balance[False]) AParent->Balance = _Delta_Balance[True]; else AParent->Balance = 0; ATo->Balance += _Delta_Balance[False]; if (ANode->Balance == _Delta_Balance[True]) ATo->Balance += _Delta_Balance[False]; ANode->Balance += AParent->Balance + ATo->Balance; // 修改左子结点 if (pLeft != NULL) pLeft->Parent = ATo; // 修改右子结点 if (pRight != NULL) pRight->Parent = AParent; // 修改根结点 if (pGrandparent == NULL) ATree->Root = ANode; else if (pGrandparent->Left == AParent) pGrandparent->Left = ANode; else pGrandparent->Right = ANode; // 返回结果 return ANode; } // 调整结点并返回调整后的子树根结点(Right->Left) static TKYAVLItem* L_AdjustRL(TKYAVLTree* ATree, TKYAVLItem* AParent, TKYAVLItem* ATo, TKYAVLItem* ANode) { // 初始化 TKYAVLItem* pGrandparent= AParent->Parent; TKYAVLItem* pRight = ANode->Right; TKYAVLItem* pLeft = ANode->Left; // 修改链接 ANode->Parent = pGrandparent; ANode->Left = AParent; ANode->Right = ATo; AParent->Parent = ANode; AParent->Right = pLeft; ATo->Parent = ANode; ATo->Left = pRight; // 修改平衡值 if (ANode->Balance == _Delta_Balance[True]) AParent->Balance = _Delta_Balance[False]; else AParent->Balance = 0; ATo->Balance += _Delta_Balance[True]; if (ANode->Balance == _Delta_Balance[False]) ATo->Balance += _Delta_Balance[True]; ANode->Balance += AParent->Balance + ATo->Balance; // 修改左子结点 if (pLeft != NULL) pLeft->Parent = AParent; // 修改右子结点 if (pRight != NULL) pRight->Parent = ATo; // 修改根结点 if (pGrandparent == NULL) ATree->Root = ANode; else if (pGrandparent->Left == AParent) pGrandparent->Left = ANode; else pGrandparent->Right = ANode; // 返回结果 return ANode; } // 调整结点并返回调整后的子树根结点(Right->Right) static TKYAVLItem* L_AdjustRR(TKYAVLTree* ATree, TKYAVLItem* AParent, TKYAVLItem* ATo, TKYAVLItem* ANode) { // 初始化 TKYAVLItem* pGrandparent= AParent->Parent; TKYAVLItem* pLeft = ATo->Left; // 修改链接 ATo->Parent = pGrandparent; ATo->Left = AParent; AParent->Parent = ATo; AParent->Right = pLeft; // 修改平衡值 AParent->Balance = _Delta_Balance[True] - ATo->Balance; ATo->Balance -= _Delta_Balance[True]; //ANode->Balance = ANode->Balance; // 修改子结点 if (pLeft != NULL) pLeft->Parent = AParent; // 修改根结点 if (pGrandparent == NULL) ATree->Root = ATo; else if (pGrandparent->Left == AParent) pGrandparent->Left = ATo; else pGrandparent->Right = ATo; // 返回结果 return ATo; } // 调整结点的方法 typedef TKYAVLItem* (*TDoAdjust)(TKYAVLTree* ATree, TKYAVLItem* AParent, TKYAVLItem* ATo, TKYAVLItem* ANode); // 调整结点方法列表 static const TDoAdjust _DoAdjust[2][2] = {{L_AdjustLL, L_AdjustLR}, {L_AdjustRL, L_AdjustRR}}; // 调整结点并返回调整后的子树根结点 /* (_DoAdjust[AIsRight1][AIsRight2])(ATree, AParent, ATo, ANode); */ // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /* 静态函数 */ // OnCompare != NULL 时查找结点 // (若 ANearest 返回为 NULL, 则表示此结点大于所有结点值, 添加时要加入末尾) static Bool L_DoFindNode(TKYAVLTree* ATree, const TKYAVLNode* ANode, PKYAVLItem* ANearest) { // 初始化 long intCompare; Bool result = False; TKYAVLItem* pFound = NULL; TKYAVLItem* pNode = ATree->Root; // 循环查找 while (pNode != NULL) { // 比较大小 intCompare = ATree->OnCompare(ANode, (TKYAVLNode*)pNode); pFound = pNode; // 判断是否成功 if (intCompare == 0) { result = True; break; } else if (intCompare < 0) pNode = pNode->Left; else { pFound = pNode->Next; pNode = pNode->Right; } } // 若允许重复则需要找到相等的第一项 if (result && ATree->CanDuplicate) { pNode = pFound->Left; while (pNode != NULL) if (ATree->OnCompare(ANode, (TKYAVLNode*)pNode) == 0) { pFound = pNode; pNode = pNode->Left; } else if (pNode->Right != NULL) pNode = pNode->Right; else break; } // 返回结果 *ANearest = pFound; return result; } // 查找结点 // (若 ANearest 返回为 NULL, 则表示此结点大于所有结点值, 添加时要加入末尾) static Bool L_FindNode(TKYAVLTree* ATree, const TKYAVLNode* ANode, PKYAVLItem* ANearest) { // 初始化 Bool result = False; TKYAVLItem* pFound = NULL; TKYAVLItem* pNode = ATree->Root; // 判断比较事件是否非空 if (ATree->OnCompare != NULL) return L_DoFindNode(ATree, ANode, ANearest); // 循环查找 while (pNode != NULL) { pFound = pNode; if (ANode->Item == pNode->Node.Item) { result = True; break; } else if ((long)ANode->Item < (long)pNode->Node.Item) pNode = pNode->Left; else { pFound = pNode->Next; pNode = pNode->Right; } } // 若允许重复则需要找到相等的第一项 if (result && ATree->CanDuplicate) { pNode = pFound->Left; while (pNode != NULL) if (ANode->Item == pNode->Node.Item) { pFound = pNode; pNode = pNode->Left; } else if (pNode->Right != NULL) pNode = pNode->Right; else break; } // 返回结果 *ANearest = pFound; return result; } // 新增结点并修改上下链接 static TKYAVLItem* L_DoNewNode(TKYAVLTree* ATree, const TKYAVLNode* ANode, PKYAVLItem* ATo, Bool* AIsAdd, Bool* AIsBreak) { // 分配项 TKYAVLItem* result= (TKYAVLItem*)malloc(sizeof(TKYAVLItem)); // 初始化项 result->Node = *ANode; result->Parent = NULL; result->Left = NULL; result->Right = NULL; result->Prior = NULL; result->Next = NULL; result->Balance = 0; result->Deleting = False; ATree->Count++; // 初始化 *AIsAdd = False; *AIsBreak = False; // 修改链接 if (*ATo == NULL) { if (ATree->Tail == NULL) { ATree->Root = result; ATree->Head = result; ATree->Tail = result; *AIsBreak = True; } else { *ATo = ATree->Tail; *AIsAdd = True; result->Prior = ATree->Tail; ATree->Tail->Next = result; ATree->Tail = result; } } else if (((*ATo)->Left != NULL) && ((*ATo)->Right != NULL)) { *AIsAdd = True; *ATo = (*ATo)->Prior; result->Prior = *ATo; result->Next = (*ATo)->Next; (*ATo)->Next->Prior = result; (*ATo)->Next = result; } else { result->Prior = (*ATo)->Prior; result->Next = (*ATo); if ((*ATo)->Prior != NULL) (*ATo)->Prior->Next = result; else ATree->Head = result; (*ATo)->Prior = result; } // 返回结果 return result; } // 减平衡值(注: AParent != NULL) static void L_DecBalance(TKYAVLTree* ATree, Bool AIsRight, TKYAVLItem* AParent) { // 初始化 Bool boolBreak, boolRight; PKYAVLItem pTo, pNode; // 循环调整平衡值 for (; ;) { // 修改父结点的平衡值 AParent->Balance -= _Delta_Balance[AIsRight]; // 判断是否中止调整 if (AParent->Balance == -_Delta_Balance[AIsRight]) break; else if (AParent->Balance != 0) // 调整结点 { // 取 To 结点 if (AIsRight) { pTo = AParent->Left; boolRight = (pTo->Balance == _Delta_Balance[True]); } else { pTo = AParent->Right; boolRight = (pTo->Balance != _Delta_Balance[False]); } // 取 Node 结点并调整结点 boolBreak = (pTo->Balance == 0); pNode = (boolRight) ? pTo->Right : pTo->Left; AParent = (_DoAdjust[!AIsRight][boolRight])(ATree, AParent, pTo, pNode); // 判断是否中止调整 if (boolBreak) break; } // 取祖父结点 pNode = AParent->Parent; if (pNode == NULL) break; // 向上传递平衡值 AIsRight = (pNode->Right == AParent); AParent = pNode; } } // 增平衡值(注: AParent != NULL) static void L_IncBalance(TKYAVLTree* ATree, Bool AIsRight, TKYAVLItem* AParent, TKYAVLItem* ATo, TKYAVLItem* ANode) { // 初始化 Bool boolRight = False; // 循环调整平衡值 do { // 修改父结点的平衡值 boolRight = (AParent->Right == ATo); AParent->Balance += _Delta_Balance[boolRight]; // 判断是否向上调整 if (AParent->Balance == _Delta_Balance[boolRight]) { ANode = ATo; ATo = AParent; AParent = AParent->Parent; AIsRight = boolRight; } else if (AParent->Balance != 0) // 调整结点并中止向上调整 { (_DoAdjust[boolRight][AIsRight])(ATree, AParent, ATo, ANode); break; } else break; } while (AParent != NULL); } // 加入结点到 ATo 后面(注: ATo->Right 必定为 NULL) static void L_DoAdd(TKYAVLTree* ATree, TKYAVLItem* ANode, TKYAVLItem* ATo) { // 初始化 TKYAVLItem* pParent = ATo->Parent; // 加入右结点 ANode->Parent = ATo; ATo->Right = ANode; ATo->Balance += _Delta_Balance[True]; // 判断是否平衡 if ((ATo->Balance != 0) && (pParent != NULL)) L_IncBalance(ATree, True, pParent, ATo, ANode); } // 插入结点到 ATo 前面 static void L_DoInsert(TKYAVLTree* ATree, TKYAVLItem* ANode, TKYAVLItem* ATo) { // 初始化 TKYAVLItem* pLeft = ATo->Left; TKYAVLItem* pParent = ATo->Parent; // 加入左结点 ANode->Parent = ATo; ATo->Left = ANode; ATo->Balance += _Delta_Balance[False]; // 判断左子结点是否非空(若非空则ATo->Right必定为NULL) if (pLeft != NULL) { pLeft->Parent = ANode; ANode->Left = pLeft; ANode->Balance += _Delta_Balance[False]; // 调整 L_AdjustLL(ATree, ATo, ANode, pLeft); } else if ((ATo->Balance != 0) && (pParent != NULL)) L_IncBalance(ATree, False, pParent, ATo, ANode); } //若 ATo == NULL 则追加到末尾, 否则插入 ATo 之前 // 注: FRoot, FHead, FTail, FCurr, FCount 会产生变化 static TKYAVLItem* L_InsertNode(TKYAVLTree* ATree, const TKYAVLNode* ANode, TKYAVLItem* ATo) { // 初始化 Bool boolAdd, boolBreak; TKYAVLItem* result = L_DoNewNode(ATree, ANode, &ATo, &boolAdd, &boolBreak); // 操作 if (boolBreak) ; else if (boolAdd) L_DoAdd(ATree, result, ATo); else L_DoInsert(ATree, result, ATo); // 返回结果 return result; } // 从树中删除结点, 并重新平衡树, 但不释放结点项 // 注: FRoot, FHead, FTail, FCurr, FCount 会产生变化 static void L_DeleteNode(TKYAVLTree* ATree, TKYAVLItem* ANode) { // 初始化 Bool boolRight; TKYAVLItem* pParent = ANode->Parent; TKYAVLItem* pPrior = ANode->Prior; TKYAVLItem* pRight = ANode->Right; TKYAVLItem* pLeft = ANode->Left; // 置删除标志 ATree->Count--; ANode->Deleting = True; // 修改前一个链接 if (pPrior == NULL) ATree->Head = ANode->Next; else pPrior->Next = ANode->Next; // 修改下一个链接 if (ANode->Next == NULL) ATree->Tail = pPrior; else ANode->Next->Prior = pPrior; // 判断左子结点是否为空 if (pLeft == NULL) { // 修改右子结点的连接 if (pRight != NULL) pRight->Parent = pParent; // 判断父结点是否为空 if (pParent == NULL) ATree->Root = pRight; else { // 修改父结点的连接 boolRight = (pParent->Right == ANode); if (boolRight) pParent->Right = pRight; else pParent->Left = pRight; // 减平衡值 L_DecBalance(ATree, boolRight, pParent); } } else if (pRight == NULL) // 判断右子结点是否为空 { // 修改左子结点的连接 pLeft->Parent = pParent; // 判断父结点是否为空 if (pParent == NULL) ATree->Root = pLeft; else { // 修改父结点的连接 boolRight = (pParent->Right == ANode); if (boolRight) pParent->Right = pLeft; else pParent->Left = pLeft; // 减平衡值 L_DecBalance(ATree, boolRight, pParent); } } else if (pPrior == pLeft) // 判断左子结点是否为前一结点 { // 判断父结点是否为空 if (pParent == NULL) ATree->Root = pLeft; else if (pParent->Right == ANode) pParent->Right = pLeft; else pParent->Left = pLeft; // 修改左右子结点的连接 pRight->Parent = pLeft; pLeft->Parent = pParent; pLeft->Right = pRight; pLeft->Balance = ANode->Balance; // 减平衡值 L_DecBalance(ATree, False, pLeft); } else { // 判断父结点是否为空 if (pParent == NULL) ATree->Root = pPrior; else if (pParent->Right == ANode) pParent->Right = pPrior; else pParent->Left = pPrior; // 修改左右子结点的连接 pLeft->Parent = pPrior; pRight->Parent = pPrior; // 取前一结点的父结点和左子结点(pPrior->Right 必定为 NULL) pParent = pPrior->Parent; pLeft = pPrior->Left; // 使用前一结点替换当前结点 pPrior->Parent = ANode->Parent; pPrior->Left = ANode->Left; pPrior->Right = ANode->Right; pPrior->Balance = ANode->Balance; // 修改前一结点的左子结点的连接 if (pLeft != NULL) pLeft->Parent = pParent; // 修改前一结点的父结点的连接(pParent->Right 必定为 pPrior) pParent->Right = pLeft; // 减平衡值 L_DecBalance(ATree, True, pParent); } } // 清除所有结点 static void L_ClearNodes(TKYAVLTree* ATree, TKYAVLItem* AHead) { // 初始化 TKYAVLItem* pNode; // 判断 OnDeletion 事件是否非空 if (ATree->OnDeletion != NULL) while (AHead != NULL) { pNode = AHead; AHead = AHead->Next; // 激发 OnDeletion 事件 ATree->OnDeletion(ATree, (TKYAVLNode*)pNode); // 删除结点项 free(pNode); } else while (AHead != NULL) { pNode = AHead; AHead = AHead->Next; // 删除结点项 free(pNode); } } // 设置所有结点删除标志 static void L_SetAllDeleting(TKYAVLItem* AHead) { while (AHead != NULL) { AHead->Deleting = True; AHead = AHead->Next; } } // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /* 公用函数, 注: 为了提高效率不检查 void* ATree 是否合法, 此参数需要外部来维护 */ // 创建 AVL 树 void* KYAVLTree_Create(TKYAVL_OnCompare OnCompare, TKYAVL_OnDeletion OnDeletion) { // 初始化 TKYAVLTree* result = (TKYAVLTree*)malloc(sizeof(TKYAVLTree)); // 赋值 result->Data = NULL; result->Root = NULL; result->Head = NULL; result->Tail = NULL; result->Count = 0; result->MaxCount = 0; result->CanDuplicate = False; // 设置事件 result->OnCompare = OnCompare; result->OnDeletion = OnDeletion; // 返回结果 return result; } // 释放 AVL 树 void KYAVLTree_Free(void* ATree) { // 清除 KYAVLTree_Clear(ATree); // 释放 free(ATree); } // default: NULL void* KYAVLTree_Data(void* ATree) { return ((TKYAVLTree*)ATree)->Data; } // 取 AVL 树的结点个数, default: 0 long KYAVLTree_Count(void* ATree) { return ((TKYAVLTree*)ATree)->Count; } // 取 AVL 树的结点最大个数, default: 0 long KYAVLTree_MaxCount(void* ATree) { return ((TKYAVLTree*)ATree)->MaxCount; } // 取 AVL 树的是否允许相同结点值项, default: False Bool KYAVLTree_CanDuplicate(void* ATree) { return ((TKYAVLTree*)ATree)->CanDuplicate; } // AVL 树设置属性 void KYAVLTree_SetData(void* ATree, void* AData) { ((TKYAVLTree*)ATree)->Data = AData; } // 设置 AVL 树的结点最大个数, 0 表示无限制个数 void KYAVLTree_SetMaxCount(void* ATree, long AMaxCount) { if (AMaxCount <= 0) ((TKYAVLTree*)ATree)->MaxCount = 0; else if (AMaxCount <= ((TKYAVLTree*)ATree)->Count) ((TKYAVLTree*)ATree)->MaxCount = ((TKYAVLTree*)ATree)->Count; else ((TKYAVLTree*)ATree)->MaxCount = AMaxCount; } // 设置 AVL 树的允许相同结点值项 void KYAVLTree_SetCanDuplicate(void* ATree, Bool ACanDuplicate) { ((TKYAVLTree*)ATree)->CanDuplicate = (ACanDuplicate != 0); } // 清除 AVL 树的所有结点, 激发 OnDeletion 事件 void KYAVLTree_Clear(void* ATree) { // 初始化 TKYAVLTree* pTree = (TKYAVLTree*)ATree; TKYAVLItem* pHead = pTree->Head; //(TKYAVLItem*)InterlockedExchange((long*)&pTree->Head, 0); // 清空 pTree->Head = NULL; pTree->Root = NULL; pTree->Tail = NULL; pTree->Count = 0; // 设置删除标志 if (pHead != NULL) L_SetAllDeleting(pHead); // 清除树 if (pHead != NULL) L_ClearNodes(pTree, pHead); } // 添加 AVL 结点, 同时: {Node->Item == AItem, Node->Data == Data} TKYAVLNode* KYAVLTree_Add(void* ATree, void* AItem, void* AData) { // 初始化 TKYAVLNode* result = NULL; TKYAVLTree* pTree = (TKYAVLTree*)ATree; // 检查个数 if ((pTree->MaxCount == 0) || (pTree->Count < pTree->MaxCount)) { // 初始化 TKYAVLNode recNode; TKYAVLItem* pNearest; // 查找结点 recNode.Item = AItem; recNode.Data = AData; if (!L_FindNode(pTree, &recNode, &pNearest) || pTree->CanDuplicate) result = (TKYAVLNode*)L_InsertNode(pTree, &recNode, pNearest); } // 返回结果 return result; } // 删除值为 AItem 和 Data 的 Compare(...) == 0 第一个 AVL 结点, 激发 OnDeletion 事件 Bool KYAVLTree_Delete(void* ATree, const void* AItem, const void* AData) { // 初始化 Bool result = False; TKYAVLTree* pTree = (TKYAVLTree*)ATree; TKYAVLItem* pFound; TKYAVLNode recNode; // 查找结点 recNode.Item = (void*)AItem; recNode.Data = (void*)AData; if (L_FindNode(pTree, &recNode, &pFound)) { // 删除结点 L_DeleteNode(pTree, pFound); result = True; // 激发 OnDeletion 事件 if (pTree->OnDeletion != NULL) pTree->OnDeletion(ATree, (TKYAVLNode*)pFound); // 释放 free(pFound); } // 返回结果 return result; } // 移除指定 AVL 结点, 激发 OnDeletion 事件 Bool KYAVLTree_Remove(void* ATree, TKYAVLNode* ANode) { // 初始化 Bool result = False; // 判断结点是否存在 if (KYAVLTree_Existed(ATree, ANode)) { // 初始化 TKYAVLTree* pTree = (TKYAVLTree*)ATree; // 删除结点 L_DeleteNode(pTree, (TKYAVLItem*)ANode); result = True; // 激发 OnDeletion 事件 if (pTree->OnDeletion != NULL) pTree->OnDeletion(ATree, ANode); // 释放 free(ANode); } // 返回结果 return result; } // 搜索值为 AItem 和 Data 的 Compare(...) == 0 第一个 AVL 结点 TKYAVLNode* KYAVLTree_Find(void* ATree, const void* AItem, const void* AData) { // 初始化 TKYAVLNode* result = NULL; TKYAVLItem* pFound; TKYAVLNode recNode; // 查找 recNode.Item = (void*)AItem; recNode.Data = (void*)AData; if (L_FindNode((TKYAVLTree*)ATree, &recNode, &pFound)) result = (TKYAVLNode*)pFound; // 返回结果 return result; } Bool KYAVLTree_Search(void* ATree, void** ARetItem, void** ARetData, const void* AItem, const void* AData) { // 初始化 Bool result = False; TKYAVLItem* pFound; TKYAVLNode recNode; // 查找 recNode.Item = (void*)AItem; recNode.Data = (void*)AData; if (L_FindNode((TKYAVLTree*)ATree, &recNode, &pFound)) { result = True; *ARetItem = pFound->Node.Item; *ARetData = pFound->Node.Data; } // 返回结果 return result; } // 查找最近一个 AVL 结点 // 若 ANearest == NULL 则表示项值大于树中的最后一个结点值 // 若返回值为 True, 则表示找到项值的结点, 否则项值在 ANearest 结点之前 Bool KYAVLTree_FindNearest(void* ATree, const void* AItem, const void* AData, PKYAVLNode* ANearest) { // 初始化 TKYAVLNode recNode; // 赋值 recNode.Item = (void*)AItem; recNode.Data = (void*)AData; // 查找 return L_FindNode((TKYAVLTree*)ATree, &recNode, (PKYAVLItem*)ANearest); } // 判断 AVL 结点是否存在 Bool KYAVLTree_Existed(void* ATree, TKYAVLNode* ANode) { // 初始化 Bool result = False; TKYAVLTree* pTree = (TKYAVLTree*)ATree; TKYAVLItem* pNode = (TKYAVLItem*)ANode; // 判断是否未删除 if ((pTree->Root != NULL) && (pNode != NULL) && !pNode->Deleting) { // 取根结点 while (pNode->Parent != NULL) pNode = pNode->Parent; // 判断根是否相同 result = (pNode == pTree->Root); } // 返回结果 return result; } // 取 AVL 树的根结点, default: NULL TKYAVLNode* KYAVLTree_Root(void* ATree) { return (TKYAVLNode*)((TKYAVLTree*)ATree)->Root; } // 取 AVL 树的尾结点, default: NULL TKYAVLNode* KYAVLTree_Last(void* ATree) { return (TKYAVLNode*)((TKYAVLTree*)ATree)->Tail; } // 取 AVL 树的首结点, default: NULL TKYAVLNode* KYAVLTree_First(void* ATree) { return (TKYAVLNode*)((TKYAVLTree*)ATree)->Head; } // 取 AVL 结点所在的层号, Level(NULL) = -1 long KYAVLTree_Level(TKYAVLNode* ANode) { // 初始化 long result = -1; TKYAVLItem* pNode = (TKYAVLItem*)ANode; // 循环计算 while (pNode != NULL) { result++; pNode = pNode->Parent; } // 返回结果 return result; } // 取 AVL 结点的子树高度, Height(NULL) = 0 long KYAVLTree_Height(TKYAVLNode* ANode) { // 初始化 long result = 0; TKYAVLItem* pNode = (TKYAVLItem*)ANode; // 循环计算 while (pNode != NULL) { result++; // 判断子树高度 if (pNode->Balance == _Delta_Balance[True]) pNode = pNode->Right; else pNode = pNode->Left; } // 返回结果 return result; } // 取 AVL 结点的子树平衡标志 char KYAVLTree_Balance(TKYAVLNode* ANode) { if (ANode != NULL) return ((TKYAVLItem*)ANode)->Balance; else return 0; } // 取 AVL 结点的左子结点 TKYAVLNode* KYAVLTree_Left(TKYAVLNode* ANode) { if (ANode != NULL) return (TKYAVLNode*)((TKYAVLItem*)ANode)->Left; else return NULL; } // 取 AVL 结点的右子结点 TKYAVLNode* KYAVLTree_Right(TKYAVLNode* ANode) { if (ANode != NULL) return (TKYAVLNode*)((TKYAVLItem*)ANode)->Right; else return NULL; } // 取 AVL 结点的父结点 TKYAVLNode* KYAVLTree_Parent(TKYAVLNode* ANode) { if (ANode != NULL) return (TKYAVLNode*)((TKYAVLItem*)ANode)->Parent; else return NULL; } // 取 AVL 结点的前一结点 TKYAVLNode* KYAVLTree_Prior(TKYAVLNode* ANode) { if (ANode != NULL) return (TKYAVLNode*)((TKYAVLItem*)ANode)->Prior; else return NULL; } // 取 AVL 结点的下一结点 TKYAVLNode* KYAVLTree_Next(TKYAVLNode* ANode) { if (ANode != NULL) return (TKYAVLNode*)((TKYAVLItem*)ANode)->Next; else return NULL; }

你可能感兴趣的:(C++类库KYLib,源码)