3 * @copy 2012 MinGW.org project
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
25 #define _SEARCH_PRIVATE
29 #define _DIAGASSERT assert
33 /* delete node with given key */
35 tdelete(const void *vkey, /* key to be deleted */
36 void **vrootp, /* address of the root of tree */
37 int (*compar)(const void *, const void *))
39 node_t **rootp = (node_t **)vrootp;
43 _DIAGASSERT(compar != NULL);
45 if (rootp == NULL || (p = *rootp) == NULL)
48 while ((cmp = (*compar)(vkey, (*rootp)->key)) != 0) {
51 &(*rootp)->llink : /* follow llink branch */
52 &(*rootp)->rlink; /* follow rlink branch */
54 return NULL; /* key not found */
56 r = (*rootp)->rlink; /* D1: */
57 if ((q = (*rootp)->llink) == NULL) /* Left NULL? */
59 else if (r != NULL) { /* Right link is NULL? */
60 if (r->llink == NULL) { /* D2: Find successor */
63 } else { /* D3: Find NULL link */
64 for (q = r->llink; q->llink != NULL; q = r->llink)
67 q->llink = (*rootp)->llink;
68 q->rlink = (*rootp)->rlink;
71 free(*rootp); /* D4: Free node */
72 *rootp = q; /* link parent to new node */