1 | /* $Id: avl_GetBestFit.cpp.h 65208 2017-01-09 14:39:54Z 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-2012 knut st. osmundsen (bird-src-spam@anduin.net)
|
---|
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 _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 | KAVL_DECL(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
|
---|