-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathavl.c
More file actions
193 lines (168 loc) · 5.09 KB
/
Copy pathavl.c
File metadata and controls
193 lines (168 loc) · 5.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
* avl.c - Generic intrusive AVL tree implementation
*
* Author: u/ApparentlyPlus
*/
#include <klibc/avl.h>
void avl_init(avl_tree_t* tree, avl_cmp_fn cmp) {
tree->root = NULL;
tree->cmp = cmp;
}
#pragma region Internal AVL Helpers
static inline int node_h(const avl_node_t* n) {
return n ? n->height : 0;
}
static inline void upd_height(avl_node_t* n) {
int l = node_h(n->left), r = node_h(n->right);
n->height = 1 + (l > r ? l : r);
}
static inline int bal(avl_node_t* n) {
return n ? node_h(n->left) - node_h(n->right) : 0;
}
/*
* Redirect the child pointer in parent (or tree->root) that currently
* points to old_child so it points to new_child instead.
*/
static void relink(avl_tree_t* tree, avl_node_t* parent, avl_node_t* old_child, avl_node_t* new_child) {
if (!parent)
tree->root = new_child;
else if (parent->left == old_child)
parent->left = new_child;
else
parent->right = new_child;
if (new_child)
new_child->parent = parent;
}
static avl_node_t* rot_right(avl_tree_t* tree, avl_node_t* y) {
avl_node_t* x = y->left;
avl_node_t* T = x->right;
relink(tree, y->parent, y, x);
x->right = y; y->parent = x;
y->left = T; if (T) T->parent = y;
upd_height(y);
upd_height(x);
return x;
}
static avl_node_t* rot_left(avl_tree_t* tree, avl_node_t* x) {
avl_node_t* y = x->right;
avl_node_t* T = y->left;
relink(tree, x->parent, x, y);
y->left = x; x->parent = y;
x->right = T; if (T) T->parent = x;
upd_height(x);
upd_height(y);
return y;
}
/*
* Walk up from n to the root, recomputing heights and rebalancing.
*/
static void fix_up(avl_tree_t* tree, avl_node_t* n) {
while (n) {
upd_height(n);
int b = bal(n);
if (b > 1) {
if (bal(n->left) < 0)
rot_left(tree, n->left); /* LR case */
n = rot_right(tree, n);
} else if (b < -1) {
if (bal(n->right) > 0)
rot_right(tree, n->right); /* RL case */
n = rot_left(tree, n);
}
n = n->parent;
}
}
static avl_node_t* subtree_min(avl_node_t* n) {
while (n->left) n = n->left;
return n;
}
#pragma region Public API
void avl_insert(avl_tree_t* tree, avl_node_t* node) {
node->left = node->right = node->parent = NULL;
node->height = 1;
if (!tree->root) { tree->root = node; return; }
avl_node_t* cur = tree->root;
avl_node_t* par = NULL;
while (cur) {
par = cur;
cur = (tree->cmp(node, cur) < 0) ? cur->left : cur->right;
}
node->parent = par;
if (tree->cmp(node, par) < 0) par->left = node;
else par->right = node;
fix_up(tree, par);
}
void avl_remove(avl_tree_t* tree, avl_node_t* node) {
avl_node_t* fix;
if (!node->left) {
fix = node->parent;
relink(tree, node->parent, node, node->right);
} else if (!node->right) {
fix = node->parent;
relink(tree, node->parent, node, node->left);
} else {
/* Replace node with its in-order successor */
avl_node_t* succ = subtree_min(node->right);
fix = (succ->parent == node) ? succ : succ->parent;
/* Detach succ from its current position */
relink(tree, succ->parent, succ, succ->right);
/* Place succ where node was */
relink(tree, node->parent, node, succ);
succ->left = node->left;
succ->right = node->right;
succ->height = node->height; /* fix_up will correct this */
if (succ->left) succ->left->parent = succ;
if (succ->right) succ->right->parent = succ;
}
fix_up(tree, fix);
}
avl_node_t* avl_find(avl_tree_t* tree, avl_node_t* key) {
avl_node_t* n = tree->root;
while (n) {
int c = tree->cmp(key, n);
if (c < 0) n = n->left;
else if (c > 0) n = n->right;
else return n;
}
return NULL;
}
avl_node_t* avl_floor(avl_tree_t* tree, avl_node_t* key) {
avl_node_t* n = tree->root, *res = NULL;
while (n) {
int c = tree->cmp(key, n);
if (c < 0) n = n->left;
else if (c > 0) { res = n; n = n->right; }
else return n;
}
return res;
}
avl_node_t* avl_ceil(avl_tree_t* tree, avl_node_t* key) {
avl_node_t* n = tree->root, *res = NULL;
while (n) {
int c = tree->cmp(key, n);
if (c > 0) n = n->right;
else if (c < 0) { res = n; n = n->left; }
else return n;
}
return res;
}
avl_node_t* avl_min(avl_tree_t* tree) {
if (!tree->root) return NULL;
return subtree_min(tree->root);
}
avl_node_t* avl_next(avl_node_t* node) {
if (node->right) return subtree_min(node->right);
avl_node_t* p = node->parent;
while (p && node == p->right) { node = p; p = p->parent; }
return p;
}
avl_node_t* avl_prev(avl_node_t* node) {
if (node->left) {
node = node->left;
while (node->right) node = node->right;
return node;
}
avl_node_t* p = node->parent;
while (p && node == p->left) { node = p; p = p->parent; }
return p;
}