xref: /minix3/lib/libc/stdlib/tdelete.c (revision f14fb602092e015ff630df58e17c2a9cd57d29b3)
1*f14fb602SLionel Sambuc /*	$NetBSD: tdelete.c,v 1.6 2012/06/25 22:32:45 abs Exp $	*/
22fe8fb19SBen Gras 
32fe8fb19SBen Gras /*
42fe8fb19SBen Gras  * Tree search generalized from Knuth (6.2.2) Algorithm T just like
52fe8fb19SBen Gras  * the AT&T man page says.
62fe8fb19SBen Gras  *
72fe8fb19SBen Gras  * The node_t structure is for internal use only, lint doesn't grok it.
82fe8fb19SBen Gras  *
92fe8fb19SBen Gras  * Written by reading the System V Interface Definition, not the code.
102fe8fb19SBen Gras  *
112fe8fb19SBen Gras  * Totally public domain.
122fe8fb19SBen Gras  */
132fe8fb19SBen Gras 
142fe8fb19SBen Gras #include <sys/cdefs.h>
152fe8fb19SBen Gras #if defined(LIBC_SCCS) && !defined(lint)
16*f14fb602SLionel Sambuc __RCSID("$NetBSD: tdelete.c,v 1.6 2012/06/25 22:32:45 abs Exp $");
172fe8fb19SBen Gras #endif /* LIBC_SCCS and not lint */
182fe8fb19SBen Gras 
192fe8fb19SBen Gras #include <assert.h>
202fe8fb19SBen Gras #define _SEARCH_PRIVATE
212fe8fb19SBen Gras #include <search.h>
222fe8fb19SBen Gras #include <stdlib.h>
232fe8fb19SBen Gras 
242fe8fb19SBen Gras 
25*f14fb602SLionel Sambuc /* find a node with key "vkey" in tree "vrootp" */
262fe8fb19SBen Gras void *
tdelete(const void * vkey,void ** vrootp,int (* compar)(const void *,const void *))27*f14fb602SLionel Sambuc tdelete(const void *vkey, void **vrootp,
28*f14fb602SLionel Sambuc     int (*compar)(const void *, const void *))
292fe8fb19SBen Gras {
302fe8fb19SBen Gras 	node_t **rootp = (node_t **)vrootp;
312fe8fb19SBen Gras 	node_t *p, *q, *r;
322fe8fb19SBen Gras 	int  cmp;
332fe8fb19SBen Gras 
342fe8fb19SBen Gras 	_DIAGASSERT(vkey != NULL);
352fe8fb19SBen Gras 	_DIAGASSERT(compar != NULL);
362fe8fb19SBen Gras 
372fe8fb19SBen Gras 	if (rootp == NULL || (p = *rootp) == NULL)
382fe8fb19SBen Gras 		return NULL;
392fe8fb19SBen Gras 
402fe8fb19SBen Gras 	while ((cmp = (*compar)(vkey, (*rootp)->key)) != 0) {
412fe8fb19SBen Gras 		p = *rootp;
422fe8fb19SBen Gras 		rootp = (cmp < 0) ?
432fe8fb19SBen Gras 		    &(*rootp)->llink :		/* follow llink branch */
442fe8fb19SBen Gras 		    &(*rootp)->rlink;		/* follow rlink branch */
452fe8fb19SBen Gras 		if (*rootp == NULL)
462fe8fb19SBen Gras 			return NULL;		/* key not found */
472fe8fb19SBen Gras 	}
482fe8fb19SBen Gras 	r = (*rootp)->rlink;			/* D1: */
492fe8fb19SBen Gras 	if ((q = (*rootp)->llink) == NULL)	/* Left NULL? */
502fe8fb19SBen Gras 		q = r;
512fe8fb19SBen Gras 	else if (r != NULL) {			/* Right link is NULL? */
522fe8fb19SBen Gras 		if (r->llink == NULL) {		/* D2: Find successor */
532fe8fb19SBen Gras 			r->llink = q;
542fe8fb19SBen Gras 			q = r;
552fe8fb19SBen Gras 		} else {			/* D3: Find NULL link */
562fe8fb19SBen Gras 			for (q = r->llink; q->llink != NULL; q = r->llink)
572fe8fb19SBen Gras 				r = q;
582fe8fb19SBen Gras 			r->llink = q->rlink;
592fe8fb19SBen Gras 			q->llink = (*rootp)->llink;
602fe8fb19SBen Gras 			q->rlink = (*rootp)->rlink;
612fe8fb19SBen Gras 		}
622fe8fb19SBen Gras 	}
632fe8fb19SBen Gras 	if (p != *rootp)
642fe8fb19SBen Gras 		free(*rootp);			/* D4: Free node */
652fe8fb19SBen Gras 	*rootp = q;				/* link parent to new node */
662fe8fb19SBen Gras 	return p;
672fe8fb19SBen Gras }
68