2 splay_tree.c -- splay tree and linked list convenience
3 Copyright (C) 2014 Guus Sliepen <guus@meshlink.io>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include "splay_tree.h"
27 static splay_node_t *splay_top_down(splay_tree_t *tree, const void *data, int *result) {
28 splay_node_t left = {NULL}, right = {NULL};
29 splay_node_t *leftbottom = &left, *rightbottom = &right, *child, *grandchild;
30 splay_node_t *root = tree->root;
39 while((c = tree->compare(data, root->data))) {
40 if(c < 0 && (child = root->left)) {
41 c = tree->compare(data, child->data);
43 if(c < 0 && (grandchild = child->left)) {
44 rightbottom->left = child;
45 child->parent = rightbottom;
48 if((root->left = child->right))
49 child->right->parent = root;
55 grandchild->parent = NULL;
58 } else if(c > 0 && (grandchild = child->right)) {
59 leftbottom->right = child;
60 child->parent = leftbottom;
64 grandchild->parent = NULL;
66 rightbottom->left = root;
67 root->parent = rightbottom;
74 rightbottom->left = root;
75 root->parent = rightbottom;
84 } else if(c > 0 && (child = root->right)) {
85 c = tree->compare(data, child->data);
87 if(c > 0 && (grandchild = child->right)) {
88 leftbottom->right = child;
89 child->parent = leftbottom;
92 if((root->right = child->left))
93 child->left->parent = root;
99 grandchild->parent = NULL;
102 } else if(c < 0 && (grandchild = child->left)) {
103 rightbottom->left = child;
104 child->parent = rightbottom;
108 grandchild->parent = NULL;
110 leftbottom->right = root;
111 root->parent = leftbottom;
118 leftbottom->right = root;
119 root->parent = leftbottom;
123 child->parent = NULL;
136 leftbottom->right = root->left;
137 root->left->parent = leftbottom;
139 root->left = left.right;
140 left.right->parent = root;
145 rightbottom->left = root->right;
146 root->right->parent = rightbottom;
148 root->right = right.left;
149 right.left->parent = root;
161 static void splay_bottom_up(splay_tree_t *tree, splay_node_t *node) {
162 splay_node_t *parent, *grandparent, *greatgrandparent;
164 while((parent = node->parent)) {
165 if(!(grandparent = parent->parent)) { /* zig */
166 if(node == parent->left) {
167 if((parent->left = node->right))
168 parent->left->parent = parent;
169 node->right = parent;
171 if((parent->right = node->left))
172 parent->right->parent = parent;
176 parent->parent = node;
179 greatgrandparent = grandparent->parent;
181 if(node == parent->left && parent == grandparent->left) { /* left zig-zig */
182 if((grandparent->left = parent->right))
183 grandparent->left->parent = grandparent;
184 parent->right = grandparent;
185 grandparent->parent = parent;
187 if((parent->left = node->right))
188 parent->left->parent = parent;
189 node->right = parent;
190 parent->parent = node;
191 } else if(node == parent->right && parent == grandparent->right) { /* right zig-zig */
192 if((grandparent->right = parent->left))
193 grandparent->right->parent = grandparent;
194 parent->left = grandparent;
195 grandparent->parent = parent;
197 if((parent->right = node->left))
198 parent->right->parent = parent;
200 parent->parent = node;
201 } else if(node == parent->right && parent == grandparent->left) { /* left-right zig-zag */
202 if((parent->right = node->left))
203 parent->right->parent = parent;
205 parent->parent = node;
207 if((grandparent->left = node->right))
208 grandparent->left->parent = grandparent;
209 node->right = grandparent;
210 grandparent->parent = node;
211 } else { /* right-left zig-zag */
212 if((parent->left = node->right))
213 parent->left->parent = parent;
214 node->right = parent;
215 parent->parent = node;
217 if((grandparent->right = node->left))
218 grandparent->right->parent = grandparent;
219 node->left = grandparent;
220 grandparent->parent = node;
223 if((node->parent = greatgrandparent)) {
224 if(grandparent == greatgrandparent->left)
225 greatgrandparent->left = node;
227 greatgrandparent->right = node;
235 /* (De)constructors */
237 splay_tree_t *splay_alloc_tree(splay_compare_t compare, splay_action_t delete) {
240 tree = xzalloc(sizeof(splay_tree_t));
241 tree->compare = compare;
242 tree->delete = delete;
247 void splay_free_tree(splay_tree_t *tree) {
251 splay_node_t *splay_alloc_node(void) {
252 return xzalloc(sizeof(splay_node_t));
255 void splay_free_node(splay_tree_t *tree, splay_node_t *node) {
256 if(node->data && tree->delete)
257 tree->delete(node->data);
264 void *splay_search(splay_tree_t *tree, const void *data) {
267 node = splay_search_node(tree, data);
269 return node ? node->data : NULL;
272 void *splay_search_closest(splay_tree_t *tree, const void *data, int *result) {
275 node = splay_search_closest_node(tree, data, result);
277 return node ? node->data : NULL;
280 void *splay_search_closest_smaller(splay_tree_t *tree, const void *data) {
283 node = splay_search_closest_smaller_node(tree, data);
285 return node ? node->data : NULL;
288 void *splay_search_closest_greater(splay_tree_t *tree, const void *data) {
291 node = splay_search_closest_greater_node(tree, data);
293 return node ? node->data : NULL;
296 splay_node_t *splay_search_node(splay_tree_t *tree, const void *data) {
300 node = splay_search_closest_node(tree, data, &result);
302 return result ? NULL : node;
305 splay_node_t *splay_search_closest_node_nosplay(const splay_tree_t *tree, const void *data, int *result) {
318 c = tree->compare(data, node->data);
339 splay_node_t *splay_search_closest_node(splay_tree_t *tree, const void *data, int *result) {
340 return splay_top_down(tree, data, result);
343 splay_node_t *splay_search_closest_smaller_node(splay_tree_t *tree, const void *data) {
347 node = splay_search_closest_node(tree, data, &result);
355 splay_node_t *splay_search_closest_greater_node(splay_tree_t *tree, const void *data) {
359 node = splay_search_closest_node(tree, data, &result);
367 /* Insertion and deletion */
369 splay_node_t *splay_insert(splay_tree_t *tree, void *data) {
370 splay_node_t *closest, *new;
374 new = splay_alloc_node();
376 splay_insert_top(tree, new);
378 closest = splay_search_closest_node(tree, data, &result);
383 new = splay_alloc_node();
387 splay_insert_before(tree, closest, new);
389 splay_insert_after(tree, closest, new);
395 splay_node_t *splay_insert_node(splay_tree_t *tree, splay_node_t *node) {
396 splay_node_t *closest;
399 node->left = node->right = node->parent = node->next = node->prev = NULL;
402 splay_insert_top(tree, node);
404 closest = splay_search_closest_node(tree, node->data, &result);
410 splay_insert_before(tree, closest, node);
412 splay_insert_after(tree, closest, node);
418 void splay_insert_top(splay_tree_t *tree, splay_node_t *node) {
419 node->prev = node->next = node->left = node->right = node->parent = NULL;
420 tree->head = tree->tail = tree->root = node;
424 void splay_insert_before(splay_tree_t *tree, splay_node_t *before, splay_node_t *node) {
427 splay_insert_after(tree, tree->tail, node);
429 splay_insert_top(tree, node);
434 if((node->prev = before->prev))
435 before->prev->next = node;
440 splay_bottom_up(tree, before);
442 node->right = before;
443 before->parent = node;
444 if((node->left = before->left))
445 before->left->parent = node;
453 void splay_insert_after(splay_tree_t *tree, splay_node_t *after, splay_node_t *node) {
456 splay_insert_before(tree, tree->head, node);
458 splay_insert_top(tree, node);
463 if((node->next = after->next))
464 after->next->prev = node;
469 splay_bottom_up(tree, after);
472 after->parent = node;
473 if((node->right = after->right))
474 after->right->parent = node;
482 splay_node_t *splay_unlink(splay_tree_t *tree, void *data) {
485 node = splay_search_node(tree, data);
488 splay_unlink_node(tree, node);
493 void splay_unlink_node(splay_tree_t *tree, splay_node_t *node) {
495 node->prev->next = node->next;
497 tree->head = node->next;
500 node->next->prev = node->prev;
502 tree->tail = node->prev;
504 splay_bottom_up(tree, node);
507 node->left->parent = NULL;
508 tree->root = node->left;
509 if((node->prev->right = node->right))
510 node->right->parent = node->prev;
511 } else if(node->next) {
512 tree->root = node->right;
513 node->right->parent = NULL;
520 void splay_delete_node(splay_tree_t *tree, splay_node_t *node) {
521 splay_unlink_node(tree, node);
522 splay_free_node(tree, node);
525 void splay_delete(splay_tree_t *tree, void *data) {
528 node = splay_search_node(tree, data);
531 splay_delete_node(tree, node);
534 /* Fast tree cleanup */
536 void splay_delete_tree(splay_tree_t *tree) {
537 for(splay_node_t *node = tree->head, *next; node; node = next) {
539 splay_free_node(tree, node);
542 splay_free_tree(tree);
547 void splay_foreach(const splay_tree_t *tree, splay_action_t action) {
548 for(splay_node_t *node = tree->head, *next; node; node = next) {
554 void splay_foreach_node(const splay_tree_t *tree, splay_action_t action) {
555 for(splay_node_t *node = tree->head, *next; node; node = next) {