Home | History | Annotate | Line # | Download | only in stdlib
tdelete.c revision 1.2
      1  1.2     lukem /*	$NetBSD: tdelete.c,v 1.2 1999/09/16 11:45:37 lukem Exp $	*/
      2  1.1  christos 
      3  1.1  christos /*
      4  1.1  christos  * Tree search generalized from Knuth (6.2.2) Algorithm T just like
      5  1.1  christos  * the AT&T man page says.
      6  1.1  christos  *
      7  1.1  christos  * The node_t structure is for internal use only, lint doesn't grok it.
      8  1.1  christos  *
      9  1.1  christos  * Written by reading the System V Interface Definition, not the code.
     10  1.1  christos  *
     11  1.1  christos  * Totally public domain.
     12  1.1  christos  */
     13  1.1  christos 
     14  1.1  christos #include <sys/cdefs.h>
     15  1.1  christos #if defined(LIBC_SCCS) && !defined(lint)
     16  1.2     lukem __RCSID("$NetBSD: tdelete.c,v 1.2 1999/09/16 11:45:37 lukem Exp $");
     17  1.1  christos #endif /* LIBC_SCCS and not lint */
     18  1.1  christos 
     19  1.2     lukem #include <assert.h>
     20  1.1  christos #define _SEARCH_PRIVATE
     21  1.1  christos #include <search.h>
     22  1.1  christos #include <stdlib.h>
     23  1.1  christos 
     24  1.1  christos 
     25  1.1  christos /* delete node with given key */
     26  1.1  christos void *
     27  1.1  christos tdelete(vkey, vrootp, compar)
     28  1.1  christos 	const void *vkey;	/* key to be deleted */
     29  1.1  christos 	void      **vrootp;	/* address of the root of tree */
     30  1.1  christos 	int       (*compar) __P((const void *, const void *));
     31  1.1  christos {
     32  1.1  christos 	node_t **rootp = (node_t **)vrootp;
     33  1.1  christos 	node_t *p, *q, *r;
     34  1.1  christos 	int  cmp;
     35  1.2     lukem 
     36  1.2     lukem 	_DIAGASSERT(vkey != NULL);
     37  1.2     lukem 	_DIAGASSERT(compar != NULL);
     38  1.2     lukem #ifdef _DIAGNOSTIC
     39  1.2     lukem 	if (vkey == NULL || compar == NULL)
     40  1.2     lukem 		return (NULL);
     41  1.2     lukem #endif
     42  1.1  christos 
     43  1.1  christos 	if (rootp == NULL || (p = *rootp) == NULL)
     44  1.1  christos 		return NULL;
     45  1.1  christos 
     46  1.1  christos 	while ((cmp = (*compar)(vkey, (*rootp)->key)) != 0) {
     47  1.1  christos 		p = *rootp;
     48  1.1  christos 		rootp = (cmp < 0) ?
     49  1.1  christos 		    &(*rootp)->llink :		/* follow llink branch */
     50  1.1  christos 		    &(*rootp)->rlink;		/* follow rlink branch */
     51  1.1  christos 		if (*rootp == NULL)
     52  1.1  christos 			return NULL;		/* key not found */
     53  1.1  christos 	}
     54  1.1  christos 	r = (*rootp)->rlink;			/* D1: */
     55  1.1  christos 	if ((q = (*rootp)->llink) == NULL)	/* Left NULL? */
     56  1.1  christos 		q = r;
     57  1.1  christos 	else if (r != NULL) {			/* Right link is NULL? */
     58  1.1  christos 		if (r->llink == NULL) {		/* D2: Find successor */
     59  1.1  christos 			r->llink = q;
     60  1.1  christos 			q = r;
     61  1.1  christos 		} else {			/* D3: Find NULL link */
     62  1.1  christos 			for (q = r->llink; q->llink != NULL; q = r->llink)
     63  1.1  christos 				r = q;
     64  1.1  christos 			r->llink = q->rlink;
     65  1.1  christos 			q->llink = (*rootp)->llink;
     66  1.1  christos 			q->rlink = (*rootp)->rlink;
     67  1.1  christos 		}
     68  1.1  christos 	}
     69  1.1  christos 	free(*rootp);				/* D4: Free node */
     70  1.1  christos 	*rootp = q;				/* link parent to new node */
     71  1.1  christos 	return p;
     72  1.1  christos }
     73