1 | /* $Id: avl_DoWithAll.cpp.h 402 2007-01-28 08:44:20Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * kAVLDoWithAll - Do with all nodes routine for AVL trees.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 1999-2002 knut st. osmundsen (bird-src-spam@anduin.net)
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License as published by the Free Software Foundation,
|
---|
13 | * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
|
---|
14 | * distribution. VirtualBox OSE is distributed in the hope that it will
|
---|
15 | * be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * If you received this file as part of a commercial VirtualBox
|
---|
18 | * distribution, then only the terms of your commercial VirtualBox
|
---|
19 | * license agreement apply instead of the previous paragraph.
|
---|
20 | */
|
---|
21 |
|
---|
22 | #ifndef _kAVLDoWithAll_h_
|
---|
23 | #define _kAVLDoWithAll_h_
|
---|
24 |
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * Iterates tru all nodes in the given tree.
|
---|
28 | * @returns 0 on success. Return from callback on failure.
|
---|
29 | * @param ppTree Pointer to the AVL-tree root node pointer.
|
---|
30 | * @param fFromLeft TRUE: Left to right.
|
---|
31 | * FALSE: Right to left.
|
---|
32 | * @param pfnCallBack Pointer to callback function.
|
---|
33 | * @param pvParam Userparameter passed on to the callback function.
|
---|
34 | */
|
---|
35 | RTDECL(int) KAVL_FN(DoWithAll)(PPKAVLNODECORE ppTree, int fFromLeft, PKAVLCALLBACK pfnCallBack, void * pvParam)
|
---|
36 | {
|
---|
37 | KAVLSTACK2 AVLStack;
|
---|
38 | PKAVLNODECORE pNode;
|
---|
39 | int rc;
|
---|
40 |
|
---|
41 | if (*ppTree == KAVL_NULL)
|
---|
42 | return 0;
|
---|
43 |
|
---|
44 | AVLStack.cEntries = 1;
|
---|
45 | AVLStack.achFlags[0] = 0;
|
---|
46 | AVLStack.aEntries[0] = KAVL_GET_POINTER(ppTree);
|
---|
47 |
|
---|
48 | if (fFromLeft)
|
---|
49 | { /* from left */
|
---|
50 | while (AVLStack.cEntries > 0)
|
---|
51 | {
|
---|
52 | pNode = AVLStack.aEntries[AVLStack.cEntries - 1];
|
---|
53 |
|
---|
54 | /* left */
|
---|
55 | if (!AVLStack.achFlags[AVLStack.cEntries - 1]++)
|
---|
56 | {
|
---|
57 | if (pNode->pLeft != KAVL_NULL)
|
---|
58 | {
|
---|
59 | AVLStack.achFlags[AVLStack.cEntries] = 0; /* 0 first, 1 last */
|
---|
60 | AVLStack.aEntries[AVLStack.cEntries++] = KAVL_GET_POINTER(&pNode->pLeft);
|
---|
61 | continue;
|
---|
62 | }
|
---|
63 | }
|
---|
64 |
|
---|
65 | /* center */
|
---|
66 | rc = pfnCallBack(pNode, pvParam);
|
---|
67 | if (rc)
|
---|
68 | return rc;
|
---|
69 |
|
---|
70 | /* right */
|
---|
71 | AVLStack.cEntries--;
|
---|
72 | if (pNode->pRight != KAVL_NULL)
|
---|
73 | {
|
---|
74 | AVLStack.achFlags[AVLStack.cEntries] = 0;
|
---|
75 | AVLStack.aEntries[AVLStack.cEntries++] = KAVL_GET_POINTER(&pNode->pRight);
|
---|
76 | }
|
---|
77 | } /* while */
|
---|
78 | }
|
---|
79 | else
|
---|
80 | { /* from right */
|
---|
81 | while (AVLStack.cEntries > 0)
|
---|
82 | {
|
---|
83 | pNode = AVLStack.aEntries[AVLStack.cEntries - 1];
|
---|
84 |
|
---|
85 |
|
---|
86 | /* right */
|
---|
87 | if (!AVLStack.achFlags[AVLStack.cEntries - 1]++)
|
---|
88 | {
|
---|
89 | if (pNode->pRight != KAVL_NULL)
|
---|
90 | {
|
---|
91 | AVLStack.achFlags[AVLStack.cEntries] = 0; /* 0 first, 1 last */
|
---|
92 | AVLStack.aEntries[AVLStack.cEntries++] = KAVL_GET_POINTER(&pNode->pRight);
|
---|
93 | continue;
|
---|
94 | }
|
---|
95 | }
|
---|
96 |
|
---|
97 | /* center */
|
---|
98 | rc = pfnCallBack(pNode, pvParam);
|
---|
99 | if (rc)
|
---|
100 | return rc;
|
---|
101 |
|
---|
102 | /* left */
|
---|
103 | AVLStack.cEntries--;
|
---|
104 | if (pNode->pLeft != KAVL_NULL)
|
---|
105 | {
|
---|
106 | AVLStack.achFlags[AVLStack.cEntries] = 0;
|
---|
107 | AVLStack.aEntries[AVLStack.cEntries++] = KAVL_GET_POINTER(&pNode->pLeft);
|
---|
108 | }
|
---|
109 | } /* while */
|
---|
110 | }
|
---|
111 |
|
---|
112 | return 0;
|
---|
113 | }
|
---|
114 |
|
---|
115 |
|
---|
116 | #endif
|
---|
117 |
|
---|