1 | /* $Id: kAvlDestroy.h 2 2007-11-16 16:07:14Z bird $ */
|
---|
2 | /** @file
|
---|
3 | * kAvlTmpl - Templated AVL Trees, Destroy the tree.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (c) 1999-2007 knut st. osmundsen <bird-src-spam@anduin.net>
|
---|
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 | * Destroys the specified tree, starting with the root node and working our way down.
|
---|
37 | *
|
---|
38 | * @returns 0 on success.
|
---|
39 | * @returns Return value from callback on failure. On failure, the tree will be in
|
---|
40 | * an unbalanced condition and only further calls to the Destroy should be
|
---|
41 | * made on it. Note that the node we fail on will be considered dead and
|
---|
42 | * no action is taken to link it back into the tree.
|
---|
43 | * @param ppTree Pointer to the AVL-tree root node pointer.
|
---|
44 | * @param pfnCallBack Pointer to callback function.
|
---|
45 | * @param pvUser User parameter passed on to the callback function.
|
---|
46 | */
|
---|
47 | KAVL_DECL(int) KAVL_FN(Destroy)(KAVLTREEPTR *ppTree, KAVL_TYPE(PFN,CALLBACK) pfnCallBack, void *pvUser)
|
---|
48 | {
|
---|
49 | unsigned cEntries;
|
---|
50 | KAVLNODE *apEntries[KAVL_MAX_STACK];
|
---|
51 | int rc;
|
---|
52 |
|
---|
53 | if (*ppTree == KAVL_NULL)
|
---|
54 | return 0;
|
---|
55 |
|
---|
56 | cEntries = 1;
|
---|
57 | apEntries[0] = KAVL_GET_POINTER(ppTree);
|
---|
58 | while (cEntries > 0)
|
---|
59 | {
|
---|
60 | /*
|
---|
61 | * Process the subtrees first.
|
---|
62 | */
|
---|
63 | KAVLNODE *pNode = apEntries[cEntries - 1];
|
---|
64 | if (pNode->mpLeft != KAVL_NULL)
|
---|
65 | apEntries[cEntries++] = KAVL_GET_POINTER(&pNode->mpLeft);
|
---|
66 | else if (pNode->mpRight != KAVL_NULL)
|
---|
67 | apEntries[cEntries++] = KAVL_GET_POINTER(&pNode->mpRight);
|
---|
68 | else
|
---|
69 | {
|
---|
70 | #ifdef KAVL_EQUAL_ALLOWED
|
---|
71 | /*
|
---|
72 | * Process nodes with the same key.
|
---|
73 | */
|
---|
74 | while (pNode->pList != KAVL_NULL)
|
---|
75 | {
|
---|
76 | KAVLNODE *pEqual = KAVL_GET_POINTER(&pNode->pList);
|
---|
77 | KAVL_SET_POINTER(&pNode->pList, KAVL_GET_POINTER_NULL(&pEqual->pList));
|
---|
78 | pEqual->pList = KAVL_NULL;
|
---|
79 |
|
---|
80 | rc = pfnCallBack(pEqual, pvUser);
|
---|
81 | if (rc)
|
---|
82 | return rc;
|
---|
83 | }
|
---|
84 | #endif
|
---|
85 |
|
---|
86 | /*
|
---|
87 | * Unlink the node.
|
---|
88 | */
|
---|
89 | if (--cEntries > 0)
|
---|
90 | {
|
---|
91 | KAVLNODE *pParent = apEntries[cEntries - 1];
|
---|
92 | if (KAVL_GET_POINTER(&pParent->mpLeft) == pNode)
|
---|
93 | pParent->mpLeft = KAVL_NULL;
|
---|
94 | else
|
---|
95 | pParent->mpRight = KAVL_NULL;
|
---|
96 | }
|
---|
97 | else
|
---|
98 | *ppTree = KAVL_NULL;
|
---|
99 |
|
---|
100 | kHlpAssert(pNode->mpLeft == KAVL_NULL);
|
---|
101 | kHlpAssert(pNode->mpRight == KAVL_NULL);
|
---|
102 | rc = pfnCallBack(pNode, pvUser);
|
---|
103 | if (rc)
|
---|
104 | return rc;
|
---|
105 | }
|
---|
106 | } /* while */
|
---|
107 | kHlpAssert(*ppTree == KAVL_NULL);
|
---|
108 |
|
---|
109 | return 0;
|
---|
110 | }
|
---|
111 |
|
---|