1 | /* $Id: avl_GetBestFit.cpp.h 402 2007-01-28 08:44:20Z 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 as published by the Free Software Foundation,
|
---|
15 | * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
|
---|
16 | * distribution. VirtualBox OSE is distributed in the hope that it will
|
---|
17 | * be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
18 | *
|
---|
19 | * If you received this file as part of a commercial VirtualBox
|
---|
20 | * distribution, then only the terms of your commercial VirtualBox
|
---|
21 | * license agreement apply instead of the previous paragraph.
|
---|
22 | */
|
---|
23 |
|
---|
24 | #ifndef _kAVLGetBestFit_h_
|
---|
25 | #define _kAVLGetBestFit_h_
|
---|
26 |
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * Finds the best fitting node in the tree for the given Key value.
|
---|
30 | * @returns Pointer to the best fitting node found.
|
---|
31 | * @param ppTree Pointer to Pointer to the tree root node.
|
---|
32 | * @param Key The Key of which is to be found a best fitting match for..
|
---|
33 | * @param fAbove TRUE: Returned node is have the closest key to Key from above.
|
---|
34 | * FALSE: Returned node is have the closest key to Key from below.
|
---|
35 | * @sketch The best fitting node is always located in the searchpath above you.
|
---|
36 | * >= (above): The node where you last turned left.
|
---|
37 | * <= (below): the node where you last turned right.
|
---|
38 | */
|
---|
39 | RTDECL(PKAVLNODECORE) KAVL_FN(GetBestFit)(PPKAVLNODECORE ppTree, KAVLKEY Key, bool fAbove)
|
---|
40 | {
|
---|
41 | register PKAVLNODECORE pNode = KAVL_GET_POINTER_NULL(ppTree);
|
---|
42 | if (pNode)
|
---|
43 | {
|
---|
44 | PKAVLNODECORE pNodeLast = NULL;
|
---|
45 | if (fAbove)
|
---|
46 | { /* pNode->Key >= Key */
|
---|
47 | while (KAVL_NE(pNode->Key, Key))
|
---|
48 | {
|
---|
49 | if (KAVL_G(pNode->Key, Key))
|
---|
50 | {
|
---|
51 | if (pNode->pLeft != KAVL_NULL)
|
---|
52 | {
|
---|
53 | pNodeLast = pNode;
|
---|
54 | pNode = KAVL_GET_POINTER(&pNode->pLeft);
|
---|
55 | }
|
---|
56 | else
|
---|
57 | return pNode;
|
---|
58 | }
|
---|
59 | else
|
---|
60 | {
|
---|
61 | if (pNode->pRight != KAVL_NULL)
|
---|
62 | pNode = KAVL_GET_POINTER(&pNode->pRight);
|
---|
63 | else
|
---|
64 | return pNodeLast;
|
---|
65 | }
|
---|
66 | }
|
---|
67 | }
|
---|
68 | else
|
---|
69 | { /* pNode->Key <= Key */
|
---|
70 | while (KAVL_NE(pNode->Key, Key))
|
---|
71 | {
|
---|
72 | if (KAVL_G(pNode->Key, Key))
|
---|
73 | {
|
---|
74 | if (pNode->pLeft != KAVL_NULL)
|
---|
75 | pNode = KAVL_GET_POINTER(&pNode->pLeft);
|
---|
76 | else
|
---|
77 | return pNodeLast;
|
---|
78 | }
|
---|
79 | else
|
---|
80 | {
|
---|
81 | if (pNode->pRight != KAVL_NULL)
|
---|
82 | {
|
---|
83 | pNodeLast = pNode;
|
---|
84 | pNode = KAVL_GET_POINTER(&pNode->pRight);
|
---|
85 | }
|
---|
86 | else
|
---|
87 | return pNode;
|
---|
88 | }
|
---|
89 | }
|
---|
90 | }
|
---|
91 | }
|
---|
92 |
|
---|
93 | /* perfect match or nothing. */
|
---|
94 | return pNode;
|
---|
95 | }
|
---|
96 |
|
---|
97 |
|
---|
98 | #endif
|
---|