xref: /freebsd/lib/libc/stdlib/tdelete.c (revision 559a218c9b257775fb249b67945fe4a05b7a6b9f)
1459d04a5SEd Schouten /*-
2459d04a5SEd Schouten  * Copyright (c) 2015 Nuxi, https://nuxi.nl/
364566a3eSAlfred Perlstein  *
4459d04a5SEd Schouten  * Redistribution and use in source and binary forms, with or without
5459d04a5SEd Schouten  * modification, are permitted provided that the following conditions
6459d04a5SEd Schouten  * are met:
7459d04a5SEd Schouten  * 1. Redistributions of source code must retain the above copyright
8459d04a5SEd Schouten  *    notice, this list of conditions and the following disclaimer.
9459d04a5SEd Schouten  * 2. Redistributions in binary form must reproduce the above copyright
10459d04a5SEd Schouten  *    notice, this list of conditions and the following disclaimer in the
11459d04a5SEd Schouten  *    documentation and/or other materials provided with the distribution.
1264566a3eSAlfred Perlstein  *
13459d04a5SEd Schouten  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14459d04a5SEd Schouten  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15459d04a5SEd Schouten  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16459d04a5SEd Schouten  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17459d04a5SEd Schouten  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18459d04a5SEd Schouten  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19459d04a5SEd Schouten  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20459d04a5SEd Schouten  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21459d04a5SEd Schouten  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22459d04a5SEd Schouten  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23459d04a5SEd Schouten  * SUCH DAMAGE.
2464566a3eSAlfred Perlstein  */
2564566a3eSAlfred Perlstein 
2664566a3eSAlfred Perlstein #define	_SEARCH_PRIVATE
2764566a3eSAlfred Perlstein #include <search.h>
28459d04a5SEd Schouten #include <stdbool.h>
2964566a3eSAlfred Perlstein #include <stdlib.h>
3064566a3eSAlfred Perlstein 
31459d04a5SEd Schouten #include "tsearch_path.h"
3264566a3eSAlfred Perlstein 
33840b798cSRobert Drehmel /*
34459d04a5SEd Schouten  * Makes a step to the left along the binary search tree. This step is
35459d04a5SEd Schouten  * also saved, so it can be replayed while rebalancing.
36840b798cSRobert Drehmel */
37459d04a5SEd Schouten #define	GO_LEFT() do {							\
38459d04a5SEd Schouten 	if ((*leaf)->balance == 0 ||					\
39459d04a5SEd Schouten 	    ((*leaf)->balance < 0 && (*leaf)->rlink->balance == 0)) {	\
40459d04a5SEd Schouten 		/*							\
41459d04a5SEd Schouten 		 * If we reach a node that is balanced, or has a child	\
42459d04a5SEd Schouten 		 * in the opposite direction that is balanced, we know	\
43459d04a5SEd Schouten 		 * that we won't need to perform any rotations above	\
44459d04a5SEd Schouten 		 * this point. In this case rotations are always	\
45459d04a5SEd Schouten 		 * capable of keeping the subtree in balance. Make	\
46*4ef9bd22SEd Schouten 		 * this the root node and reset the path.		\
47459d04a5SEd Schouten 		 */							\
48*4ef9bd22SEd Schouten 		rootp = leaf;						\
49459d04a5SEd Schouten 		path_init(&path);					\
50459d04a5SEd Schouten 	}								\
51459d04a5SEd Schouten 	path_taking_left(&path);					\
52459d04a5SEd Schouten 	leaf = &(*leaf)->llink;						\
53459d04a5SEd Schouten } while (0)
54459d04a5SEd Schouten 
55459d04a5SEd Schouten /* Makes a step to the right along the binary search tree. */
56459d04a5SEd Schouten #define	GO_RIGHT() do {							\
57459d04a5SEd Schouten 	if ((*leaf)->balance == 0 ||					\
58459d04a5SEd Schouten 	    ((*leaf)->balance > 0 && (*leaf)->llink->balance == 0)) {	\
59*4ef9bd22SEd Schouten 		rootp = leaf;						\
60459d04a5SEd Schouten 		path_init(&path);					\
61459d04a5SEd Schouten 	}								\
62459d04a5SEd Schouten 	path_taking_right(&path);					\
63459d04a5SEd Schouten 	leaf = &(*leaf)->rlink;						\
64459d04a5SEd Schouten } while (0)
65459d04a5SEd Schouten 
6664566a3eSAlfred Perlstein void *
tdelete(const void * restrict key,posix_tnode ** restrict rootp,int (* compar)(const void *,const void *))67*4ef9bd22SEd Schouten tdelete(const void *restrict key, posix_tnode **restrict rootp,
68840b798cSRobert Drehmel     int (*compar)(const void *, const void *))
6964566a3eSAlfred Perlstein {
70459d04a5SEd Schouten 	struct path path;
71*4ef9bd22SEd Schouten 	posix_tnode **leaf, *old, **n, *x, *y, *z, *result;
7264566a3eSAlfred Perlstein 	int cmp;
7364566a3eSAlfred Perlstein 
74459d04a5SEd Schouten 	/* POSIX requires that tdelete() returns NULL if rootp is NULL. */
75459d04a5SEd Schouten 	if (rootp == NULL)
76459d04a5SEd Schouten 		return (NULL);
7764566a3eSAlfred Perlstein 
78459d04a5SEd Schouten 	/*
79459d04a5SEd Schouten 	 * Find the leaf that needs to be removed. Return if we cannot
80459d04a5SEd Schouten 	 * find an existing entry. Keep track of the path that is taken
81459d04a5SEd Schouten 	 * to get to the node, as we will need it to adjust the
82459d04a5SEd Schouten 	 * balances.
83459d04a5SEd Schouten 	 */
84*4ef9bd22SEd Schouten 	result = (posix_tnode *)1;
85459d04a5SEd Schouten 	path_init(&path);
86*4ef9bd22SEd Schouten 	leaf = rootp;
87459d04a5SEd Schouten 	for (;;) {
88459d04a5SEd Schouten 		if (*leaf == NULL)
89459d04a5SEd Schouten 			return (NULL);
90459d04a5SEd Schouten 		cmp = compar(key, (*leaf)->key);
91459d04a5SEd Schouten 		if (cmp < 0) {
92*4ef9bd22SEd Schouten 			result = *leaf;
93459d04a5SEd Schouten 			GO_LEFT();
94459d04a5SEd Schouten 		} else if (cmp > 0) {
95*4ef9bd22SEd Schouten 			result = *leaf;
96459d04a5SEd Schouten 			GO_RIGHT();
97459d04a5SEd Schouten 		} else {
98459d04a5SEd Schouten 			break;
9964566a3eSAlfred Perlstein 		}
10064566a3eSAlfred Perlstein 	}
101459d04a5SEd Schouten 
102459d04a5SEd Schouten 	/* Found a matching key in the tree. Remove the node. */
103459d04a5SEd Schouten 	if ((*leaf)->llink == NULL) {
104459d04a5SEd Schouten 		/* Node has no left children. Replace by its right subtree. */
105459d04a5SEd Schouten 		old = *leaf;
106459d04a5SEd Schouten 		*leaf = old->rlink;
107459d04a5SEd Schouten 		free(old);
108459d04a5SEd Schouten 	} else {
109459d04a5SEd Schouten 		/*
110459d04a5SEd Schouten 		 * Node has left children. Replace this node's key by
111459d04a5SEd Schouten 		 * its predecessor's and remove that node instead.
112459d04a5SEd Schouten 		 */
113459d04a5SEd Schouten 		void **keyp = &(*leaf)->key;
114459d04a5SEd Schouten 		GO_LEFT();
115459d04a5SEd Schouten 		while ((*leaf)->rlink != NULL)
116459d04a5SEd Schouten 			GO_RIGHT();
117459d04a5SEd Schouten 		old = *leaf;
118459d04a5SEd Schouten 		*keyp = old->key;
119459d04a5SEd Schouten 		*leaf = old->llink;
120459d04a5SEd Schouten 		free(old);
121459d04a5SEd Schouten 	}
122459d04a5SEd Schouten 
123459d04a5SEd Schouten 	/*
124459d04a5SEd Schouten 	 * Walk along the same path a second time and adjust the
125459d04a5SEd Schouten 	 * balances. Though this code looks similar to the rebalancing
126459d04a5SEd Schouten 	 * performed in tsearch(), it is not identical. We now also need
127459d04a5SEd Schouten 	 * to consider the case of outward imbalance in the right-right
128459d04a5SEd Schouten 	 * and left-left case that only exists when deleting. Hence the
129459d04a5SEd Schouten 	 * duplication of code.
130459d04a5SEd Schouten 	 */
131*4ef9bd22SEd Schouten 	for (n = rootp; n != leaf;) {
132459d04a5SEd Schouten 		if (path_took_left(&path)) {
133459d04a5SEd Schouten 			x = *n;
134459d04a5SEd Schouten 			if (x->balance < 0) {
135459d04a5SEd Schouten 				y = x->rlink;
136459d04a5SEd Schouten 				if (y->balance > 0) {
137459d04a5SEd Schouten 					/* Right-left case. */
138459d04a5SEd Schouten 					z = y->llink;
139459d04a5SEd Schouten 					x->rlink = z->llink;
140459d04a5SEd Schouten 					z->llink = x;
141459d04a5SEd Schouten 					y->llink = z->rlink;
142459d04a5SEd Schouten 					z->rlink = y;
143459d04a5SEd Schouten 					*n = z;
144459d04a5SEd Schouten 
145459d04a5SEd Schouten 					x->balance = z->balance < 0 ? 1 : 0;
146459d04a5SEd Schouten 					y->balance = z->balance > 0 ? -1 : 0;
147459d04a5SEd Schouten 					z->balance = 0;
148459d04a5SEd Schouten 				} else {
149459d04a5SEd Schouten 					/* Right-right case. */
150459d04a5SEd Schouten 					x->rlink = y->llink;
151459d04a5SEd Schouten 					y->llink = x;
152459d04a5SEd Schouten 					*n = y;
153459d04a5SEd Schouten 
154459d04a5SEd Schouten 					if (y->balance < 0) {
155459d04a5SEd Schouten 						x->balance = 0;
156459d04a5SEd Schouten 						y->balance = 0;
157459d04a5SEd Schouten 					} else {
158459d04a5SEd Schouten 						x->balance = -1;
159459d04a5SEd Schouten 						y->balance = 1;
160459d04a5SEd Schouten 					}
161459d04a5SEd Schouten 				}
162459d04a5SEd Schouten 			} else {
163459d04a5SEd Schouten 				--x->balance;
164459d04a5SEd Schouten 			}
165459d04a5SEd Schouten 			n = &x->llink;
166459d04a5SEd Schouten 		} else {
167459d04a5SEd Schouten 			x = *n;
168459d04a5SEd Schouten 			if (x->balance > 0) {
169459d04a5SEd Schouten 				y = x->llink;
170459d04a5SEd Schouten 				if (y->balance < 0) {
171459d04a5SEd Schouten 					/* Left-right case. */
172459d04a5SEd Schouten 					z = y->rlink;
173459d04a5SEd Schouten 					y->rlink = z->llink;
174459d04a5SEd Schouten 					z->llink = y;
175459d04a5SEd Schouten 					x->llink = z->rlink;
176459d04a5SEd Schouten 					z->rlink = x;
177459d04a5SEd Schouten 					*n = z;
178459d04a5SEd Schouten 
179459d04a5SEd Schouten 					x->balance = z->balance > 0 ? -1 : 0;
180459d04a5SEd Schouten 					y->balance = z->balance < 0 ? 1 : 0;
181459d04a5SEd Schouten 					z->balance = 0;
182459d04a5SEd Schouten 				} else {
183459d04a5SEd Schouten 					/* Left-left case. */
184459d04a5SEd Schouten 					x->llink = y->rlink;
185459d04a5SEd Schouten 					y->rlink = x;
186459d04a5SEd Schouten 					*n = y;
187459d04a5SEd Schouten 
188459d04a5SEd Schouten 					if (y->balance > 0) {
189459d04a5SEd Schouten 						x->balance = 0;
190459d04a5SEd Schouten 						y->balance = 0;
191459d04a5SEd Schouten 					} else {
192459d04a5SEd Schouten 						x->balance = 1;
193459d04a5SEd Schouten 						y->balance = -1;
194459d04a5SEd Schouten 					}
195459d04a5SEd Schouten 				}
196459d04a5SEd Schouten 			} else {
197459d04a5SEd Schouten 				++x->balance;
198459d04a5SEd Schouten 			}
199459d04a5SEd Schouten 			n = &x->rlink;
200459d04a5SEd Schouten 		}
201459d04a5SEd Schouten 	}
202459d04a5SEd Schouten 
203459d04a5SEd Schouten 	/* Return the parent of the old entry. */
204459d04a5SEd Schouten 	return (result);
20564566a3eSAlfred Perlstein }
206