Home | History | Annotate | Line # | Download | only in stdlib
tdelete.c revision 1.1
      1  1.1  christos /*	$NetBSD: tdelete.c,v 1.1 1999/02/22 10:33:15 christos 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.1  christos __RCSID("$NetBSD: tdelete.c,v 1.1 1999/02/22 10:33:15 christos Exp $");
     17  1.1  christos #endif /* LIBC_SCCS and not lint */
     18  1.1  christos 
     19  1.1  christos #define _SEARCH_PRIVATE
     20  1.1  christos #include <search.h>
     21  1.1  christos #include <stdlib.h>
     22  1.1  christos 
     23  1.1  christos 
     24  1.1  christos /* delete node with given key */
     25  1.1  christos void *
     26  1.1  christos tdelete(vkey, vrootp, compar)
     27  1.1  christos 	const void *vkey;	/* key to be deleted */
     28  1.1  christos 	void      **vrootp;	/* address of the root of tree */
     29  1.1  christos 	int       (*compar) __P((const void *, const void *));
     30  1.1  christos {
     31  1.1  christos 	node_t **rootp = (node_t **)vrootp;
     32  1.1  christos 	node_t *p, *q, *r;
     33  1.1  christos 	int  cmp;
     34  1.1  christos 
     35  1.1  christos 	if (rootp == NULL || (p = *rootp) == NULL)
     36  1.1  christos 		return NULL;
     37  1.1  christos 
     38  1.1  christos 	while ((cmp = (*compar)(vkey, (*rootp)->key)) != 0) {
     39  1.1  christos 		p = *rootp;
     40  1.1  christos 		rootp = (cmp < 0) ?
     41  1.1  christos 		    &(*rootp)->llink :		/* follow llink branch */
     42  1.1  christos 		    &(*rootp)->rlink;		/* follow rlink branch */
     43  1.1  christos 		if (*rootp == NULL)
     44  1.1  christos 			return NULL;		/* key not found */
     45  1.1  christos 	}
     46  1.1  christos 	r = (*rootp)->rlink;			/* D1: */
     47  1.1  christos 	if ((q = (*rootp)->llink) == NULL)	/* Left NULL? */
     48  1.1  christos 		q = r;
     49  1.1  christos 	else if (r != NULL) {			/* Right link is NULL? */
     50  1.1  christos 		if (r->llink == NULL) {		/* D2: Find successor */
     51  1.1  christos 			r->llink = q;
     52  1.1  christos 			q = r;
     53  1.1  christos 		} else {			/* D3: Find NULL link */
     54  1.1  christos 			for (q = r->llink; q->llink != NULL; q = r->llink)
     55  1.1  christos 				r = q;
     56  1.1  christos 			r->llink = q->rlink;
     57  1.1  christos 			q->llink = (*rootp)->llink;
     58  1.1  christos 			q->rlink = (*rootp)->rlink;
     59  1.1  christos 		}
     60  1.1  christos 	}
     61  1.1  christos 	free(*rootp);				/* D4: Free node */
     62  1.1  christos 	*rootp = q;				/* link parent to new node */
     63  1.1  christos 	return p;
     64  1.1  christos }
     65