Changeset 3555 for trunk/kStuff/include/k/kAVLTmpl/kAVLGetBestFit.h
- Timestamp:
- Aug 26, 2007, 12:43:27 PM (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/kStuff/include/k/kAVLTmpl/kAVLGetBestFit.h
r3553 r3555 1 1 /* $Id$ */ 2 2 /** @file 3 * 4 * kAVLGetBestFit - Get Best Fit routine for AVL trees. 5 * Intended specially on heaps. The tree should allow duplicate keys. 6 * 7 * Copyright (c) 1999-2002 knut st. osmundsen (bird@anduin.net) 8 * 9 * GPL 3 * kAVLTmpl - Templated AVL Trees, Get Best Fitting Node. 10 4 */ 11 5 12 #ifndef _kAVLGetBestFit_h_ 13 #define _kAVLGetBestFit_h_ 6 /* 7 * Copyright (c) 1999-2007 knut st. osmundsen <bird-src-spam@anduin.net> 8 * 9 * This file is part of kStuff. 10 * 11 * kStuff is free software; you can redistribute it and/or 12 * modify it under the terms of the GNU Lesser General Public 13 * License as published by the Free Software Foundation; either 14 * version 2.1 of the License, or (at your option) any later version. 15 * 16 * kStuff is distributed in the hope that it will be useful, 17 * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 * Lesser General Public License for more details. 20 * 21 * You should have received a copy of the GNU Lesser General Public 22 * License along with kStuff; if not, write to the Free Software 23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 24 * 25 */ 14 26 15 27 16 28 /** 17 29 * Finds the best fitting node in the tree for the given Key value. 18 * @returns Pointer to the best fitting node found. 19 * @param ppTree Pointer to Pointer to the tree root node. 20 * @param Key The Key of which is to be found a best fitting match for.. 21 * @param fAbove TRUE: Returned node is have the closest key to Key from above. 22 * FALSE: Returned node is have the closest key to Key from below. 23 * @status completely implemented. 24 * @sketch The best fitting node is always located in the searchpath above you. 25 * >= (above): The node where you last turned left. 26 * <= (below): the node where you last turned right. 27 * @author knut st. osmundsen 30 * 31 * @returns Pointer to the best fitting node found. 32 * @param ppTree Pointer to Pointer to the tree root node. 33 * @param Key The Key of which is to be found a best fitting match for.. 34 * @param fAbove K_TRUE: Returned node is have the closest key to Key from above. 35 * K_FALSE: Returned node is have the closest key to Key from below. 36 * @sketch The best fitting node is always located in the searchpath above you. 37 * >= (above): The node where you last turned left. 38 * <= (below): the node where you last turned right. 28 39 */ 29 PKAVLNODECORE KLIBCALL KAVL_FN(GetBestFit)(PPKAVLNODECOREppTree, KAVLKEY Key, KBOOL fAbove)40 KAVL_DECL(KAVLNODECORE *) KAVL_FN(GetBestFit)(KAVLTREEPTR *ppTree, KAVLKEY Key, KBOOL fAbove) 30 41 { 31 KLOGENTRY3("PKAVLNODECORE","PPKAVLNODECORE ppTree, KAVLKEY Key, KBOOL fAbove", ppTree, Key, fAbove); 32 #ifdef AVL_CMP 33 register int iDiff; 34 #endif 35 register PKAVLNODECORE pNode = *ppTree; 36 PKAVLNODECORE pNodeLast = NULL; 42 register KAVLNODECORE *pNode; 43 KAVLNODECORE *pNodeLast; 37 44 45 if (*ppTree == KAVL_NULL) 46 return NULL; 47 48 pNode = KAVL_GET_POINTER(ppTree); 49 pNodeLast = NULL; 38 50 if (fAbove) 39 51 { /* pNode->Key >= Key */ 40 #ifndef AVL_CMP 41 while (pNode != NULL && AVL_NE(pNode->Key, Key)) 42 #else 43 while (pNode != NULL && (iDiff = AVL_CMP(pNode->Key, Key)) != 0) 44 #endif 52 while (KAVL_NE(pNode->Key, Key)) 45 53 { 46 #ifndef AVL_CMP 47 if (AVL_G(pNode->Key, Key)) 48 #else 49 if (iDiff > 0) 50 #endif 54 if (KAVL_G(pNode->Key, Key)) 51 55 { 56 if (pNode->pLeft == KAVL_NULL) 57 return pNode; 52 58 pNodeLast = pNode; 53 pNode = pNode->pLeft;59 pNode = KAVL_GET_POINTER(&pNode->pLeft); 54 60 } 55 61 else 56 pNode = pNode->pRight; 62 { 63 if (pNode->pRight == KAVL_NULL) 64 return pNodeLast; 65 pNode = KAVL_GET_POINTER(&pNode->pRight); 66 } 57 67 } 58 68 } 59 69 else 60 70 { /* pNode->Key <= Key */ 61 #ifndef AVL_CMP 62 while (pNode != NULL && AVL_NE(pNode->Key, Key)) 63 #else 64 while (pNode != NULL && (iDiff = AVL_CMP(pNode->Key, Key)) != 0) 65 #endif 71 while (KAVL_NE(pNode->Key, Key)) 66 72 { 67 #ifndef AVL_CMP 68 if (AVL_L(pNode->Key, Key)) 69 #else 70 if (iDiff < 0) 71 #endif 73 if (KAVL_G(pNode->Key, Key)) 72 74 { 73 pNodeLast = pNode; 74 pNode = pNode->pRight; 75 if (pNode->pLeft == KAVL_NULL) 76 return pNodeLast; 77 pNode = KAVL_GET_POINTER(&pNode->pLeft); 75 78 } 76 79 else 77 pNode = pNode->pLeft; 80 { 81 if (pNode->pRight == KAVL_NULL) 82 return pNode; 83 pNodeLast = pNode; 84 pNode = KAVL_GET_POINTER(&pNode->pRight); 85 } 78 86 } 79 87 } 80 88 81 KLOGEXIT(pNode == NULL ? pNodeLast /* best fit */ : pNode /* perfect match */);82 return pNode == NULL ? pNodeLast /* best fit */ : pNode /* perfect match */;89 /* perfect match or nothing. */ 90 return pNode; 83 91 } 84 92 85 86 #endif
Note:
See TracChangeset
for help on using the changeset viewer.