1 | /* $Id: avl_RemoveBestFit.cpp.h 65208 2017-01-09 14:39:54Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * kAVLRemoveBestFit - Remove Best Fit routine for AVL trees.
|
---|
4 | * Intended specially on heaps. The tree should allow duplicate keys.
|
---|
5 | *
|
---|
6 | */
|
---|
7 |
|
---|
8 | /*
|
---|
9 | * Copyright (C) 1999-2011 knut st. osmundsen ([email protected])
|
---|
10 | *
|
---|
11 | * Permission is hereby granted, free of charge, to any person
|
---|
12 | * obtaining a copy of this software and associated documentation
|
---|
13 | * files (the "Software"), to deal in the Software without
|
---|
14 | * restriction, including without limitation the rights to use,
|
---|
15 | * copy, modify, merge, publish, distribute, sublicense, and/or sell
|
---|
16 | * copies of the Software, and to permit persons to whom the
|
---|
17 | * Software is furnished to do so, subject to the following
|
---|
18 | * conditions:
|
---|
19 | *
|
---|
20 | * The above copyright notice and this permission notice shall be
|
---|
21 | * included in all copies or substantial portions of the Software.
|
---|
22 | *
|
---|
23 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
---|
24 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
---|
25 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
---|
26 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
---|
27 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
---|
28 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
---|
29 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
---|
30 | * OTHER DEALINGS IN THE SOFTWARE.
|
---|
31 | */
|
---|
32 |
|
---|
33 | #ifndef _kAVLRemoveBestFit_h_
|
---|
34 | #define _kAVLRemoveBestFit_h_
|
---|
35 |
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * Finds the best fitting node in the tree for the given Key value.
|
---|
39 | * And removes it.
|
---|
40 | * @returns Pointer to the best fitting node found.
|
---|
41 | * @param ppTree Pointer to Pointer to the tree root node.
|
---|
42 | * @param Key The Key of which is to be found a best fitting match for..
|
---|
43 | * @param fAbove TRUE: Returned node is have the closest key to Key from above.
|
---|
44 | * FALSE: Returned node is have the closest key to Key from below.
|
---|
45 | * @sketch The best fitting node is always located in the searchpath above you.
|
---|
46 | * >= (above): The node where you last turned left.
|
---|
47 | * <= (below): the node where you last turned right.
|
---|
48 | * @remark This implementation should be speeded up slightly!
|
---|
49 | */
|
---|
50 | KAVL_DECL(PKAVLNODECORE) KAVL_FN(RemoveBestFit)(PPKAVLNODECORE ppTree, KAVLKEY Key, bool fAbove)
|
---|
51 | {
|
---|
52 | /*
|
---|
53 | * If we find anything we'll have to remove the node and return it.
|
---|
54 | * But, if duplicate keys are allowed we'll have to check for multiple
|
---|
55 | * nodes first and return one of them before doing an expensive remove+insert.
|
---|
56 | */
|
---|
57 | PKAVLNODECORE pNode = KAVL_FN(GetBestFit)(ppTree, Key, fAbove);
|
---|
58 | if (pNode != NULL)
|
---|
59 | {
|
---|
60 | #ifdef KAVL_EQUAL_ALLOWED
|
---|
61 | if (pNode->pList != KAVL_NULL)
|
---|
62 | {
|
---|
63 | PKAVLNODECORE pRet = KAVL_GET_POINTER(&pNode->pList);
|
---|
64 | KAVL_SET_POINTER_NULL(&pNode->pList, &pRet->pList);
|
---|
65 | return pRet;
|
---|
66 | }
|
---|
67 | #endif
|
---|
68 | pNode = KAVL_FN(Remove)(ppTree, pNode->Key);
|
---|
69 | }
|
---|
70 | return pNode;
|
---|
71 | }
|
---|
72 |
|
---|
73 |
|
---|
74 | #endif
|
---|