2 * Tree search generalized from Knuth (6.2.2) Algorithm T just like
3 * the AT&T man page says.
5 * The node_t structure is for internal use only, lint doesn't grok it.
7 * Written by reading the System V Interface Definition, not the code.
9 * Totally public domain.
11 * $NetBSD: tdelete.c,v 1.2 1999/09/16 11:45:37 lukem Exp $
12 * $FreeBSD: src/lib/libc/stdlib/tdelete.c,v 1.1.2.1 2000/08/17 07:38:39 jhb Exp $
13 * $DragonFly: src/lib/libc/stdlib/tdelete.c,v 1.3 2003/09/06 08:19:16 asmodai Exp $
16 #include <sys/cdefs.h>
19 #define _SEARCH_PRIVATE
24 /* delete node with given key */
26 tdelete(vkey, vrootp, compar)
27 const void *vkey; /* key to be deleted */
28 void **vrootp; /* address of the root of tree */
29 int (*compar) (const void *, const void *);
31 node_t **rootp = (node_t **)vrootp;
35 if (rootp == NULL || (p = *rootp) == NULL)
38 while ((cmp = (*compar)(vkey, (*rootp)->key)) != 0) {
41 &(*rootp)->llink : /* follow llink branch */
42 &(*rootp)->rlink; /* follow rlink branch */
44 return NULL; /* key not found */
46 r = (*rootp)->rlink; /* D1: */
47 if ((q = (*rootp)->llink) == NULL) /* Left NULL? */
49 else if (r != NULL) { /* Right link is NULL? */
50 if (r->llink == NULL) { /* D2: Find successor */
53 } else { /* D3: Find NULL link */
54 for (q = r->llink; q->llink != NULL; q = r->llink)
57 q->llink = (*rootp)->llink;
58 q->rlink = (*rootp)->rlink;
61 free(*rootp); /* D4: Free node */
62 *rootp = q; /* link parent to new node */