2023-01-30 20:57:55 +00:00
|
|
|
/*
|
|
|
|
|
The Clear BSD License
|
|
|
|
|
|
|
|
|
|
Copyright (c) 2023 Max Wash
|
|
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
|
modification, are permitted (subject to the limitations in the disclaimer
|
|
|
|
|
below) provided that the following conditions are met:
|
|
|
|
|
|
|
|
|
|
- Redistributions of source code must retain the above copyright notice,
|
|
|
|
|
this list of conditions and the following disclaimer.
|
|
|
|
|
|
|
|
|
|
- Redistributions in binary form must reproduce the above copyright
|
|
|
|
|
notice, this list of conditions and the following disclaimer in the
|
|
|
|
|
documentation and/or other materials provided with the distribution.
|
|
|
|
|
|
|
|
|
|
- Neither the name of the copyright holder nor the names of its
|
|
|
|
|
contributors may be used to endorse or promote products derived from this
|
|
|
|
|
software without specific prior written permission.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/* templated AVL binary tree implementation
|
|
|
|
|
|
|
|
|
|
this file implements an extensible AVL binary tree data structure.
|
|
|
|
|
|
|
|
|
|
the primary rule of an AVL binary tree is that for a given node N,
|
|
|
|
|
the heights of N's left and right subtrees can differ by at most 1.
|
|
|
|
|
|
|
|
|
|
the height of a subtree is the length of the longest path between
|
|
|
|
|
the root of the subtree and a leaf node, including the root node itself.
|
|
|
|
|
|
|
|
|
|
the height of a leaf node is 1.
|
|
|
|
|
|
|
|
|
|
when a node is inserted into or deleted from the tree, this rule may
|
|
|
|
|
be broken, in which the tree must be rotated to restore the balance.
|
|
|
|
|
|
|
|
|
|
no more than one rotation is required for any insert operations,
|
|
|
|
|
while multiple rotations may be required for a delete operation.
|
|
|
|
|
|
|
|
|
|
there are four types of rotations that can be applied to a tree:
|
|
|
|
|
- left rotation
|
|
|
|
|
- right rotation
|
|
|
|
|
- double left rotations
|
|
|
|
|
- double right rotations
|
|
|
|
|
|
|
|
|
|
by enforcing the balance rule, for a tree with n nodes, the worst-case
|
|
|
|
|
performance for insert, delete, and search operations is guaranteed
|
|
|
|
|
to be O(log n).
|
|
|
|
|
|
|
|
|
|
this file intentionally excludes any kind of search function implementation.
|
|
|
|
|
it is up to the programmer to implement their own tree node type
|
2023-04-12 20:17:11 +01:00
|
|
|
using struct btree_node, and their own search function using struct btree.
|
2023-01-30 20:57:55 +00:00
|
|
|
this allows the programmer to define their own node types with complex
|
|
|
|
|
non-integer key types. btree.h contains a number of macros to help
|
|
|
|
|
define these functions. the macros do all the work, you just have to
|
|
|
|
|
provide a comparator function.
|
|
|
|
|
*/
|
|
|
|
|
|
2026-02-19 18:54:48 +00:00
|
|
|
#include <kernel/btree.h>
|
2023-01-15 08:24:51 +00:00
|
|
|
#include <stddef.h>
|
2023-01-21 17:36:37 +00:00
|
|
|
|
2023-01-15 08:24:51 +00:00
|
|
|
#define MAX(a, b) ((a) > (b) ? (a) : (b))
|
|
|
|
|
#define MIN(a, b) ((a) < (b) ? (a) : (b))
|
|
|
|
|
|
|
|
|
|
#define IS_LEFT_CHILD(p, c) ((p) && (c) && ((p)->b_left == (c)))
|
|
|
|
|
#define IS_RIGHT_CHILD(p, c) ((p) && (c) && ((p)->b_right == (c)))
|
|
|
|
|
|
2023-01-18 19:45:58 +00:00
|
|
|
#define HAS_LEFT_CHILD(x) ((x) && ((x)->b_left))
|
|
|
|
|
#define HAS_RIGHT_CHILD(x) ((x) && ((x)->b_right))
|
|
|
|
|
|
|
|
|
|
#define HAS_NO_CHILDREN(x) ((x) && (!(x)->b_left) && (!(x)->b_right))
|
|
|
|
|
#define HAS_ONE_CHILD(x) ((HAS_LEFT_CHILD(x) && !HAS_RIGHT_CHILD(x)) || (!HAS_LEFT_CHILD(x) && HAS_RIGHT_CHILD(x)))
|
|
|
|
|
#define HAS_TWO_CHILDREN(x) (HAS_LEFT_CHILD(x) && HAS_RIGHT_CHILD(x))
|
|
|
|
|
|
2023-01-19 20:51:59 +00:00
|
|
|
#define HEIGHT(x) ((x) ? (x)->b_height : 0)
|
|
|
|
|
|
2023-04-12 20:17:11 +01:00
|
|
|
static inline void update_height(struct btree_node *x)
|
2023-01-19 20:51:59 +00:00
|
|
|
{
|
|
|
|
|
x->b_height = MAX(HEIGHT(x->b_left), HEIGHT((x->b_right))) + 1;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-12 20:17:11 +01:00
|
|
|
static inline int bf(struct btree_node *x)
|
2023-01-19 20:51:59 +00:00
|
|
|
{
|
|
|
|
|
int bf = 0;
|
|
|
|
|
|
|
|
|
|
if (!x) {
|
|
|
|
|
return bf;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (x->b_right) {
|
|
|
|
|
bf += x->b_right->b_height;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (x->b_left) {
|
|
|
|
|
bf -= x->b_left->b_height;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return bf;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-30 20:57:55 +00:00
|
|
|
/* perform a left rotation on a subtree
|
|
|
|
|
|
|
|
|
|
if you have a tree like this:
|
|
|
|
|
|
|
|
|
|
Z
|
|
|
|
|
/ \
|
|
|
|
|
X .
|
|
|
|
|
/ \
|
|
|
|
|
. Y
|
|
|
|
|
/ \
|
|
|
|
|
. .
|
|
|
|
|
|
|
|
|
|
and you perform a left rotation on node X,
|
|
|
|
|
you will get the following tree:
|
|
|
|
|
|
|
|
|
|
Z
|
|
|
|
|
/ \
|
|
|
|
|
Y .
|
|
|
|
|
/ \
|
|
|
|
|
X .
|
|
|
|
|
/ \
|
|
|
|
|
. .
|
|
|
|
|
|
|
|
|
|
note that this function does NOT update b_height for the rotated
|
|
|
|
|
nodes. it is up to you to call update_height_to_root().
|
|
|
|
|
*/
|
2023-04-12 20:17:11 +01:00
|
|
|
static void rotate_left(struct btree *tree, struct btree_node *x)
|
2023-01-13 18:31:29 +00:00
|
|
|
{
|
2023-04-12 20:17:11 +01:00
|
|
|
struct btree_node *y = x->b_right;
|
2023-01-21 17:36:37 +00:00
|
|
|
|
2023-04-12 20:17:11 +01:00
|
|
|
struct btree_node *p = x->b_parent;
|
2023-01-15 08:24:51 +00:00
|
|
|
|
|
|
|
|
if (y->b_left) {
|
|
|
|
|
y->b_left->b_parent = x;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
x->b_right = y->b_left;
|
|
|
|
|
|
|
|
|
|
if (!p) {
|
|
|
|
|
tree->b_root = y;
|
|
|
|
|
} else if (x == p->b_left) {
|
|
|
|
|
p->b_left = y;
|
|
|
|
|
} else {
|
|
|
|
|
p->b_right = y;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
x->b_parent = y;
|
|
|
|
|
y->b_left = x;
|
|
|
|
|
y->b_parent = p;
|
2023-01-21 17:36:37 +00:00
|
|
|
}
|
2023-01-15 08:24:51 +00:00
|
|
|
|
2023-04-12 20:17:11 +01:00
|
|
|
static void update_height_to_root(struct btree_node *x)
|
2023-01-21 17:36:37 +00:00
|
|
|
{
|
|
|
|
|
while (x) {
|
|
|
|
|
update_height(x);
|
|
|
|
|
x = x->b_parent;
|
2023-01-15 08:24:51 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-30 20:57:55 +00:00
|
|
|
/* perform a right rotation on a subtree
|
|
|
|
|
|
|
|
|
|
if you have a tree like this:
|
|
|
|
|
|
|
|
|
|
Z
|
|
|
|
|
/ \
|
|
|
|
|
. X
|
|
|
|
|
/ \
|
|
|
|
|
Y .
|
|
|
|
|
/ \
|
|
|
|
|
. .
|
|
|
|
|
|
|
|
|
|
and you perform a right rotation on node X,
|
|
|
|
|
you will get the following tree:
|
|
|
|
|
|
|
|
|
|
Z
|
|
|
|
|
/ \
|
|
|
|
|
. Y
|
|
|
|
|
/ \
|
|
|
|
|
. X
|
|
|
|
|
/ \
|
|
|
|
|
. .
|
|
|
|
|
|
|
|
|
|
note that this function does NOT update b_height for the rotated
|
|
|
|
|
nodes. it is up to you to call update_height_to_root().
|
|
|
|
|
*/
|
2023-04-12 20:17:11 +01:00
|
|
|
static void rotate_right(struct btree *tree, struct btree_node *y)
|
2023-01-15 08:24:51 +00:00
|
|
|
{
|
2023-04-12 20:17:11 +01:00
|
|
|
struct btree_node *x = y->b_left;
|
2023-01-22 20:21:29 +00:00
|
|
|
|
2023-04-12 20:17:11 +01:00
|
|
|
struct btree_node *p = y->b_parent;
|
2023-01-15 08:24:51 +00:00
|
|
|
|
|
|
|
|
if (x->b_right) {
|
|
|
|
|
x->b_right->b_parent = y;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
y->b_left = x->b_right;
|
|
|
|
|
|
|
|
|
|
if (!p) {
|
|
|
|
|
tree->b_root = x;
|
2023-01-21 17:36:37 +00:00
|
|
|
} else if (y == p->b_left) {
|
2023-01-15 08:24:51 +00:00
|
|
|
p->b_left = x;
|
|
|
|
|
} else {
|
|
|
|
|
p->b_right = x;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
y->b_parent = x;
|
|
|
|
|
x->b_right = y;
|
|
|
|
|
x->b_parent = p;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-30 20:57:55 +00:00
|
|
|
/* for a given node Z, perform a right rotation on Z's right child,
|
|
|
|
|
followed by a left rotation on Z itself.
|
|
|
|
|
|
|
|
|
|
if you have a tree like this:
|
|
|
|
|
|
|
|
|
|
Z
|
|
|
|
|
/ \
|
|
|
|
|
. X
|
|
|
|
|
/ \
|
|
|
|
|
Y .
|
|
|
|
|
/ \
|
|
|
|
|
. .
|
|
|
|
|
|
|
|
|
|
and you perform a double-left rotation on node Z,
|
|
|
|
|
you will get the following tree:
|
|
|
|
|
|
|
|
|
|
Y
|
|
|
|
|
/ \
|
|
|
|
|
/ \
|
|
|
|
|
Z X
|
|
|
|
|
/ \ / \
|
|
|
|
|
. . . .
|
|
|
|
|
|
|
|
|
|
note that, unlike rotate_left and rotate_right, this function
|
|
|
|
|
DOES update b_height for the rotated nodes (since it needs to be
|
|
|
|
|
done in a certain order).
|
|
|
|
|
*/
|
2023-04-12 20:17:11 +01:00
|
|
|
static void rotate_double_left(struct btree *tree, struct btree_node *z)
|
2023-01-15 08:24:51 +00:00
|
|
|
{
|
2023-04-12 20:17:11 +01:00
|
|
|
struct btree_node *x = z->b_right;
|
|
|
|
|
struct btree_node *y = x->b_left;
|
2023-01-15 08:24:51 +00:00
|
|
|
|
|
|
|
|
rotate_right(tree, x);
|
|
|
|
|
rotate_left(tree, z);
|
2023-01-19 20:51:59 +00:00
|
|
|
|
|
|
|
|
update_height(z);
|
|
|
|
|
update_height(x);
|
|
|
|
|
|
2023-01-21 17:36:37 +00:00
|
|
|
while (y) {
|
|
|
|
|
update_height(y);
|
|
|
|
|
y = y->b_parent;
|
2023-01-19 20:51:59 +00:00
|
|
|
}
|
2023-01-15 08:24:51 +00:00
|
|
|
}
|
|
|
|
|
|
2023-01-30 20:57:55 +00:00
|
|
|
/* for a given node Z, perform a left rotation on Z's left child,
|
|
|
|
|
followed by a right rotation on Z itself.
|
|
|
|
|
|
|
|
|
|
if you have a tree like this:
|
|
|
|
|
|
|
|
|
|
Z
|
|
|
|
|
/ \
|
|
|
|
|
X .
|
|
|
|
|
/ \
|
|
|
|
|
. Y
|
|
|
|
|
/ \
|
|
|
|
|
. .
|
|
|
|
|
|
|
|
|
|
and you perform a double-right rotation on node Z,
|
|
|
|
|
you will get the following tree:
|
|
|
|
|
|
|
|
|
|
Y
|
|
|
|
|
/ \
|
|
|
|
|
/ \
|
|
|
|
|
X Z
|
|
|
|
|
/ \ / \
|
|
|
|
|
. . . .
|
|
|
|
|
|
|
|
|
|
note that, unlike rotate_left and rotate_right, this function
|
|
|
|
|
DOES update b_height for the rotated nodes (since it needs to be
|
|
|
|
|
done in a certain order).
|
|
|
|
|
*/
|
2023-04-12 20:17:11 +01:00
|
|
|
static void rotate_double_right(struct btree *tree, struct btree_node *z)
|
2023-01-15 08:24:51 +00:00
|
|
|
{
|
2023-04-12 20:17:11 +01:00
|
|
|
struct btree_node *x = z->b_left;
|
|
|
|
|
struct btree_node *y = x->b_right;
|
2023-01-15 08:24:51 +00:00
|
|
|
|
|
|
|
|
rotate_left(tree, x);
|
|
|
|
|
rotate_right(tree, z);
|
2023-01-19 20:51:59 +00:00
|
|
|
|
|
|
|
|
update_height(z);
|
|
|
|
|
update_height(x);
|
|
|
|
|
|
2023-01-21 17:36:37 +00:00
|
|
|
while (y) {
|
|
|
|
|
update_height(y);
|
|
|
|
|
y = y->b_parent;
|
2023-01-19 20:51:59 +00:00
|
|
|
}
|
2023-01-15 08:24:51 +00:00
|
|
|
}
|
|
|
|
|
|
2023-01-30 20:57:55 +00:00
|
|
|
/* run after an insert operation. checks that the balance factor
|
|
|
|
|
of the local subtree is within the range -1 <= BF <= 1. if it
|
|
|
|
|
is not, rotate the subtree to restore balance.
|
|
|
|
|
|
|
|
|
|
note that at most one rotation should be required after a node
|
|
|
|
|
is inserted into the tree.
|
|
|
|
|
|
|
|
|
|
this function depends on all nodes in the tree having
|
|
|
|
|
correct b_height values.
|
|
|
|
|
|
|
|
|
|
@param w the node that was just inserted into the tree
|
|
|
|
|
*/
|
2023-04-12 20:17:11 +01:00
|
|
|
static void insert_fixup(struct btree *tree, struct btree_node *w)
|
2023-01-15 08:24:51 +00:00
|
|
|
{
|
2023-04-12 20:17:11 +01:00
|
|
|
struct btree_node *z = NULL, *y = NULL, *x = NULL;
|
2023-01-18 19:45:58 +00:00
|
|
|
|
2023-01-15 08:24:51 +00:00
|
|
|
z = w;
|
|
|
|
|
while (z) {
|
2023-01-19 20:51:59 +00:00
|
|
|
if (bf(z) >= -1 && bf(z) <= 1) {
|
2023-01-15 08:24:51 +00:00
|
|
|
goto next_ancestor;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (IS_LEFT_CHILD(z, y)) {
|
|
|
|
|
if (IS_LEFT_CHILD(y, x)) {
|
|
|
|
|
rotate_right(tree, z);
|
2023-01-21 17:36:37 +00:00
|
|
|
update_height_to_root(z);
|
2023-01-15 08:24:51 +00:00
|
|
|
} else {
|
|
|
|
|
rotate_double_right(tree, z);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (IS_LEFT_CHILD(y, x)) {
|
|
|
|
|
rotate_double_left(tree, z);
|
|
|
|
|
} else {
|
|
|
|
|
rotate_left(tree, z);
|
2023-01-21 17:36:37 +00:00
|
|
|
update_height_to_root(z);
|
2023-01-15 08:24:51 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
next_ancestor:
|
|
|
|
|
x = y;
|
|
|
|
|
y = z;
|
|
|
|
|
z = z->b_parent;
|
|
|
|
|
}
|
2023-01-22 20:21:29 +00:00
|
|
|
}
|
|
|
|
|
|
2023-01-30 20:57:55 +00:00
|
|
|
/* run after a delete operation. checks that the balance factor
|
|
|
|
|
of the local subtree is within the range -1 <= BF <= 1. if it
|
|
|
|
|
is not, rotate the subtree to restore balance.
|
|
|
|
|
|
|
|
|
|
note that, unlike insert_fixup, multiple rotations may be required
|
|
|
|
|
to restore balance after a node is deleted.
|
|
|
|
|
|
|
|
|
|
this function depends on all nodes in the tree having
|
|
|
|
|
correct b_height values.
|
|
|
|
|
|
|
|
|
|
@param w one of the following:
|
|
|
|
|
- the parent of the node that was deleted if the node
|
|
|
|
|
had no children.
|
|
|
|
|
- the parent of the node that replaced the deleted node
|
|
|
|
|
if the deleted node had two children.
|
|
|
|
|
- the node that replaced the node that was deleted, if
|
|
|
|
|
the node that was deleted had one child.
|
|
|
|
|
*/
|
2023-04-12 20:17:11 +01:00
|
|
|
static void delete_fixup(struct btree *tree, struct btree_node *w)
|
2023-01-22 20:21:29 +00:00
|
|
|
{
|
2023-04-12 20:17:11 +01:00
|
|
|
struct btree_node *z = w;
|
2023-01-22 20:21:29 +00:00
|
|
|
|
|
|
|
|
while (z) {
|
|
|
|
|
if (bf(z) > 1) {
|
2023-01-24 21:26:22 +00:00
|
|
|
if (bf(z->b_right) >= 0) {
|
2023-01-22 20:21:29 +00:00
|
|
|
rotate_left(tree, z);
|
|
|
|
|
update_height_to_root(z);
|
|
|
|
|
} else {
|
2023-01-30 20:57:55 +00:00
|
|
|
rotate_double_left(tree, z);
|
2023-01-22 20:21:29 +00:00
|
|
|
}
|
|
|
|
|
} else if (bf(z) < -1) {
|
2023-01-24 21:26:22 +00:00
|
|
|
if (bf(z->b_left) <= 0) {
|
2023-01-22 20:21:29 +00:00
|
|
|
rotate_right(tree, z);
|
|
|
|
|
update_height_to_root(z);
|
|
|
|
|
} else {
|
|
|
|
|
rotate_double_right(tree, z);
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-01-21 21:42:54 +00:00
|
|
|
|
2023-01-22 20:21:29 +00:00
|
|
|
z = z->b_parent;
|
|
|
|
|
}
|
2023-01-13 18:31:29 +00:00
|
|
|
}
|
|
|
|
|
|
2023-01-30 20:57:55 +00:00
|
|
|
/* updates b_height for all nodes between the inserted node and the root
|
|
|
|
|
of the tree, and calls insert_fixup.
|
|
|
|
|
|
|
|
|
|
@param node the node that was just inserted into the tree.
|
|
|
|
|
*/
|
2023-04-12 20:17:11 +01:00
|
|
|
void btree_insert_fixup(struct btree *tree, struct btree_node *node)
|
2023-01-13 18:31:29 +00:00
|
|
|
{
|
2023-01-26 18:30:14 +00:00
|
|
|
node->b_height = 0;
|
|
|
|
|
|
2023-04-12 20:17:11 +01:00
|
|
|
struct btree_node *cur = node;
|
2023-01-26 18:30:14 +00:00
|
|
|
while (cur) {
|
|
|
|
|
update_height(cur);
|
|
|
|
|
cur = cur->b_parent;
|
|
|
|
|
}
|
2023-01-22 20:21:29 +00:00
|
|
|
|
2023-01-26 18:30:14 +00:00
|
|
|
insert_fixup(tree, node);
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-30 20:57:55 +00:00
|
|
|
/* remove a node from a tree.
|
|
|
|
|
|
|
|
|
|
this function assumes that `node` has no children, and therefore
|
|
|
|
|
doesn't need to be replaced.
|
|
|
|
|
|
|
|
|
|
updates b_height for all nodes between `node` and the tree root.
|
|
|
|
|
|
|
|
|
|
@param node the node to delete.
|
|
|
|
|
*/
|
2023-04-12 20:17:11 +01:00
|
|
|
static struct btree_node *remove_node_with_no_children(struct btree *tree, struct btree_node *node)
|
2023-01-18 19:45:58 +00:00
|
|
|
{
|
2023-04-12 20:17:11 +01:00
|
|
|
struct btree_node *w = node->b_parent;
|
|
|
|
|
struct btree_node *p = node->b_parent;
|
2023-01-18 19:45:58 +00:00
|
|
|
node->b_parent = NULL;
|
|
|
|
|
|
|
|
|
|
if (!p) {
|
|
|
|
|
tree->b_root = NULL;
|
|
|
|
|
} else if (IS_LEFT_CHILD(p, node)) {
|
|
|
|
|
p->b_left = NULL;
|
|
|
|
|
} else {
|
|
|
|
|
p->b_right = NULL;
|
|
|
|
|
}
|
2023-01-19 20:51:59 +00:00
|
|
|
|
|
|
|
|
while (p) {
|
|
|
|
|
update_height(p);
|
|
|
|
|
p = p->b_parent;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return w;
|
2023-01-18 19:45:58 +00:00
|
|
|
}
|
|
|
|
|
|
2023-01-30 20:57:55 +00:00
|
|
|
/* remove a node from a tree.
|
|
|
|
|
|
|
|
|
|
this function assumes that `node` has one child.
|
|
|
|
|
the child of `node` is inherited by `node`'s parent, and `node` is removed.
|
|
|
|
|
|
|
|
|
|
updates b_height for all nodes between the node that replaced
|
|
|
|
|
`node` and the tree root.
|
|
|
|
|
|
|
|
|
|
@param node the node to delete.
|
|
|
|
|
*/
|
2023-04-12 20:17:11 +01:00
|
|
|
static struct btree_node *replace_node_with_one_subtree(struct btree *tree, struct btree_node *node)
|
2023-01-18 19:45:58 +00:00
|
|
|
{
|
2023-04-12 20:17:11 +01:00
|
|
|
struct btree_node *p = node->b_parent;
|
|
|
|
|
struct btree_node *z = NULL;
|
2023-01-18 19:45:58 +00:00
|
|
|
|
|
|
|
|
if (HAS_LEFT_CHILD(node)) {
|
|
|
|
|
z = node->b_left;
|
|
|
|
|
} else {
|
|
|
|
|
z = node->b_right;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-12 20:17:11 +01:00
|
|
|
struct btree_node *w = z;
|
2023-01-24 21:35:32 +00:00
|
|
|
if (!p) {
|
|
|
|
|
tree->b_root = z;
|
|
|
|
|
} else if (IS_LEFT_CHILD(p, node)) {
|
2023-01-18 19:45:58 +00:00
|
|
|
p->b_left = z;
|
|
|
|
|
} else if (IS_RIGHT_CHILD(p, node)) {
|
|
|
|
|
p->b_right = z;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
z->b_parent = p;
|
|
|
|
|
|
|
|
|
|
node->b_parent = NULL;
|
2023-01-19 20:51:59 +00:00
|
|
|
node->b_left = node->b_right = NULL;
|
|
|
|
|
|
|
|
|
|
while (z) {
|
|
|
|
|
update_height(z);
|
|
|
|
|
z = z->b_parent;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return w;
|
2023-01-18 19:45:58 +00:00
|
|
|
}
|
|
|
|
|
|
2023-01-30 20:57:55 +00:00
|
|
|
/* remove a node from a tree.
|
|
|
|
|
|
|
|
|
|
this function assumes that `node` has two children.
|
|
|
|
|
find the in-order successor Y of `node` (the largest node in `node`'s left sub-tree),
|
|
|
|
|
removes `node` from the tree and moves Y to where `node` used to be.
|
|
|
|
|
|
|
|
|
|
if Y has a child (it will never have more than one), have Y's parent inherit
|
|
|
|
|
Y's child.
|
|
|
|
|
|
|
|
|
|
updates b_height for all nodes between the deepest node that was modified
|
|
|
|
|
and the tree root.
|
|
|
|
|
|
|
|
|
|
@param z the node to delete.
|
|
|
|
|
*/
|
2023-04-12 20:17:11 +01:00
|
|
|
static struct btree_node *replace_node_with_two_subtrees(struct btree *tree, struct btree_node *z)
|
2023-01-18 19:45:58 +00:00
|
|
|
{
|
2023-01-24 21:26:22 +00:00
|
|
|
/* x will replace z */
|
2023-04-12 20:17:11 +01:00
|
|
|
struct btree_node *x = z->b_left;
|
2023-01-24 21:26:22 +00:00
|
|
|
|
|
|
|
|
while (x->b_right) {
|
|
|
|
|
x = x->b_right;
|
2023-01-18 19:45:58 +00:00
|
|
|
}
|
|
|
|
|
|
2023-01-24 21:26:22 +00:00
|
|
|
/* y is the node that will replace x (if x has a left child) */
|
2023-04-12 20:17:11 +01:00
|
|
|
struct btree_node *y = x->b_left;
|
2023-01-18 19:45:58 +00:00
|
|
|
|
2023-01-24 21:26:22 +00:00
|
|
|
/* w is the starting point for the height update and fixup */
|
2023-04-12 20:17:11 +01:00
|
|
|
struct btree_node *w = x;
|
2023-01-24 21:26:22 +00:00
|
|
|
if (w->b_parent != z) {
|
|
|
|
|
w = w->b_parent;
|
2023-01-18 19:45:58 +00:00
|
|
|
}
|
|
|
|
|
|
2023-01-24 21:26:22 +00:00
|
|
|
if (y) {
|
|
|
|
|
w = y;
|
|
|
|
|
}
|
2023-01-18 19:45:58 +00:00
|
|
|
|
2023-01-24 21:26:22 +00:00
|
|
|
if (IS_LEFT_CHILD(x->b_parent, x)) {
|
|
|
|
|
x->b_parent->b_left = y;
|
|
|
|
|
} else if (IS_RIGHT_CHILD(x->b_parent, x)) {
|
|
|
|
|
x->b_parent->b_right = y;
|
2023-01-18 19:45:58 +00:00
|
|
|
}
|
|
|
|
|
|
2023-01-24 21:26:22 +00:00
|
|
|
if (y) {
|
|
|
|
|
y->b_parent = x->b_parent;
|
|
|
|
|
}
|
2023-01-18 19:45:58 +00:00
|
|
|
|
2023-01-24 21:26:22 +00:00
|
|
|
if (IS_LEFT_CHILD(z->b_parent, z)) {
|
|
|
|
|
z->b_parent->b_left = x;
|
|
|
|
|
} else if (IS_RIGHT_CHILD(z->b_parent, z)) {
|
|
|
|
|
z->b_parent->b_right = x;
|
2023-01-18 19:45:58 +00:00
|
|
|
}
|
|
|
|
|
|
2023-01-24 21:26:22 +00:00
|
|
|
x->b_parent = z->b_parent;
|
|
|
|
|
x->b_left = z->b_left;
|
|
|
|
|
x->b_right = z->b_right;
|
|
|
|
|
|
|
|
|
|
if (x->b_left) {
|
|
|
|
|
x->b_left->b_parent = x;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (x->b_right) {
|
|
|
|
|
x->b_right->b_parent = x;
|
2023-01-19 20:51:59 +00:00
|
|
|
}
|
2023-01-18 19:45:58 +00:00
|
|
|
|
2023-01-24 21:26:22 +00:00
|
|
|
if (!x->b_parent) {
|
|
|
|
|
tree->b_root = x;
|
2023-01-18 19:45:58 +00:00
|
|
|
}
|
|
|
|
|
|
2023-04-12 20:17:11 +01:00
|
|
|
struct btree_node *cur = w;
|
2023-01-22 20:21:29 +00:00
|
|
|
while (cur) {
|
|
|
|
|
update_height(cur);
|
|
|
|
|
cur = cur->b_parent;
|
2023-01-18 19:45:58 +00:00
|
|
|
}
|
|
|
|
|
|
2023-01-19 20:51:59 +00:00
|
|
|
return w;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-30 20:57:55 +00:00
|
|
|
/* delete a node from the tree and re-balance it afterwards */
|
2023-04-12 20:17:11 +01:00
|
|
|
void btree_delete(struct btree *tree, struct btree_node *node)
|
2023-01-19 20:51:59 +00:00
|
|
|
{
|
2023-04-12 20:17:11 +01:00
|
|
|
struct btree_node *w = NULL;
|
2023-01-22 20:21:29 +00:00
|
|
|
|
2023-01-19 20:51:59 +00:00
|
|
|
if (HAS_NO_CHILDREN(node)) {
|
|
|
|
|
w = remove_node_with_no_children(tree, node);
|
|
|
|
|
} else if (HAS_ONE_CHILD(node)) {
|
|
|
|
|
w = replace_node_with_one_subtree(tree, node);
|
|
|
|
|
} else if (HAS_TWO_CHILDREN(node)) {
|
|
|
|
|
w = replace_node_with_two_subtrees(tree, node);
|
2023-01-18 19:45:58 +00:00
|
|
|
}
|
2023-01-19 20:51:59 +00:00
|
|
|
|
2023-01-22 20:21:29 +00:00
|
|
|
if (w) {
|
|
|
|
|
delete_fixup(tree, w);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
node->b_left = node->b_right = node->b_parent = NULL;
|
2023-01-15 08:24:51 +00:00
|
|
|
}
|
2023-01-26 20:35:56 +00:00
|
|
|
|
2023-04-12 20:17:11 +01:00
|
|
|
struct btree_node *btree_first(struct btree *tree)
|
2023-01-26 20:35:56 +00:00
|
|
|
{
|
2023-01-30 20:57:55 +00:00
|
|
|
/* the first node in the tree is the node with the smallest key.
|
|
|
|
|
we keep moving left until we can't go any further */
|
2023-04-12 20:17:11 +01:00
|
|
|
struct btree_node *cur = tree->b_root;
|
2023-01-26 20:35:56 +00:00
|
|
|
if (!cur) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while (cur->b_left) {
|
|
|
|
|
cur = cur->b_left;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return cur;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-12 20:17:11 +01:00
|
|
|
struct btree_node *btree_last(struct btree *tree)
|
2023-01-26 20:35:56 +00:00
|
|
|
{
|
2023-01-30 20:57:55 +00:00
|
|
|
/* the first node in the tree is the node with the largest key.
|
|
|
|
|
we keep moving right until we can't go any further */
|
2023-04-12 20:17:11 +01:00
|
|
|
struct btree_node *cur = tree->b_root;
|
2023-01-26 20:35:56 +00:00
|
|
|
if (!cur) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while (cur->b_right) {
|
|
|
|
|
cur = cur->b_right;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return cur;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-12 20:17:11 +01:00
|
|
|
struct btree_node *btree_next(struct btree_node *node)
|
2023-01-26 20:35:56 +00:00
|
|
|
{
|
|
|
|
|
if (!node) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-30 20:57:55 +00:00
|
|
|
/* there are two possibilities for the next node:
|
|
|
|
|
|
|
|
|
|
1. if `node` has a right sub-tree, every node in this sub-tree is bigger
|
|
|
|
|
than node. the in-order successor of `node` is the smallest node in
|
|
|
|
|
this subtree.
|
|
|
|
|
2. if `node` has no right sub-tree, we've reached the largest node in
|
|
|
|
|
the sub-tree rooted at `node`. we need to go back to our parent
|
|
|
|
|
and continue the search elsewhere.
|
|
|
|
|
*/
|
2023-01-26 20:35:56 +00:00
|
|
|
if (node->b_right) {
|
2023-01-30 20:57:55 +00:00
|
|
|
/* case 1: step into `node`'s right sub-tree and keep going
|
|
|
|
|
left to find the smallest node */
|
2023-04-12 20:17:11 +01:00
|
|
|
struct btree_node *cur = node->b_right;
|
2023-01-26 20:35:56 +00:00
|
|
|
while (cur->b_left) {
|
|
|
|
|
cur = cur->b_left;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return cur;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-30 20:57:55 +00:00
|
|
|
/* case 2: keep stepping back up towards the root of the tree.
|
|
|
|
|
if we encounter a step where we are our parent's left child,
|
|
|
|
|
we've found a parent with a value larger than us. this parent
|
|
|
|
|
is the in-order successor of `node` */
|
2023-01-26 20:35:56 +00:00
|
|
|
while (node->b_parent && node->b_parent->b_left != node) {
|
|
|
|
|
node = node->b_parent;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return node->b_parent;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-12 20:17:11 +01:00
|
|
|
struct btree_node *btree_prev(struct btree_node *node)
|
2023-01-26 20:35:56 +00:00
|
|
|
{
|
|
|
|
|
if (!node) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-30 20:57:55 +00:00
|
|
|
/* there are two possibilities for the previous node:
|
|
|
|
|
|
|
|
|
|
1. if `node` has a left sub-tree, every node in this sub-tree is smaller
|
|
|
|
|
than `node`. the in-order predecessor of `node` is the largest node in
|
|
|
|
|
this subtree.
|
|
|
|
|
2. if `node` has no left sub-tree, we've reached the smallest node in
|
|
|
|
|
the sub-tree rooted at `node`. we need to go back to our parent
|
|
|
|
|
and continue the search elsewhere.
|
|
|
|
|
*/
|
2023-01-26 20:35:56 +00:00
|
|
|
if (node->b_left) {
|
2023-01-30 20:57:55 +00:00
|
|
|
/* case 1: step into `node`'s left sub-tree and keep going
|
|
|
|
|
right to find the largest node */
|
2023-04-12 20:17:11 +01:00
|
|
|
struct btree_node *cur = node->b_left;
|
2023-01-26 20:35:56 +00:00
|
|
|
while (cur->b_right) {
|
|
|
|
|
cur = cur->b_right;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return cur;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-30 20:57:55 +00:00
|
|
|
/* case 2: keep stepping back up towards the root of the tree.
|
|
|
|
|
if we encounter a step where we are our parent's right child,
|
|
|
|
|
we've found a parent with a value smaller than us. this parent
|
|
|
|
|
is the in-order predecessor of `node`. */
|
2023-01-26 20:35:56 +00:00
|
|
|
while (node->b_parent && node->b_parent->b_right != node) {
|
|
|
|
|
node = node->b_parent;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return node->b_parent;
|
|
|
|
|
}
|