1 | /* $Id: avl_GetBestFit.cpp.h 8155 2008-04-18 15:16:47Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * kAVLGetBestFit - Get 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-2002 knut st. osmundsen ([email protected])
|
---|
10 | *
|
---|
11 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
12 | * available from http://www.virtualbox.org. This file is free software;
|
---|
13 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
14 | * General Public License (GPL) as published by the Free Software
|
---|
15 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
16 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
17 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
18 | *
|
---|
19 | * The contents of this file may alternatively be used under the terms
|
---|
20 | * of the Common Development and Distribution License Version 1.0
|
---|
21 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
22 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
23 | * CDDL are applicable instead of those of the GPL.
|
---|
24 | *
|
---|
25 | * You may elect to license modified versions of this file under the
|
---|
26 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
27 | *
|
---|
28 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
29 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
30 | * additional information or have any questions.
|
---|
31 | */
|
---|
32 |
|
---|
33 | #ifndef _kAVLGetBestFit_h_
|
---|
34 | #define _kAVLGetBestFit_h_
|
---|
35 |
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * Finds the best fitting node in the tree for the given Key value.
|
---|
39 | * @returns Pointer to the best fitting node found.
|
---|
40 | * @param ppTree Pointer to Pointer to the tree root node.
|
---|
41 | * @param Key The Key of which is to be found a best fitting match for..
|
---|
42 | * @param fAbove TRUE: Returned node is have the closest key to Key from above.
|
---|
43 | * FALSE: Returned node is have the closest key to Key from below.
|
---|
44 | * @sketch The best fitting node is always located in the searchpath above you.
|
---|
45 | * >= (above): The node where you last turned left.
|
---|
46 | * <= (below): the node where you last turned right.
|
---|
47 | */
|
---|
48 | RTDECL(PKAVLNODECORE) KAVL_FN(GetBestFit)(PPKAVLNODECORE ppTree, KAVLKEY Key, bool fAbove)
|
---|
49 | {
|
---|
50 | register PKAVLNODECORE pNode = KAVL_GET_POINTER_NULL(ppTree);
|
---|
51 | if (pNode)
|
---|
52 | {
|
---|
53 | PKAVLNODECORE pNodeLast = NULL;
|
---|
54 | if (fAbove)
|
---|
55 | { /* pNode->Key >= Key */
|
---|
56 | while (KAVL_NE(pNode->Key, Key))
|
---|
57 | {
|
---|
58 | if (KAVL_G(pNode->Key, Key))
|
---|
59 | {
|
---|
60 | if (pNode->pLeft != KAVL_NULL)
|
---|
61 | {
|
---|
62 | pNodeLast = pNode;
|
---|
63 | pNode = KAVL_GET_POINTER(&pNode->pLeft);
|
---|
64 | }
|
---|
65 | else
|
---|
66 | return pNode;
|
---|
67 | }
|
---|
68 | else
|
---|
69 | {
|
---|
70 | if (pNode->pRight != KAVL_NULL)
|
---|
71 | pNode = KAVL_GET_POINTER(&pNode->pRight);
|
---|
72 | else
|
---|
73 | return pNodeLast;
|
---|
74 | }
|
---|
75 | }
|
---|
76 | }
|
---|
77 | else
|
---|
78 | { /* pNode->Key <= Key */
|
---|
79 | while (KAVL_NE(pNode->Key, Key))
|
---|
80 | {
|
---|
81 | if (KAVL_G(pNode->Key, Key))
|
---|
82 | {
|
---|
83 | if (pNode->pLeft != KAVL_NULL)
|
---|
84 | pNode = KAVL_GET_POINTER(&pNode->pLeft);
|
---|
85 | else
|
---|
86 | return pNodeLast;
|
---|
87 | }
|
---|
88 | else
|
---|
89 | {
|
---|
90 | if (pNode->pRight != KAVL_NULL)
|
---|
91 | {
|
---|
92 | pNodeLast = pNode;
|
---|
93 | pNode = KAVL_GET_POINTER(&pNode->pRight);
|
---|
94 | }
|
---|
95 | else
|
---|
96 | return pNode;
|
---|
97 | }
|
---|
98 | }
|
---|
99 | }
|
---|
100 | }
|
---|
101 |
|
---|
102 | /* perfect match or nothing. */
|
---|
103 | return pNode;
|
---|
104 | }
|
---|
105 |
|
---|
106 |
|
---|
107 | #endif
|
---|