2 * Copyright (c) 2015 Nuxi, https://nuxi.nl/
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
29 #define _SEARCH_PRIVATE
34 #include "tsearch_path.h"
37 * Makes a step to the left along the binary search tree. This step is
38 * also saved, so it can be replayed while rebalancing.
40 #define GO_LEFT() do { \
41 if ((*leaf)->balance == 0 || \
42 ((*leaf)->balance < 0 && (*leaf)->rlink->balance == 0)) { \
44 * If we reach a node that is balanced, or has a child \
45 * in the opposite direction that is balanced, we know \
46 * that we won't need to perform any rotations above \
47 * this point. In this case rotations are always \
48 * capable of keeping the subtree in balance. Make \
49 * this the root node and reset the path. \
54 path_taking_left(&path); \
55 leaf = &(*leaf)->llink; \
58 /* Makes a step to the right along the binary search tree. */
59 #define GO_RIGHT() do { \
60 if ((*leaf)->balance == 0 || \
61 ((*leaf)->balance > 0 && (*leaf)->llink->balance == 0)) { \
65 path_taking_right(&path); \
66 leaf = &(*leaf)->rlink; \
70 tdelete(const void *restrict key, posix_tnode **restrict rootp,
71 int (*compar)(const void *, const void *))
74 posix_tnode **leaf, *old, **n, *x, *y, *z, *result;
77 /* POSIX requires that tdelete() returns NULL if rootp is NULL. */
82 * Find the leaf that needs to be removed. Return if we cannot
83 * find an existing entry. Keep track of the path that is taken
84 * to get to the node, as we will need it to adjust the
87 result = (posix_tnode *)1;
93 cmp = compar(key, (*leaf)->key);
105 /* Found a matching key in the tree. Remove the node. */
106 if ((*leaf)->llink == NULL) {
107 /* Node has no left children. Replace by its right subtree. */
113 * Node has left children. Replace this node's key by
114 * its predecessor's and remove that node instead.
116 void **keyp = &(*leaf)->key;
118 while ((*leaf)->rlink != NULL)
127 * Walk along the same path a second time and adjust the
128 * balances. Though this code looks similar to the rebalancing
129 * performed in tsearch(), it is not identical. We now also need
130 * to consider the case of outward imbalance in the right-right
131 * and left-left case that only exists when deleting. Hence the
132 * duplication of code.
134 for (n = rootp; n != leaf;) {
135 if (path_took_left(&path)) {
137 if (x->balance < 0) {
139 if (y->balance > 0) {
140 /* Right-left case. */
148 x->balance = z->balance < 0 ? 1 : 0;
149 y->balance = z->balance > 0 ? -1 : 0;
152 /* Right-right case. */
157 if (y->balance < 0) {
171 if (x->balance > 0) {
173 if (y->balance < 0) {
174 /* Left-right case. */
182 x->balance = z->balance > 0 ? -1 : 0;
183 y->balance = z->balance < 0 ? 1 : 0;
186 /* Left-left case. */
191 if (y->balance > 0) {
206 /* Return the parent of the old entry. */