summaryrefslogtreecommitdiff
path: root/src/aatree.c
blob: 6a51784dce50c19800781f82ca453d28b4011023 (plain)
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/*
 * AA-Tree - Binary tree with embeddable nodes.
 * 
 * Copyright (c) 2007-2009  Marko Kreen, Skype Technologies OÜ
 * 
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

/*
 * Self-balancing binary tree.
 *
 * Here is an implementation of AA-tree (Arne Andersson tree)
 * which is simplification of Red-Black tree.
 *
 * Red-black tree has following properties that must be kept:
 * 1. A node is either red or black.
 * 2. The root is black.
 * 3. All leaves (NIL nodes) are black.
 * 4. Both childen of red node are black.
 * 5. Every path from root to leaf contains same number of black nodes.
 *
 * AA-tree adds additional property:
 * 6. Red node can exist only as a right node.
 *
 * Red-black tree properties quarantee that the longest path is max 2x longer
 * than shortest path (B-R-B-R-B-R-B vs. B-B-B-B) thus the tree will be roughly
 * balanced.  Also it has good worst-case guarantees for insertion and deletion,
 * which makes it good tool for real-time applications.
 *
 * AA-tree removes most special cases from RB-tree, thus making resulting
 * code lot simpler.  It requires slightly more rotations when inserting
 * and deleting but also keeps the tree more balanced.
 */


#include "aatree.h"

typedef struct AATree Tree;
typedef struct AANode Node;

/*
 * NIL node
 */
#define NIL ((struct AANode *)&_nil)
static const struct AANode _nil = { NIL, NIL, 0 };

/*
 * Rebalancing.  AA-tree needs only 2 operations
 * to keep the tree balanced.
 */

/*
 * Fix red on left.
 *
 *     X          Y
 *    /     -->    \
 *   Y              X
 *    \            /
 *     a          a
 */
static inline Node * skew(Node *x)
{
	Node *y = x->left;
	if (x->level == y->level && x != NIL) {
		x->left = y->right;
		y->right = x;
		return y;
	}
	return x;
}

/*
 * Fix 2 reds on right.
 *
 *    X                Y
 *     \              / \
 *      Y      -->   X   Z
 *     / \            \
 *    a   Z            a
 */
static inline Node * split(Node *x)
{
	Node *y = x->right;
	if (x->level == y->right->level && x != NIL) {
		x->right = y->left;
		y->left = x;
		y->level++;
		return y;
	}
	return x;
}

/* insert is easy */
static Node *rebalance_on_insert(Node *current)
{
	return split(skew(current));
}

/* remove is bit more tricky */
static Node *rebalance_on_remove(Node *current)
{
	/*
	 * Removal can create a gap in levels,
	 * fix it by lowering current->level.
	 */
	if (current->left->level < current->level - 1
	    || current->right->level < current->level - 1)
	{
		current->level--;

		/* if ->right is red, change it's level too */
		if (current->right->level > current->level)
			current->right->level = current->level;

		/* reshape, ask Arne about those */
		current = skew(current);
		current->right = skew(current->right);
		current->right->right = skew(current->right->right);
		current = split(current);
		current->right = split(current->right);
	}
	return current;
}

/*
 * Recursive insertion
 */

static Node * insert_sub(Tree *tree, Node *current, uintptr_t value, Node *node)
{
	int cmp;

	if (current == NIL) {
		/*
		 * Init node as late as possible, to avoid corrupting
		 * the tree in case it is already added.
		 */
		node->left = node->right = NIL;
		node->level = 1;

		tree->count++;
		return node;
	}

	/* recursive insert */
	cmp = tree->node_cmp(value, current);
	if (cmp > 0)
		current->right = insert_sub(tree, current->right, value, node);
	else if (cmp < 0)
		current->left = insert_sub(tree, current->left, value, node);
	else
		/* already exists? */
		return current;

	return rebalance_on_insert(current);
}

void aatree_insert(Tree *tree, uintptr_t value, Node *node)
{
	tree->root = insert_sub(tree, tree->root, value, node);
}

/*
 * Recursive removal
 */

/* remove_sub could be used for that, but want to avoid comparisions */
static Node *steal_leftmost(Tree *tree, Node *current, Node **save_p)
{
	if (current->left == NIL) {
		*save_p = current;
		return current->right;
	}

	current->left = steal_leftmost(tree, current->left, save_p);
	return rebalance_on_remove(current);
}

/* drop this node from tree */
static Node *drop_this_node(Tree *tree, Node *old)
{
	Node *new = NIL;

	if (old->left == NIL)
		new = old->right;
	else if (old->right == NIL)
		new = old->left;
	else {
		/*
		 * Picking nearest from right is better than from left,
		 * due to asymmetry of the AA-tree.  It will result in
		 * less tree operations in the long run,
		 */
		old->right = steal_leftmost(tree, old->right, &new);

		/* take old node's place */
		*new = *old;
	}

	/* cleanup for old node */
	if (tree->release_cb)
		tree->release_cb(old, tree);
	tree->count--;

	return new;
}

static Node *remove_sub(Tree *tree, Node *current, uintptr_t value)
{
	int cmp;

	/* not found? */
	if (current == NIL)
		return current;

	cmp = tree->node_cmp(value, current);
	if (cmp > 0)
		current->right = remove_sub(tree, current->right, value);
	else if (cmp < 0)
		current->left = remove_sub(tree, current->left, value);
	else
		current = drop_this_node(tree, current);
 
	return rebalance_on_remove(current);
}

void aatree_remove(Tree *tree, uintptr_t value)
{
	tree->root = remove_sub(tree, tree->root, value);
}

/*
 * Walking all nodes
 */

static void walk_sub(Node *current, enum AATreeWalkType wtype,
		     aatree_walker_f walker, void *arg)
{
	if (current == NIL)
		return;

	switch (wtype) {
	case AA_WALK_IN_ORDER:
		walk_sub(current->left, wtype, walker, arg);
		walker(current, arg);
		walk_sub(current->right, wtype, walker, arg);
		break;
	case AA_WALK_POST_ORDER:
		walk_sub(current->left, wtype, walker, arg);
		walk_sub(current->right, wtype, walker, arg);
		walker(current, arg);
		break;
	case AA_WALK_PRE_ORDER:
		walker(current, arg);
		walk_sub(current->left, wtype, walker, arg);
		walk_sub(current->right, wtype, walker, arg);
		break;
	}
}

/* walk tree in correct order */
void aatree_walk(Tree *tree, enum AATreeWalkType wtype, aatree_walker_f walker, void *arg)
{
	walk_sub(tree->root, wtype, walker, arg);
}

/* walk tree in bottom-up order, so that walker can destroy the nodes */
void aatree_destroy(Tree *tree)
{
	walk_sub(tree->root, AA_WALK_POST_ORDER, tree->release_cb, tree);

	/* reset tree */
	tree->root = NIL;
	tree->count = 0;
}

/* prepare tree */
void aatree_init(Tree *tree, aatree_cmp_f cmpfn, aatree_walker_f release_cb)
{
	tree->root = NIL;
	tree->count = 0;
	tree->node_cmp = cmpfn;
	tree->release_cb = release_cb;
}

/*
 * search function
 */
Node *aatree_search(Tree *tree, uintptr_t value)
{
	Node *current = tree->root;
	while (current != NIL) {
		int cmp = tree->node_cmp(value, current);
		if (cmp > 0)
			current = current->right;
		else if (cmp < 0)
			current = current->left;
		else
			return current;
	}
	return NULL;
}