亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 學院 > 開發設計 > 正文

用c做存儲之二:avl樹結構、應用場景及實現

2019-11-11 05:33:49
字體:
來源:轉載
供稿:網友

avl樹用于存儲大量非相關數據,并且在動態更新時能夠快速平衡。 avl樹實現實例:

typedef struct{ UINT8 Name[MAX_DIAGLOG_TYPE_NAME_LEN]; /*業務注冊類型名*/ }DIAGLOG_REG; /* 診斷日志注冊數據結構,使用時先memset清0 */typedef struct{ AVL_NODE Node; /*平衡樹結點*/ DIAGLOG_REG DiaglogReg;}DIAGLOG_REG_NODE; DIAGLOG_REG_NODE * p_DiagLogRegNode = NULL;//結點名稱屬性比較函數INT32S Diaglog_RegAvlNodeCmp(void * v_Node, GENERIC_ARGUMENT v_Key){ DIAGLOG_REG_NODE* PRegNode = (DIAGLOG_REG_NODE *)v_Node; return strncmp(v_Key.p, (char *)pRegNode->DiaglogReg.Name, MAX_DIAGLOG_TYPE_NAME_LEN);}DIAGLOG_REG_NODE *Diaglog_RegAvlNodeGet(UINT8* key){ DIAGLOG_REG_NODE* pRegNode = NULL; GENERIC_ARGUMENT GenericArg = {0}; GenericArg.p = (void *)key; pRegNode = (DIAGLOG_REG_NODE*)avlSearch((AVL_TREE)(p_DiagLogRegNode), GenericArg, Diaglog_RegAvlNodeCmp); return pRegNode;}INT32S Diaglog_RegAddAvlNode(DIAGLOG_REG_NODE * v_Node){ GENERIC_ARGUMENT GenericArg = {0}; DIAGLOG_REG_NODE* pRegNode = NULL; pRegNode = Diaglog_RegAvlNodeGet(v_Node->DiaglogReg.Name); if (NULL != pRegNode) { return ERROR; } pRegNode = Diaglog_UniAlloc(sizeof(DIAGLOG_REG_NODE)); if (NULL == pRegNode) { return ERROR; } memset(pRegNode, 0, sizeof(DIAGLOG_REG_NODE)); memmove(&pRegNode->DiaglogReg, &v_Node->DiaglogReg, sizeof(DIAGLOG_REG)); GenericArg.p = (void*)v_Node->DiaglogReg.Name; if (OK == avlInsert((AVL_TREE *)&p_DiagLogRegNode, pRegNode, GenericArg, Diaglog_RegAvlNodeCmp)) { return OK; } else { return ERROR; }}DIAGLOG_REG_NODE * Diaglog_RegAvlNodeGetFirst(void){ DIAGLOG_REG_NODE * pRegNode = NULL; pRegNode = (DIAGLOG_REG_NODE *)avlMinimumGet((AVL_TREE)p_DiagLogRegNode); return pRegNode;}DIAGLOG_REG_NODE *Diaglog_RegAvlNodeGetNext(UINT8* key){ GENERIC_ARGUMENT genericArg = {0}; if (0==strlen((char *)key)) { return Diaglog_RegAvlNodeGetFirst(); } genericArg.p = (void *)key; return (DIAGLOG_REG_NODE *)avlSuccessorGet((AVL_TREE)p_DiagLogRegNode, genericArg, Diaglog_RegAvlNodeCmp);}void Diaglog_RegDelAvlNode(UINT8* key){ DIAGLOG_REG_NODE * pRegNode = NULL; GENERIC_ARGUMENT genericArg = {0}; genericArg.p = (void*)key; pRegNode = (DIAGLOG_REG_NODE *)avlDelete((AVL_TREE *)&p_DiagLogRegNode, genericArg, Diaglog_RegAvlNodeCmp); if (NULL == pRegNode) { return; } Diaglog_FreeBuf((void *)pRegNode); }

avl樹庫函數源碼 頭文件

/* avlLib.h - AVL trees library header file *//* Copyright 1999 Wind River Systems, Inc. *//*modification history--------------------01f,24jan01,sn end file with newline(!) to avoid cpp errors01e,10feb00,abd added avlMinimumGet and avlMaximumGet01d,10feb00,abd added avlTreeWalk, avlTreePrint, avlTreeErase, avlTreePrintErase01c,03feb00,abd added avlInsertInform, avlRemoveInsert01b,24jan00,abd added avlSuccessorGet and avlPredecessorGet01a,08feb99,wkn created.*/#ifndef __INCavlLibh#define __INCavlLibh#ifdef __cplusplusextern "C" {#endif/* typedefs *//*#ifndef _My_AvlLib_H*/typedef struct { void * left; /* pointer to the left subtree */ void * right; /* pointer to the right subtree */ int height; /* height of the subtree rooted at this node */ } AVL_NODE;typedef AVL_NODE * AVL_TREE; /* points to the root node of the tree */typedef union { int i; UINT u; void * p; } GENERIC_ARGUMENT;/*#endif*//* function declarations */void avlRebalance (AVL_NODE *** ancestors, int count);void * avlSearch (AVL_TREE root, GENERIC_ARGUMENT key, int compare (void *, GENERIC_ARGUMENT));void * avlSuccessorGet (AVL_TREE root, GENERIC_ARGUMENT key, int compare (void *, GENERIC_ARGUMENT)); void * avlPredecessorGet (AVL_TREE root, GENERIC_ARGUMENT key, int compare (void *, GENERIC_ARGUMENT)); void * avlMinimumGet (AVL_TREE root);void * avlMaximumGet (AVL_TREE root);STATUS avlInsert (AVL_TREE * root, void * newNode, GENERIC_ARGUMENT key, int compare (void *, GENERIC_ARGUMENT));STATUS avlInsertInform (AVL_TREE * pRoot, void * pNewNode, GENERIC_ARGUMENT key, void ** ppKeyHolder, int compare (void *, GENERIC_ARGUMENT));void * avlRemoveInsert (AVL_TREE * pRoot, void * pNewNode, GENERIC_ARGUMENT key, int compare (void *, GENERIC_ARGUMENT));void * avlDelete (AVL_TREE * root, GENERIC_ARGUMENT key, int compare (void *, GENERIC_ARGUMENT));STATUS avlTreeWalk(AVL_TREE * pRoot, void walkExec(AVL_TREE * nodepp));STATUS avlTreeWalkWithPara(AVL_TREE * pRoot, void walkExec(AVL_TREE * ppNode, void * v_Para), void * v_Para);STATUS avlTreePrint(AVL_TREE * pRoot, void printNode(void * nodep));STATUS avlTreeErase(AVL_TREE * pRoot);STATUS avlTreePrintErase(AVL_TREE * pRoot, void printNode(void * nodep));/* specialized implementation functions */void * avlSearchUnsigned (AVL_TREE root, UINT key);STATUS avlInsertUnsigned (AVL_TREE * root, void * newNode);void * avlDeleteUnsigned (AVL_TREE * root, UINT key);#ifndef AVLTREE_FREE_FUNC_DEFINED#define AVLTREE_FREE_FUNC_DEFINEDtypedef void (*AVLTREE_FREE_FUNC)(void *);#endifSTATUS avlTreeEraseWithFunc(AVL_TREE * pRoot, AVLTREE_FREE_FUNC v_Func);#ifdef __cplusplus}#endif#endif /* __INCavlLibh */

c文件

/* avlLib.c - AVL trees library *//* Copyright 1999 Wind River Systems, Inc. */#include "copyright_wrs.h"/*modification history--------------------01e,10feb00,abd added avlMinimumGet and avlMaximumGet01d,10feb00,abd added avlTreeWalk, avlTreePrint, avlTreeErase, avlTreePrintErase01c,03feb00,abd added avlInsertInform, avlRemoveInsert01b,24jan00,abd added avlSuccessorGet and avlPredecessorGet01a,08feb99,wkn created.*//*DESCRipTIONThis library provides routines to manage some partially-balanced binary treesusing the AVL algorithm. The tree nodes are ordered according to a given fullyordered relation, and there cannot be two nodes in the tree that are consideredas equals by this relation. A balancing algorithm is run after each insertionor deletion Operation. The balancing algorithm is guaranteed to run in timeproportional to the height of the tree, and this height is guaranteed to onlygrow with log(N) where N is the number of nodes in the tree. Thus searching,insertion and deletion are all guaranteed to run in time proportional tolog(N).Because the rebalancing operation might require re-rooting the binary tree,the arguments to the insertion and deletion operations cannot be simplypointers to the root node but they need to be pointers to the root nodepointer. This way the root node pointer can be modified when the binary treeis re-rooted.In order to save some memory, the tree nodes does not contains pointers totheir parent node. However, the rebalancing operation needs to walk from agiven node up to the root node. We thus expect the insertion and deletionroutines to keep track of the path they followed from the root down to theleaf node they had to insert or delete. See the avlRebalance routine for moredetails about this, and the avlInsert and avlDelete routines for referenceimplementations.It is an implementation goal to allow the creation of AVL trees containingsome various data types in their nodes. However, the searching, insertion,and deletion routines needs to be able to compare two data nodes, so theyare dependant upon the precise type of the search keys. The implementation ofavlSearch, avlInsert and avlDelete uses a user-provided function that comparesa tree node with a value and returns -1, 0 or 1 depending if this value isinferior, equal or superior to the key of the tree node. The value that ispassed to this comparison function is typed as a GENERIC_ARGUMENT, which is aC union of an integer and a void pointer, thus making argument passing bothfast and generic.INTERNALFor cases where execution speed is more important than code size, one couldalso use his own specialized routines instead of avlInsert, avlDelete and/oravlSearch, where the comparison function would be hardcoded to suit the user'sneeds. If one decides to do so, he can still use the generic avlRebalanceroutine because thisone does not needs to use a comparison function.INCLUDE FILE: avlLib.h*//* includes */#include <vxWorks.h>#include <stdlib.h>#include <stdio.h>#include "avlLib.h"/* typedefs */typedef struct { AVL_NODE avl; UINT key; } AVL_UNSIGNED_NODE;/* defines */#define AVL_MAX_HEIGHT 42 /* The meaning of life, the universe and everything. Plus, the nodes for a tree this high would use more than 2**32 bytes anyway *//********************************************************************************* avlRebalance - make sure the tree conforms to the AVL balancing rules, while* preserving the ordering of the binary tree** INTERNAL* The AVL tree balancing rules are as follows :* - the height of the left and right subtrees under a given node must never* differ by more than one* - the height of a given subtree is defined as 1 plus the maximum height of* the subtrees under his root node** The avlRebalance procedure must be called after a leaf node has been inserted* or deleted from the tree. It checks that the AVL balancing rules are* respected, makes local adjustments to the tree if necessary, recalculates* the height field of the modified nodes, and repeats the process for every* node up to the root node. This iteration is necessary because the balancing* rules for a given node might have been broken by the modification we did on* one of the subtrees under it.** Because we need to iterate the process up to the root node, and the tree* nodes does not contain pointers to their father node, we ask the caller of* this procedure to keep a list of all the nodes traversed from the root node* to the node just before the recently inserted or deleted node. This is the* <ancestors> argument. Because each subtree might have to be re-rooted in the* balancing operation, <ancestors> is actually a list pointers to the node* pointers - thus if re-rooting occurs, the node pointers can be modified so* that they keep pointing to the root of a given subtree.** <count> is simply a count of elements in the <ancestors> list.** RETURNS: N/A*/void avlRebalance ( AVL_NODE *** ancestors, /* list of pointers to the ancestor node pointers */ int count /* number ancestors to rebalance */ ) { while (count > 0) { AVL_NODE ** nodepp; /* address of the pointer to the root node of the current subtree */ AVL_NODE * nodep; /* points to root node of current subtree */ AVL_NODE * leftp; /* points to root node of left subtree */ int lefth; /* height of the left subtree */ AVL_NODE * rightp; /* points to root node of right subtree */ int righth; /* height of the right subtree */ /* * Find the current root node and its two subtrees. By construction, * we know that both of them conform to the AVL balancing rules. */ nodepp = ancestors[--count]; nodep = *nodepp; leftp = nodep->left; lefth = (leftp != NULL) ? leftp->height : 0; rightp = nodep->right; righth = (rightp != NULL) ? rightp->height : 0; if (righth - lefth < -1) { /* * * * / / * n+2 n * * The current subtree violates the balancing rules by beeing too * high on the left side. We must use one of two different * rebalancing methods depending on the configuration of the left * subtree. * * Note that leftp cannot be NULL or we would not pass there ! */ AVL_NODE * leftleftp; /* points to root of left left subtree */ AVL_NODE * leftrightp; /* points to root of left right subtree */ int leftrighth; /* height of left right subtree */ leftleftp = leftp->left; leftrightp = leftp->right; leftrighth = (leftrightp != NULL) ? leftrightp->height : 0; if ((leftleftp != NULL) && (leftleftp->height >= leftrighth)) { /* * <D> <B> * * n+2|n+3 * / / / / * <B> <E> ----> <A> <D> * n+2 n n+1 n+1|n+2 * / / / / * <A> <C> <C> <E> * n+1 n|n+1 n|n+1 n */ nodep->left = leftrightp; /* D.left = C */ nodep->height = leftrighth + 1; leftp->right = nodep; /* B.right = D */ leftp->height = leftrighth + 2; *nodepp = leftp; /* B becomes root */ } else { /* * <F> * * * / / <D> * <B> <G> n+2 * n+2 n / / * / / ----> <B> <F> * <A> <D> n+1 n+1 * n n+1 / / / / * / / <A> <C> <E> <G> * <C> <E> n n|n-1 n|n-1 n * n|n-1 n|n-1 * * We can assume that leftrightp is not NULL because we expect * leftp and rightp to conform to the AVL balancing rules. * Note that if this assumption is wrong, the algorithm will * crash here. */ leftp->right = leftrightp->left; /* B.right = C */ leftp->height = leftrighth; nodep->left = leftrightp->right; /* F.left = E */ nodep->height = leftrighth; leftrightp->left = leftp; /* D.left = B */ leftrightp->right = nodep; /* D.right = F */ leftrightp->height = leftrighth + 1; *nodepp = leftrightp; /* D becomes root */ } } else if (righth - lefth > 1) { /* * * * / / * n n+2 * * The current subtree violates the balancing rules by beeing too * high on the right side. This is exactly symmetric to the * previous case. We must use one of two different rebalancing * methods depending on the configuration of the right subtree. * * Note that rightp cannot be NULL or we would not pass there ! */ AVL_NODE * rightleftp; /* points to the root of right left subtree */ int rightlefth; /* height of right left subtree */ AVL_NODE * rightrightp; /* points to the root of right right subtree */ rightleftp = rightp->left; rightlefth = (rightleftp != NULL) ? rightleftp->height : 0; rightrightp = rightp->right; if ((rightrightp != NULL) && (rightrightp->height >= rightlefth)) { /* <B> <D> * * n+2|n+3 * / / / / * <A> <D> ----> <B> <E> * n n+2 n+1|n+2 n+1 * / / / / * <C> <E> <A> <C> * n|n+1 n+1 n n|n+1 */ nodep->right = rightleftp; /* B.right = C */ nodep->height = rightlefth + 1; rightp->left = nodep; /* D.left = B */ rightp->height = rightlefth + 2; *nodepp = rightp; /* D becomes root */ } else { /* <B> * * * / / <D> * <A> <F> n+2 * n n+2 / / * / / ----> <B> <F> * <D> <G> n+1 n+1 * n+1 n / / / / * / / <A> <C> <E> <G> * <C> <E> n n|n-1 n|n-1 n * n|n-1 n|n-1 * * We can assume that rightleftp is not NULL because we expect * leftp and rightp to conform to the AVL balancing rules. * Note that if this assumption is wrong, the algorithm will * crash here. */ nodep->right = rightleftp->left; /* B.right = C */ nodep->height = rightlefth; rightp->left = rightleftp->right; /* F.left = E */ rightp->height = rightlefth; rightleftp->left = nodep; /* D.left = B */ rightleftp->right = rightp; /* D.right = F */ rightleftp->height = rightlefth + 1; *nodepp = rightleftp; /* D becomes root */ } } else { /* * No rebalancing, just set the tree height * * If the height of the current subtree has not changed, we can * stop here because we know that we have not broken the AVL * balancing rules for our ancestors. */ int height; height = ((righth > lefth) ? righth : lefth) + 1; if (nodep->height == height) break; nodep->height = height; } } }/********************************************************************************* avlSearch - search a node in an AVL tree** At the time of the call, <root> is the root node pointer. <key> is the value* we want to search, and <compare> is the user-provided comparison function.** Note that we cannot have several nodes with the equal keys in the tree, so* there is no ambiguity about which node will be found.** Also note that the search procedure does not depend on the tree balancing* rules, but because the tree is balanced, we know that the search procedure* will always be efficient.** RETURNS: pointer to the node whose key equals <key>, or NULL if there is* no such node in the tree*/void * avlSearch ( AVL_TREE root, /* root node pointer */ GENERIC_ARGUMENT key, /* search key */ int compare (void *, GENERIC_ARGUMENT) /* comparison function */ ) { AVL_NODE * nodep; /* pointer to the current node */ nodep = root; while (1) { int delta; /* result of the comparison operation */ if (nodep == NULL) return NULL; /* not found ! */ delta = compare (nodep, key); if (0 == delta) return nodep; /* found the node */ else if (delta < 0) nodep = nodep->left; else nodep = nodep->right; } } /********************************************************************************* avlSearchUnsigned - search a node in an AVL tree** This is a specialized implementation of avlSearch for cases where the* node to be searched is an AVL_UNSIGNED_NODE.** RETURNS: pointer to the node whose key equals <key>, or NULL if there is* no such node in the tree*/void * avlSearchUnsigned ( AVL_TREE root, /* root node pointer */ UINT key /* search key */ ) { AVL_UNSIGNED_NODE * nodep; /* pointer to the current node */ nodep = (AVL_UNSIGNED_NODE *) root; while (1) { if (nodep == NULL) return NULL; /* not found ! */ if (key == nodep->key) return nodep; /* found the node */ else if (key < nodep->key) nodep = nodep->avl.left; else nodep = nodep->avl.right; } }/********************************************************************************* avlInsert - insert a node in an AVL tree** At the time of the call, <root> points to the root node pointer. This root* node pointer is possibly NULL if the tree is empty. <newNode> points to the* node we want to insert. His left, right and height fields need not be filled,* but the user will probably have added his own data fields after those. <key>* is newNode's key, that will be passed to the comparison function. This is* redundant because it could really be derived from newNode, but the way to do* this depends on the precise type of newNode so we cannot do this in a generic* routine. <compare> is the user-provided comparison function.** Note that we cannot have several nodes with the equal keys in the tree, so* the insertion operation will fail if we try to insert a node that has a* duplicate key.** Also note that because we keep the tree balanced, the root node pointer that* is pointed by the <root> argument can be modified in this function.** INTERNAL* The insertion routine works just like in a non-balanced binary tree : we* walk down the tree like if we were searching a node, and when we reach a leaf* node we insert newNode at this position.** Because the balancing procedure needs to be able to walk back to the root* node, we keep a list of pointers to the pointers we followed on our way down* the tree.** RETURNS: OK, or ERROR if the tree already contained a node with the same key*/STATUS avlInsert ( AVL_TREE * root, /* pointer to the root node ptr */ void * newNode, /* ptr to the node we want to insert */ GENERIC_ARGUMENT key, /* search key of newNode */ int compare (void *, GENERIC_ARGUMENT) /* comparison function */ ) { AVL_NODE ** nodepp; /* ptr to current node ptr */ AVL_NODE ** ancestor[AVL_MAX_HEIGHT]; /* list of pointers to all our ancestor node ptrs */ int ancestorCount; /* number of ancestors */ nodepp = root; ancestorCount = 0; while (1) { AVL_NODE * nodep; /* pointer to the current node */ int delta; /* result of the comparison operation */ nodep = *nodepp; if (nodep == NULL) break; /* we can insert a leaf node here ! */ ancestor[ancestorCount++] = nodepp; delta = compare (nodep, key); if (0 == delta) return ERROR; else if (delta < 0) nodepp = (AVL_NODE **)&(nodep->left); else nodepp = (AVL_NODE **)&(nodep->right); } ((AVL_NODE *)newNode)->left = NULL; ((AVL_NODE *)newNode)->right = NULL; ((AVL_NODE *)newNode)->height = 1; *nodepp = newNode; avlRebalance (ancestor, ancestorCount); return OK; }/********************************************************************************* avlInsertUnsigned - insert a node in an AVL tree** This is a specialized implementation of avlInsert for cases where the* node to be inserted is an AVL_UNSIGNED_NODE.** RETURNS: OK, or ERROR if the tree already contained a node with the same key*/STATUS avlInsertUnsigned ( AVL_TREE * root, /* pointer to the root node ptr */ void * newNode /* ptr to the node we want to insert */ ) { AVL_UNSIGNED_NODE ** nodepp; /* ptr to current node ptr */ AVL_UNSIGNED_NODE ** ancestor[AVL_MAX_HEIGHT]; /* list of pointers to all our ancestor node ptrs */ int ancestorCount; /* number of ancestors */ UINT key; key = ((AVL_UNSIGNED_NODE *)newNode)->key; nodepp = (AVL_UNSIGNED_NODE **) root; ancestorCount = 0; while (1) { AVL_UNSIGNED_NODE * nodep; /* pointer to the current node */ nodep = *nodepp; if (nodep == NULL) break; /* we can insert a leaf node here ! */ ancestor[ancestorCount++] = nodepp; if (key == nodep->key) return ERROR; else if (key < nodep->key) nodepp = (AVL_UNSIGNED_NODE **)&(nodep->avl.left); else nodepp = (AVL_UNSIGNED_NODE **)&(nodep->avl.right); } ((AVL_NODE *)newNode)->left = NULL; ((AVL_NODE *)newNode)->right = NULL; ((AVL_NODE *)newNode)->height = 1; *nodepp = newNode; avlRebalance ((AVL_NODE ***)ancestor, ancestorCount); return OK; }/********************************************************************************* avlDelete - delete a node in an AVL tree** At the time of the call, <root> points to the root node pointer and* <key> is the key of the node we want to delete. <compare> is the* user-provided comparison function.** The deletion operation will of course fail if the desired node was not* already in the tree.** Also note that because we keep the tree balanced, the root node pointer that* is pointed by the <root> argument can be modified in this function.** On exit, the node is removed from the AVL tree but it is not free()'d.** INTERNAL* The deletion routine works just like in a non-balanced binary tree : we* walk down the tree like searching the node we have to delete. When we find* it, if it is not a leaf node, we have to replace it with a leaf node that* has an adjacent key. Then the rebalancing operation will have to enforce the* balancing rules by walking up the path from the leaf node that got deleted* or moved.** Because the balancing procedure needs to be able to walk back to the root* node, we keep a list of pointers to the pointers we followed on our way down* the tree.** RETURNS: pointer to the node we deleted, or NULL if the tree does not* contain any such node*/void * avlDelete ( AVL_TREE * root, /* pointer to the root node pointer */ GENERIC_ARGUMENT key, /* search key of node we want to delete */ int compare (void *, GENERIC_ARGUMENT) /* comparison function */ ) { AVL_NODE ** nodepp; /* ptr to current node ptr */ AVL_NODE * nodep; /* ptr to the current node */ AVL_NODE ** ancestor[AVL_MAX_HEIGHT]; /* list of pointers to all our ancestor node pointers */ int ancestorCount; /* number of ancestors */ AVL_NODE * deletep; /* ptr to the node we have to delete */ nodepp = root; ancestorCount = 0; while (1) { int delta; /* result of the comparison operation */ nodep = *nodepp; if (nodep == NULL) return NULL; /* node was not in the tree ! */ ancestor[ancestorCount++] = nodepp; delta = compare (nodep, key); if (0 == delta) break; /* we found the node we have to delete */ else if (delta < 0) nodepp = (AVL_NODE **)&(nodep->left); else nodepp = (AVL_NODE **)&(nodep->right); } deletep = nodep; if (nodep->left == NULL) { /* * There is no node on the left subtree of delNode. * Either there is one (and only one, because of the balancing rules) * on its right subtree, and it replaces delNode, or it has no child * nodes at all and it just gets deleted */ *nodepp = nodep->right; /* * we know that nodep->right was already balanced so we don't have to * check it again */ ancestorCount--; } else { /* * We will find the node that is just before delNode in the ordering * of the tree and promote it to delNode's position in the tree. */ AVL_NODE ** deletepp; /* ptr to the ptr to the node we have to delete */ int deleteAncestorCount; /* place where the replacing node will have to be inserted in the ancestor list */ deleteAncestorCount = ancestorCount; deletepp = nodepp; deletep = nodep; /* search for node just before delNode in the tree ordering */ nodepp = (AVL_NODE **)&(nodep->left); while (1) { nodep = *nodepp; if (nodep->right == NULL) break; ancestor[ancestorCount++] = nodepp; nodepp = (AVL_NODE **)&(nodep->right); } /* * this node gets replaced by its (unique, because of balancing rules) * left child, or deleted if it has no childs at all */ *nodepp = nodep->left; /* now this node replaces delNode in the tree */ nodep->left = deletep->left; nodep->right = deletep->right; nodep->height = deletep->height; *deletepp = nodep; /* * We have replaced delNode with nodep. Thus the pointer to the left * subtree of delNode was stored in delNode->left and it is now * stored in nodep->left. We have to adjust the ancestor list to * reflect this. */ ancestor[deleteAncestorCount] = (AVL_NODE **)&(nodep->left); } avlRebalance (ancestor, ancestorCount); return deletep; }/********************************************************************************* avlDeleteUnsigned - delete a node in an AVL tree** This is a specialized implementation of avlDelete for cases where the* node to be deleted is an AVL_UNSIGNED_NODE.** RETURNS: pointer to the node we deleted, or NULL if the tree does not* contain any such node*/void * avlDeleteUnsigned ( AVL_TREE * root, /* pointer to the root node pointer */ UINT key /* search key of node we want to delete */ ) { AVL_UNSIGNED_NODE ** nodepp; /* ptr to current node ptr */ AVL_UNSIGNED_NODE * nodep; /* ptr to the current node */ AVL_UNSIGNED_NODE ** ancestor[AVL_MAX_HEIGHT]; /* list of pointers to all our ancestor node pointers */ int ancestorCount; /* number of ancestors */ AVL_UNSIGNED_NODE * deletep; /* ptr to the node we have to delete */ nodepp = (AVL_UNSIGNED_NODE **)root; ancestorCount = 0; while (1) { nodep = *nodepp; if (nodep == NULL) return NULL; /* node was not in the tree ! */ ancestor[ancestorCount++] = nodepp; if (key == nodep->key) break; /* we found the node we have to delete */ else if (key < nodep->key) nodepp = (AVL_UNSIGNED_NODE **)&(nodep->avl.left); else nodepp = (AVL_UNSIGNED_NODE **)&(nodep->avl.right); } deletep = nodep; if (nodep->avl.left == NULL) { /* * There is no node on the left subtree of delNode. * Either there is one (and only one, because of the balancing rules) * on its right subtree, and it replaces delNode, or it has no child * nodes at all and it just gets deleted */ *nodepp = nodep->avl.right; /* * we know that nodep->right was already balanced so we don't have to * check it again */ ancestorCount--; } else { /* * We will find the node that is just before delNode in the ordering * of the tree and promote it to delNode's position in the tree. */ AVL_UNSIGNED_NODE ** deletepp; /* ptr to the ptr to the node we have to delete */ int deleteAncestorCount; /* place where the replacing node will have to be inserted in the ancestor list */ deleteAncestorCount = ancestorCount; deletepp = nodepp; deletep = nodep; /* search for node just before delNode in the tree ordering */ nodepp = (AVL_UNSIGNED_NODE **)&(nodep->avl.left); while (1) { nodep = *nodepp; if (nodep->avl.right == NULL) break; ancestor[ancestorCount++] = nodepp; nodepp = (AVL_UNSIGNED_NODE **)&(nodep->avl.right); } /* * this node gets replaced by its (unique, because of balancing rules) * left child, or deleted if it has no childs at all */ *nodepp = nodep->avl.left; /* now this node replaces delNode in the tree */ nodep->avl.left = deletep->avl.left; nodep->avl.right = deletep->avl.right; nodep->avl.height = deletep->avl.height; *deletepp = nodep; /* * We have replaced delNode with nodep. Thus the pointer to the left * subtree of delNode was stored in delNode->left and it is now * stored in nodep->left. We have to adjust the ancestor list to * reflect this. */ ancestor[deleteAncestorCount] = (AVL_UNSIGNED_NODE **)&(nodep->avl.left); } avlRebalance ((AVL_NODE ***)ancestor, ancestorCount); return deletep; }/********************************************************************************* avlSuccessorGet - find node with key successor to input key on an AVL tree** At the time of the call, <root> is the root node pointer. <key> is the value* we want to search, and <compare> is the user-provided comparison function.** Note that we cannot have several nodes with the equal keys in the tree, so* there is no ambiguity about which node will be found.** Also note that the search procedure does not depend on the tree balancing* rules, but because the tree is balanced, we know that the search procedure* will always be efficient.** RETURNS: pointer to the node whose key is the immediate successor of <key>,* or NULL if there is no such node in the tree*/void * avlSuccessorGet ( AVL_TREE root, /* root node pointer */ GENERIC_ARGUMENT key, /* search key */ int compare (void *, GENERIC_ARGUMENT) /* comparison function */ ) { AVL_NODE * nodep; /* pointer to the current node */ AVL_NODE * superiorp; /* pointer to the current superior*/ nodep = root; superiorp = NULL; while (1) { int delta; /* result of the comparison operation */ if (nodep == NULL) return superiorp; delta = compare (nodep, key); if (delta < 0) { superiorp = nodep; /* update superiorp */ nodep = nodep->left; } else nodep = nodep->right; } }/********************************************************************************* avlPredecessorGet - find node with key predecessor to input key on an AVL tree** At the time of the call, <root> is the root node pointer. <key> is the value* we want to search, and <compare> is the user-provided comparison function.** Note that we cannot have several nodes with the equal keys in the tree, so* there is no ambiguity about which node will be found.** Also note that the search procedure does not depend on the tree balancing* rules, but because the tree is balanced, we know that the search procedure* will always be efficient.** RETURNS: pointer to the node whose key is the immediate predecessor of <key>,* or NULL if there is no such node in the tree*/void * avlPredecessorGet ( AVL_TREE root, /* root node pointer */ GENERIC_ARGUMENT key, /* search key */ int compare (void *, GENERIC_ARGUMENT) /* comparison function */ ) { AVL_NODE * nodep; /* pointer to the current node */ AVL_NODE * inferiorp; /* pointer to the current inferior*/ nodep = root; inferiorp = NULL; while (1) { int delta; /* result of the comparison operation */ if (nodep == NULL) return inferiorp; delta = compare (nodep, key); if (delta > 0) { inferiorp = nodep; /* update inferiorp */ nodep = nodep->right; } else nodep = nodep->left; } }/********************************************************************************* avlMinimumGet - find node with minimum key** At the time of the call, <root> is the root node pointer. <key> is the value* we want to search, and <compare> is the user-provided comparison function.** RETURNS: pointer to the node with minimum key; NULL if the tree is empty*/void * avlMinimumGet ( AVL_TREE root /* root node pointer */ ) { if (NULL == root) return NULL; while (root->left != NULL) { root = root->left; } return root; }/********************************************************************************* avlMaximumGet - find node with maximum key** At the time of the call, <root> is the root node pointer. <key> is the value* we want to search, and <compare> is the user-provided comparison function.** RETURNS: pointer to the node with maximum key; NULL if the tree is empty*/void * avlMaximumGet ( AVL_TREE root /* root node pointer */ ) { if (NULL == root) return NULL; while (root->right != NULL) { root = root->right; } return root; }/********************************************************************************* avlInsertInform - insert a node in an AVL tree and report key holder** At the time of the call, <pRoot> points to the root node pointer. This root* node pointer is possibly NULL if the tree is empty. <pNewNode> points to the* node we want to insert. His left, right and height fields need not be filled,* but the user will probably have added his own data fields after those. <key>* is newNode's key, that will be passed to the comparison function. This is* redundant because it could really be derived from newNode, but the way to do* this depends on the precise type of newNode so we cannot do this in a generic* routine. <compare> is the user-provided comparison function.** Note that we cannot have several nodes with the equal keys in the tree, so* the insertion operation will fail if we try to insert a node that has a* duplicate key. However, if the <replace> boolean is set to true then in* case of conflict we will remove the old node, we will put in its position the* new one, and we will return the old node pointer in the postion pointed by* <ppReplacedNode>.** Also note that because we keep the tree balanced, the root node pointer that* is pointed by the <pRoot> argument can be modified in this function.** INTERNAL* The insertion routine works just like in a non-balanced binary tree : we* walk down the tree like if we were searching a node, and when we reach a leaf* node we insert newNode at this position.** Because the balancing procedure needs to be able to walk back to the root* node, we keep a list of pointers to the pointers we followed on our way down* the tree.** RETURNS: OK, or ERROR if the tree already contained a node with the same key* and replacement was not allowed. In both cases it will place a pointer to* the key holder in the position pointed by <ppKeyHolder>.*/STATUS avlInsertInform ( AVL_TREE * pRoot, /* ptr to the root node pointer */ void * pNewNode, /* pointer to the candidate node */ GENERIC_ARGUMENT key, /* unique key of new node */ void ** ppKeyHolder, /* ptr to final key holder */ int compare (void *, GENERIC_ARGUMENT) /* comparison function */ ) { AVL_NODE ** nodepp; /* ptr to current node ptr */ AVL_NODE ** ancestor[AVL_MAX_HEIGHT]; /* list of pointers to all our ancestor node ptrs */ int ancestorCount; /* number of ancestors */ if (NULL == ppKeyHolder) { printf("invalid input data were passed to avlInsertInform/n"); return ERROR; }; nodepp = pRoot; ancestorCount = 0; while (1) { AVL_NODE * nodep; /* pointer to the current node */ int delta; /* result of the comparison operation */ nodep = *nodepp; if (nodep == NULL) break; /* we can insert a leaf node here ! */ ancestor[ancestorCount++] = nodepp; delta = compare (nodep, key); if (0 == delta) { /* we inform the caller of the key holder node and return ERROR */ *ppKeyHolder = nodep; return ERROR; } else if (delta < 0) nodepp = (AVL_NODE **)&(nodep->left); else nodepp = (AVL_NODE **)&(nodep->right); } ((AVL_NODE *)pNewNode)->left = NULL; ((AVL_NODE *)pNewNode)->right = NULL; ((AVL_NODE *)pNewNode)->height = 1; *nodepp = pNewNode; *ppKeyHolder = pNewNode; avlRebalance (ancestor, ancestorCount); return OK; }/********************************************************************************* avlRemoveInsert - forcefully insert a node in an AVL tree** At the time of the call, <pRoot> points to the root node pointer. This root* node pointer is possibly NULL if the tree is empty. <pNewNode> points to the* node we want to insert. His left, right and height fields need not be filled,* but the user will probably have added his own data fields after those. <key>* is newNode's key, that will be passed to the comparison function. This is* redundant because it could really be derived from newNode, but the way to do* this depends on the precise type of newNode so we cannot do this in a generic* routine. <compare> is the user-provided comparison function.** Note that we cannot have several nodes with the equal keys in the tree, so* the insertion operation will fail if we try to insert a node that has a* duplicate key. However, in case of conflict we will remove the old node, we * will put in its position the new one, and we will return the old node pointer** Also note that because we keep the tree balanced, the root node pointer that* is pointed by the <pRoot> argument can be modified in this function.** INTERNAL* The insertion routine works just like in a non-balanced binary tree : we* walk down the tree like if we were searching a node, and when we reach a leaf* node we insert newNode at this position.** Because the balancing procedure needs to be able to walk back to the root* node, we keep a list of pointers to the pointers we followed on our way down* the tree.** RETURNS: NULL if insertion was carried out without replacement, or if * replacement occured the pointer to the replaced node**/void * avlRemoveInsert ( AVL_TREE * pRoot, /* ptr to the root node pointer */ void * pNewNode, /* pointer to the candidate node */ GENERIC_ARGUMENT key, /* unique key of new node */ int compare (void *, GENERIC_ARGUMENT) /* comparison function */ ) { AVL_NODE ** nodepp; /* ptr to current node ptr */ AVL_NODE ** ancestor[AVL_MAX_HEIGHT]; /* list of pointers to all our ancestor node ptrs */ int ancestorCount; /* number of ancestors */ nodepp = pRoot; ancestorCount = 0; while (1) { AVL_NODE * nodep; /* pointer to the current node */ int delta; /* result of the comparison operation */ nodep = *nodepp; if (nodep == NULL) break; /* we can insert a leaf node here ! */ ancestor[ancestorCount++] = nodepp; delta = compare (nodep, key); if (0 == delta) { /* we copy the tree data from the old node to the new node */ ((AVL_NODE *)pNewNode)->left = nodep->left; ((AVL_NODE *)pNewNode)->right = nodep->right; ((AVL_NODE *)pNewNode)->height = nodep->height; /* and we make the new node child of the old node's parent */ *nodepp = pNewNode; /* before we return it we sterilize the old node */ nodep->left = NULL; nodep->right = NULL; nodep->height = 1; return nodep; } else if (delta < 0) nodepp = (AVL_NODE **)&(nodep->left); else nodepp = (AVL_NODE **)&(nodep->right); } ((AVL_NODE *)pNewNode)->left = NULL; ((AVL_NODE *)pNewNode)->right = NULL; ((AVL_NODE *)pNewNode)->height = 1; *nodepp = pNewNode; avlRebalance (ancestor, ancestorCount); return NULL; }/********************************************************************************* avlTreeWalk- walk the whole tree and execute a function on each node** At the time of the call, <pRoot> points to the root node pointer.** RETURNS: OK always**/STATUS avlTreeWalk(AVL_TREE * pRoot, void walkExec(AVL_TREE * ppNode)) { if ((NULL == pRoot) || (NULL == *pRoot)) { return OK; }; if (!(NULL == (*pRoot)->left)) { avlTreeWalk((AVL_TREE *)(&((*pRoot)->left)), walkExec); } if (!(NULL == (*pRoot)->right)) { avlTreeWalk((AVL_TREE *)(&((*pRoot)->right)), walkExec); } walkExec(pRoot); return OK; }STATUS avlTreeWalkWithPara(AVL_TREE * pRoot, void walkExec(AVL_TREE * ppNode, void * v_Para), void * v_Para) { if ((NULL == pRoot) || (NULL == *pRoot)) { return OK; }; if (!(NULL == (*pRoot)->left)) { avlTreeWalkWithPara((AVL_TREE *)(&((*pRoot)->left)), walkExec, v_Para); } if (!(NULL == (*pRoot)->right)) { avlTreeWalkWithPara((AVL_TREE *)(&((*pRoot)->right)), walkExec, v_Para); } walkExec(pRoot, v_Para); return OK; }/********************************************************************************* avlTreePrint- print the whole tree** At the time of the call, <pRoot> points to the root node pointer.** RETURNS: OK always**/STATUS avlTreePrint(AVL_TREE * pRoot, void printNode(void * nodep)) { if ((NULL == pRoot) || (NULL == *pRoot)) { return OK; }; printNode(*pRoot); if (!(NULL == (*pRoot)->left)) { avlTreePrint((AVL_TREE *)(&((*pRoot)->left)), printNode); } if (!(NULL == (*pRoot)->right)) { avlTreePrint((AVL_TREE *)(&((*pRoot)->right)), printNode); } return OK; }/********************************************************************************* avlTreeErase - erase the whole tree** At the time of the call, <pRoot> points to the root node pointer.* Unlike the avlDelete routine here we do perform memory management* ASSUMING that all nodes carry shallow data only. Otherwise we should* use avlTreeWalk with the appropriate walkExec memory freeing function.* Since we do not plan to reuse the tree intermediate rebalancing is not needed.** RETURNS: OK always**/STATUS avlTreeErase(AVL_TREE * pRoot) { if ((NULL == pRoot) || (NULL == *pRoot)) { return OK; }; if (!(NULL == (*pRoot)->left)) { avlTreeErase((AVL_TREE *)(&((*pRoot)->left))); } if (!(NULL == (*pRoot)->right)) { avlTreeErase((AVL_TREE *)(&((*pRoot)->right))); } free(*pRoot); *pRoot = NULL; return OK; }#ifndef AVLTREE_FREE_FUNC_DEFINED#define AVLTREE_FREE_FUNC_DEFINEDtypedef void (*AVLTREE_FREE_FUNC)(void *);#endifSTATUS avlTreeEraseWithFunc(AVL_TREE * pRoot, AVLTREE_FREE_FUNC v_Func) { if ((NULL == pRoot) || (NULL == *pRoot) || (NULL == v_Func)) { return OK; }; if (!(NULL == (*pRoot)->left)) { avlTreeEraseWithFunc((AVL_TREE *)(&((*pRoot)->left)), v_Func); } if (!(NULL == (*pRoot)->right)) { avlTreeEraseWithFunc((AVL_TREE *)(&((*pRoot)->right)), v_Func); } v_Func(*pRoot); *pRoot = NULL; return OK; }/********************************************************************************* avlTreePrintErase - erase the whole tree assuming that all nodes were* created using malloc.** At the time of the call, <pRoot> points to the root node pointer.* Unlike the avlDelete routine here we do perform memory management.* Since we do not plan to reuse the tree intermediate rebalancing is not needed.** RETURNS: OK always and assigns NULL to *pRoot.**/STATUS avlTreePrintErase(AVL_TREE * pRoot, void printNode(void * nodep)) { if ((NULL == pRoot) || (NULL == *pRoot)) { return OK; }; printNode(*pRoot); if (!(NULL == (*pRoot)->left)) { avlTreePrintErase((AVL_TREE *)(&((*pRoot)->left)), printNode); free((*pRoot)->left); (*pRoot)->left = NULL; } if (!(NULL == (*pRoot)->right)) { avlTreePrintErase((AVL_TREE *)(&((*pRoot)->right)), printNode); free((*pRoot)->right); (*pRoot)->right = NULL; } free(*pRoot); *pRoot = NULL; return OK; }
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
亚洲男人天堂2023| 91精品久久久久久久久久久久久| 欧美巨乳美女视频| 秋霞午夜一区二区| 国产精品啪视频| 91欧美日韩一区| 国产精品无av码在线观看| 欧美性少妇18aaaa视频| 成人xvideos免费视频| 国产色婷婷国产综合在线理论片a| 91精品一区二区| 久久91精品国产| 97免费中文视频在线观看| 欧洲亚洲在线视频| 91精品视频在线免费观看| 亚洲欧美日韩一区二区在线| 欧美在线性爱视频| 精品日韩美女的视频高清| 国产精品免费久久久久久| 亚洲欧美成人一区二区在线电影| 国产日本欧美一区二区三区| 午夜免费日韩视频| 亚洲精品电影久久久| 欧美国产精品人人做人人爱| 亚洲免费电影在线观看| 久久亚洲私人国产精品va| 97人人模人人爽人人喊中文字| 欧美天天综合色影久久精品| 亚洲精品之草原avav久久| 成人黄色大片在线免费观看| 日韩在线观看免费av| 免费99精品国产自在在线| 91理论片午午论夜理片久久| 最新亚洲国产精品| 欧美成人在线免费视频| 欧美午夜xxx| 国产精品网址在线| 成人国产精品一区二区| 中文字幕在线日韩| 欧美做爰性生交视频| 欧美一级视频免费在线观看| 日韩最新免费不卡| 日韩精品有码在线观看| 日韩小视频在线| 欧美激情综合色| 成人精品一区二区三区| 韩日欧美一区二区| 欧美在线视频一二三| 成人激情黄色网| 亚洲最大福利网| 欧美激情精品久久久久久大尺度| 亚洲第一男人av| 精品国产欧美一区二区三区成人| 亚洲精品xxx| 中文字幕自拍vr一区二区三区| 久久精品久久久久| 国产午夜精品视频免费不卡69堂| 国产激情视频一区| 在线电影av不卡网址| 欧美激情图片区| 亚洲激情免费观看| 国产精品视频播放| 91精品国产综合久久香蕉922| 视频在线观看99| 欧美综合第一页| 久久亚洲电影天堂| 欧美成人h版在线观看| 精品一区二区三区四区在线| 国产精品一区二区三区在线播放| 欧亚精品在线观看| 国产一区二区在线免费| 日韩高清av在线| 色综合久久88色综合天天看泰| 国产精品一区久久| 国产精品大陆在线观看| 国产精品高潮呻吟久久av无限| 国产午夜精品麻豆| 97精品在线观看| 日韩免费视频在线观看| 国产精品入口免费视频一| 欧美激情一级欧美精品| 91亚洲精品久久久久久久久久久久| 91精品国产高清自在线| 久久久爽爽爽美女图片| 精品香蕉一区二区三区| 色偷偷av一区二区三区乱| 国产精品专区h在线观看| 蜜臀久久99精品久久久久久宅男| 亚洲一区亚洲二区亚洲三区| 一区二区国产精品视频| 久久视频在线免费观看| 国产精品手机播放| 日韩欧美在线第一页| 亚洲天堂免费在线| 国产欧美亚洲视频| 国产精品免费在线免费| 亚洲激情 国产| 欧美午夜www高清视频| 免费不卡欧美自拍视频| 亚洲人成网站在线播| 国产精品69精品一区二区三区| 亚洲福利影片在线| 深夜成人在线观看| 91免费高清视频| 国产精品av免费在线观看| 精品久久久久久久久久国产| 久久九九有精品国产23| 亚洲第一天堂无码专区| 欧美激情视频一区二区三区不卡| 97视频免费看| 久久久国产视频91| 大胆人体色综合| 两个人的视频www国产精品| 狠狠躁夜夜躁久久躁别揉| 在线精品高清中文字幕| 在线观看日韩欧美| 狠狠色噜噜狠狠狠狠97| 97视频在线免费观看| 成人黄色av播放免费| 欧美视频在线免费看| 欧美成人免费小视频| 国产精品久久久久aaaa九色| 日本精品久久久久久久| 亚洲免费视频一区二区| 精品在线欧美视频| 国产精品免费观看在线| 欧美另类精品xxxx孕妇| 日韩经典中文字幕| 亚洲品质视频自拍网| 色爱av美腿丝袜综合粉嫩av| 欧美电影院免费观看| 久久久精品中文字幕| 国产一区二区三区在线播放免费观看| 成人在线视频福利| 日韩精品视频在线观看免费| 亚洲国产欧美一区二区三区同亚洲| 国产成人欧美在线观看| 日韩av免费在线看| 欧美交受高潮1| 国产精品爱久久久久久久| 日本19禁啪啪免费观看www| 国产69精品99久久久久久宅男| 777精品视频| 91精品国产91久久| 一区二区三区无码高清视频| 久久成人精品视频| 国产亚洲aⅴaaaaaa毛片| 欧美精品在线极品| 欧美亚洲国产成人精品| 亚洲欧美中文字幕| 亚洲精品乱码久久久久久按摩观| 成人精品一区二区三区电影黑人| 亚洲品质视频自拍网| 91免费观看网站| 国产精品入口免费视频一| 国产一区二区激情| 亚洲韩国欧洲国产日产av| 性色av一区二区三区在线观看| 欧美性猛交xxxx乱大交极品| 亚洲精品丝袜日韩| 一区二区亚洲精品国产| 国产啪精品视频| 66m—66摸成人免费视频| 久久99精品久久久久久琪琪|