1 | /* $Id: kAvlGetBestFit.h 7 2008-02-04 02:08:02Z bird $ */
|
---|
2 | /** @file
|
---|
3 | * kAvlTmpl - Templated AVL Trees, Get Best Fitting Node.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (c) 1999-2007 knut st. osmundsen <[email protected]>
|
---|
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 | *
|
---|
26 | * As a special exception, since this is a source file and not a header
|
---|
27 | * file, you are granted permission to #include this file as you wish
|
---|
28 | * without this in itself causing the resulting program or whatever to be
|
---|
29 | * covered by the LGPL license. This exception does not however invalidate
|
---|
30 | * any other reasons why the resulting program/whatever should not be
|
---|
31 | * covered the LGPL or GPL.
|
---|
32 | */
|
---|
33 |
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * Finds the best fitting node in the tree for the given Key value.
|
---|
37 | *
|
---|
38 | * @returns Pointer to the best fitting node found.
|
---|
39 | * @param pRoot Pointer to the AVL-tree root structure.
|
---|
40 | * @param Key The Key of which is to be found a best fitting match for..
|
---|
41 | * @param fAbove K_TRUE: Returned node is have the closest key to Key from above.
|
---|
42 | * K_FALSE: Returned node is have the closest key to Key from below.
|
---|
43 | * @sketch The best fitting node is always located in the searchpath above you.
|
---|
44 | * >= (above): The node where you last turned left.
|
---|
45 | * <= (below): the node where you last turned right.
|
---|
46 | */
|
---|
47 | KAVL_DECL(KAVLNODE *) KAVL_FN(GetBestFit)(KAVLROOT *pRoot, KAVLKEY Key, KBOOL fAbove)
|
---|
48 | {
|
---|
49 | register KAVLNODE *pNode;
|
---|
50 | KAVLNODE *pNodeLast;
|
---|
51 |
|
---|
52 | KAVL_READ_LOCK(pLook);
|
---|
53 | if (pRoot->mpRoot == KAVL_NULL)
|
---|
54 | {
|
---|
55 | KAVL_READ_UNLOCK(pLook);
|
---|
56 | return NULL;
|
---|
57 | }
|
---|
58 |
|
---|
59 | pNode = KAVL_GET_POINTER(&pRoot->mpRoot);
|
---|
60 | pNodeLast = NULL;
|
---|
61 | if (fAbove)
|
---|
62 | { /* pNode->mKey >= Key */
|
---|
63 | while (KAVL_NE(pNode->mKey, Key))
|
---|
64 | {
|
---|
65 | if (KAVL_G(pNode->mKey, Key))
|
---|
66 | {
|
---|
67 | if (pNode->mpLeft == KAVL_NULL)
|
---|
68 | {
|
---|
69 | KAVL_READ_UNLOCK(pLook);
|
---|
70 | return pNode;
|
---|
71 | }
|
---|
72 | pNodeLast = pNode;
|
---|
73 | pNode = KAVL_GET_POINTER(&pNode->mpLeft);
|
---|
74 | }
|
---|
75 | else
|
---|
76 | {
|
---|
77 | if (pNode->mpRight == KAVL_NULL)
|
---|
78 | {
|
---|
79 | KAVL_READ_UNLOCK(pLook);
|
---|
80 | return pNodeLast;
|
---|
81 | }
|
---|
82 | pNode = KAVL_GET_POINTER(&pNode->mpRight);
|
---|
83 | }
|
---|
84 | }
|
---|
85 | }
|
---|
86 | else
|
---|
87 | { /* pNode->mKey <= Key */
|
---|
88 | while (KAVL_NE(pNode->mKey, Key))
|
---|
89 | {
|
---|
90 | if (KAVL_G(pNode->mKey, Key))
|
---|
91 | {
|
---|
92 | if (pNode->mpLeft == KAVL_NULL)
|
---|
93 | {
|
---|
94 | KAVL_READ_UNLOCK(pLook);
|
---|
95 | return pNodeLast;
|
---|
96 | }
|
---|
97 | pNode = KAVL_GET_POINTER(&pNode->mpLeft);
|
---|
98 | }
|
---|
99 | else
|
---|
100 | {
|
---|
101 | if (pNode->mpRight == KAVL_NULL)
|
---|
102 | {
|
---|
103 | KAVL_READ_UNLOCK(pLook);
|
---|
104 | return pNode;
|
---|
105 | }
|
---|
106 | pNodeLast = pNode;
|
---|
107 | pNode = KAVL_GET_POINTER(&pNode->mpRight);
|
---|
108 | }
|
---|
109 | }
|
---|
110 | }
|
---|
111 |
|
---|
112 | /* perfect match or nothing. */
|
---|
113 | KAVL_READ_UNLOCK(pLook);
|
---|
114 | return pNode;
|
---|
115 | }
|
---|
116 |
|
---|