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

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

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

2019-11-11 06:10: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
www高清在线视频日韩欧美| 在线中文字幕日韩| 午夜精品久久久久久久白皮肤| 在线日韩欧美视频| 久久国产精品电影| 久久久综合av| 日韩在线激情视频| 久久精品影视伊人网| 91精品久久久久久久久久| 国产丝袜一区二区三区| 欧美成人精品一区二区三区| 日韩av片电影专区| 国产精品久久视频| 茄子视频成人在线| 日韩欧美黄色动漫| 国产精品欧美日韩一区二区| 国产精品91久久久| 亚洲97在线观看| 亚洲va久久久噜噜噜久久天堂| 91在线视频九色| 国产精品久久久久99| 成人a免费视频| 国产精品视频成人| 亚洲高清一区二| 国产一区二区黄| 久久久久久久久久久久久久久久久久av| 欧美午夜激情小视频| 欧美成年人视频| 91精品国产自产在线老师啪| 麻豆国产va免费精品高清在线| 日韩成人激情视频| 色婷婷亚洲mv天堂mv在影片| 日韩视频―中文字幕| 欧美成年人网站| 国产精品久久久久久久久久尿| 欧美日韩在线视频首页| 高跟丝袜欧美一区| 日韩日本欧美亚洲| 国产脚交av在线一区二区| 久久视频这里只有精品| 国产精品久久久久久久久久久久| 亚洲精品免费av| 国产精品h在线观看| 91精品国产91久久久久久吃药| 国产精品r级在线| 欧美影院久久久| 久久久久久美女| 亚洲精品一区二区三区婷婷月| 国产亚洲欧美另类中文| 91精品国产综合久久男男| 欧美大秀在线观看| 欧美国产日韩xxxxx| 日韩亚洲精品电影| 91sao在线观看国产| 欧美巨大黑人极品精男| 96精品视频在线| 激情久久av一区av二区av三区| 宅男66日本亚洲欧美视频| 亚洲成人a级网| 8090理伦午夜在线电影| 日韩在线视频二区| 91九色综合久久| 在线精品91av| 亚洲欧美日韩国产成人| 欧美另类99xxxxx| 日韩在线视频观看正片免费网站| 午夜精品福利电影| 亚洲精品电影网在线观看| 日日摸夜夜添一区| 日韩电影免费观看在线观看| 91久久精品国产91久久性色| 激情亚洲一区二区三区四区| 热久久美女精品天天吊色| 国产精品久久久久久av福利软件| 美日韩精品视频免费看| www.国产精品一二区| 欧美一级视频一区二区| 国产suv精品一区二区三区88区| 性色av一区二区三区红粉影视| 日韩av免费在线看| 国产精品久久久久免费a∨大胸| 国产97免费视| 亚洲精品久久久久久下一站| 国产精品人人做人人爽| 亚洲男子天堂网| 日韩av网址在线观看| 日韩精品免费综合视频在线播放| 日韩激情av在线免费观看| 亚洲一区二区三区sesese| 国产91亚洲精品| 日本高清不卡的在线| 国产精品美女www爽爽爽视频| 一区二区三区国产视频| 国产日韩欧美在线观看| 国产91ⅴ在线精品免费观看| 国产精品va在线| 亚洲成人久久久久| 日本中文字幕成人| 一区二区亚洲欧洲国产日韩| 欧美黑人狂野猛交老妇| 欧美日韩国产一区在线| 亚洲国产日韩欧美综合久久| 日韩一级黄色av| 欧美午夜视频在线观看| 亚洲人午夜色婷婷| 亚洲国语精品自产拍在线观看| 亚洲精品一区二区三区不| 欧洲永久精品大片ww免费漫画| 成人字幕网zmw| 亚洲精品短视频| 亚洲精品乱码久久久久久按摩观| 欧美亚洲伦理www| 国产精品久久久av| 91久久国产综合久久91精品网站| 国产精品普通话| 成人性生交大片免费观看嘿嘿视频| 国产精品久久久久久网站| 欧美日韩国产在线播放| 亚洲人成在线观看网站高清| 成人黄色av播放免费| 欧美国产高跟鞋裸体秀xxxhd| 国产成人一区二区三区小说| 国产精品白丝jk喷水视频一区| 国产乱人伦真实精品视频| 日韩av片免费在线观看| 日韩在线一区二区三区免费视频| 成人妇女免费播放久久久| 亚洲欧美日韩一区二区在线| 91精品在线影院| 在线观看欧美视频| 青青草原一区二区| 亚洲综合精品一区二区| 欧美视频在线观看免费网址| 欧美在线免费视频| 国产精品嫩草视频| 91精品久久久久久久久久另类| 综合av色偷偷网| 色综合天天综合网国产成人网| 日韩毛片中文字幕| 精品成人国产在线观看男人呻吟| 亚洲黄在线观看| 欧美极品xxxx| 色爱精品视频一区| 欧美日韩高清区| 欧洲成人在线观看| 亚洲视频在线免费看| 日韩中文综合网| 久久久黄色av| 国产精品三级在线| 日韩av不卡电影| 人九九综合九九宗合| 久久久中精品2020中文| 日韩精品在线观看一区二区| 亚洲精品日韩丝袜精品| 精品国产鲁一鲁一区二区张丽| 国产成人精品久久二区二区91| 亚洲视频一区二区| 91禁外国网站| 中文字幕日韩精品在线观看| 亚洲国产一区自拍| 国产啪精品视频| 日韩精品亚洲视频| 日韩中文av在线| 国产精品偷伦免费视频观看的|