summaryrefslogtreecommitdiffstats
path: root/src/avl.c
blob: fd4fb1420637e0668597cbe94803f48b0b4622da (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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
/*
 * ANSI C Library for maintainance of AVL Balanced Trees
 *
 * ref.:
 *  G. M. Adelson-Velskij & E. M. Landis
 *  Doklady Akad. Nauk SSSR 146 (1962), 263-266
 *
 * see also:
 *  D. E. Knuth: The Art of Computer Programming Vol.3 (Sorting and Searching)
 *
 * (C) 2000 Daniel Nagy, Budapest University of Technology and Economics
 * Released under GNU General Public License (GPL) version 2
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "avl.h"
#include "log.h"

/* Private methods */
int _avl_removeroot(avl_tree* t);

/* Swing to the left
 * Warning: no balance maintainance
 */
void avl_swl(avl** root) {
	avl* a = *root;
	avl* b = a->right;
	*root = b;
	a->right = b->left;
	b->left = a;
}

/* Swing to the right
 * Warning: no balance maintainance
 */
void avl_swr(avl** root) {
	avl* a = *root;
	avl* b = a->left;
	*root = b;
	a->left = b->right;
	b->right = a;
}

/* Balance maintainance after especially nasty swings
 */
void avl_nasty(avl* root) {
	switch (root->balance) {
	case -1:
		root->left->balance = 0;
		root->right->balance = 1;
		break;
	case 1:
		root->left->balance = -1;
		root->right->balance = 0;
		break;
	case 0:
		root->left->balance = 0;
		root->right->balance = 0;
	}
	root->balance = 0;
}

/* Public methods */

/* Insert element a into the AVL tree t
 * returns 1 if the depth of the tree has grown
 * Warning: do not insert elements already present
 */
int _avl_insert(avl_tree* t, avl* a) {
	/* initialize */
	a->left = 0;
	a->right = 0;
	a->balance = 0;
	/* insert into an empty tree */
	if (!t->root) {
		t->root = a;
		return 1;
	}

	if (t->compar(t->root, a) > 0) {
		/* insert into the left subtree */
		if (t->root->left) {
			avl_tree left_subtree;
			left_subtree.root = t->root->left;
			left_subtree.compar = t->compar;
			if (_avl_insert(&left_subtree, a)) {
				switch (t->root->balance--) {
				case 1:
					return 0;
				case 0:
					return 1;
				}
				if (t->root->left->balance < 0) {
					avl_swr(&(t->root));
					t->root->balance = 0;
					t->root->right->balance = 0;
				} else {
					avl_swl(&(t->root->left));
					avl_swr(&(t->root));
					avl_nasty(t->root);
				}
			} else
				t->root->left = left_subtree.root;
			return 0;
		} else {
			t->root->left = a;
			if (t->root->balance--)
				return 0;
			return 1;
		}
	} else {
		/* insert into the right subtree */
		if (t->root->right) {
			avl_tree right_subtree;
			right_subtree.root = t->root->right;
			right_subtree.compar = t->compar;
			if (_avl_insert(&right_subtree, a)) {
				switch (t->root->balance++) {
				case -1:
					return 0;
				case 0:
					return 1;
				}
				if (t->root->right->balance > 0) {
					avl_swl(&(t->root));
					t->root->balance = 0;
					t->root->left->balance = 0;
				} else {
					avl_swr(&(t->root->right));
					avl_swl(&(t->root));
					avl_nasty(t->root);
				}
			} else
				t->root->right = right_subtree.root;
			return 0;
		} else {
			t->root->right = a;
			if (t->root->balance++)
				return 0;
			return 1;
		}
	}
}
int avl_insert(avl_tree* t, avl* a) {
#ifndef AVL_WITHOUT_PTHREADS
#ifdef AVL_LOCK_WITH_MUTEX
	pthread_mutex_lock(&t->mutex);
#else
	pthread_rwlock_wrlock(&t->rwlock);
#endif
#endif /* AVL_WITHOUT_PTHREADS */

	int ret = _avl_insert(t, a);

#ifndef AVL_WITHOUT_PTHREADS
#ifdef AVL_LOCK_WITH_MUTEX
	pthread_mutex_unlock(&t->mutex);
#else
	pthread_rwlock_unlock(&t->rwlock);
#endif
#endif /* AVL_WITHOUT_PTHREADS */
	return ret;
}

/* Remove an element a from the AVL tree t
 * returns -1 if the depth of the tree has shrunk
 * Warning: if the element is not present in the tree,
 *          returns 0 as if it had been removed succesfully.
 */
int _avl_remove(avl_tree* t, avl* a) {
	int b;
	if (t->root == a)
		return _avl_removeroot(t);
	b = t->compar(t->root, a);
	if (b >= 0) {
		/* remove from the left subtree */
		int ch;
		avl_tree left_subtree;
		if ((left_subtree.root = t->root->left)) {
			left_subtree.compar = t->compar;
			ch = _avl_remove(&left_subtree, a);
			t->root->left = left_subtree.root;
			if (ch) {
				switch (t->root->balance++) {
				case -1:
					return -1;
				case 0:
					return 0;
				}
				switch (t->root->right->balance) {
				case 0:
					avl_swl(&(t->root));
					t->root->balance = -1;
					t->root->left->balance = 1;
					return 0;
				case 1:
					avl_swl(&(t->root));
					t->root->balance = 0;
					t->root->left->balance = 0;
					return -1;
				}
				avl_swr(&(t->root->right));
				avl_swl(&(t->root));
				avl_nasty(t->root);
				return -1;
			}
		}
	}
	if (b <= 0) {
		/* remove from the right subtree */
		int ch;
		avl_tree right_subtree;
		if ((right_subtree.root = t->root->right)) {
			right_subtree.compar = t->compar;
			ch = _avl_remove(&right_subtree, a);
			t->root->right = right_subtree.root;
			if (ch) {
				switch (t->root->balance--) {
				case 1:
					return -1;
				case 0:
					return 0;
				}
				switch (t->root->left->balance) {
				case 0:
					avl_swr(&(t->root));
					t->root->balance = 1;
					t->root->right->balance = -1;
					return 0;
				case -1:
					avl_swr(&(t->root));
					t->root->balance = 0;
					t->root->right->balance = 0;
					return -1;
				}
				avl_swl(&(t->root->left));
				avl_swr(&(t->root));
				avl_nasty(t->root);
				return -1;
			}
		}
	}
	return 0;
}

int avl_remove(avl_tree* t, avl* a) {
#ifndef AVL_WITHOUT_PTHREADS
#ifdef AVL_LOCK_WITH_MUTEX
	pthread_mutex_lock(&t->mutex);
#else
	pthread_rwlock_wrlock(&t->rwlock);
#endif
#endif /* AVL_WITHOUT_PTHREADS */

	int ret = _avl_remove(t, a);

#ifndef AVL_WITHOUT_PTHREADS
#ifdef AVL_LOCK_WITH_MUTEX
	pthread_mutex_unlock(&t->mutex);
#else
	pthread_rwlock_unlock(&t->rwlock);
#endif
#endif /* AVL_WITHOUT_PTHREADS */
	return ret;
}

/* Remove the root of the AVL tree t
 * Warning: dumps core if t is empty
 */
int _avl_removeroot(avl_tree* t) {
	int ch;
	avl* a;
	if (!t->root->left) {
		if (!t->root->right) {
			t->root = 0;
			return -1;
		}
		t->root = t->root->right;
		return -1;
	}
	if (!t->root->right) {
		t->root = t->root->left;
		return -1;
	}
	if (t->root->balance < 0) {
		/* remove from the left subtree */
		a = t->root->left;
		while (a->right)
			a = a->right;
	} else {
		/* remove from the right subtree */
		a = t->root->right;
		while (a->left)
			a = a->left;
	}
	ch = _avl_remove(t, a);
	a->left = t->root->left;
	a->right = t->root->right;
	a->balance = t->root->balance;
	t->root = a;
	if (a->balance == 0)
		return ch;
	return 0;
}

int avl_removeroot(avl_tree* t) {
#ifndef AVL_WITHOUT_PTHREADS
#ifdef AVL_LOCK_WITH_MUTEX
	pthread_mutex_lock(&t->mutex);
#else
	pthread_rwlock_wrlock(&t->rwlock);
#endif
#endif /* AVL_WITHOUT_PTHREADS */

	int ret = _avl_removeroot(t);

#ifndef AVL_WITHOUT_PTHREADS
#ifdef AVL_LOCK_WITH_MUTEX
	pthread_mutex_unlock(&t->mutex);
#else
	pthread_rwlock_unlock(&t->rwlock);
#endif
#endif /* AVL_WITHOUT_PTHREADS */
	return ret;
}

/* Iterate through elements in t from a range between a and b (inclusive)
 * for each element calls iter(a) until it returns 0
 * returns the last value returned by iterator or 0 if there were no calls
 * Warning: a<=b must hold
 */
int _avl_range(avl_tree* t, avl* a, avl* b, int (*iter)(avl*), avl** ret) {
	int x, c = 0;
	if (!t->root)
		return 0;
	x = t->compar(t->root, a);
	if (a != b) {
		if (x < 0) {
			x = t->compar(t->root, b);
			if (x > 0)
				x = 0;
		}
	}
	if (x >= 0) {
		/* search in the left subtree */
		avl_tree left_subtree;
		if ((left_subtree.root = t->root->left)) {
			left_subtree.compar = t->compar;
			if (!(c = _avl_range(&left_subtree, a, b, iter, ret)))
				if (x > 0)
					return 0;
		}
	}
	if (x == 0) {
		if (!(c = iter(t->root))) {
			if (ret)
				*ret = t->root;
			return 0;
		}
	}
	if (x <= 0) {
		/* search in the right subtree */
		avl_tree right_subtree;
		if ((right_subtree.root = t->root->right)) {
			right_subtree.compar = t->compar;
			if (!(c = _avl_range(&right_subtree, a, b, iter, ret)))
				if (x < 0)
					return 0;
		}
	}
	return c;
}

int avl_range(avl_tree* t, avl* a, avl* b, int (*iter)(avl*), avl** ret) {
#ifndef AVL_WITHOUT_PTHREADS
#ifdef AVL_LOCK_WITH_MUTEX
	pthread_mutex_lock(&t->mutex);
#else
	pthread_rwlock_wrlock(&t->rwlock);
#endif
#endif /* AVL_WITHOUT_PTHREADS */

	int ret2 = _avl_range(t, a, b, iter, ret);

#ifndef AVL_WITHOUT_PTHREADS
#ifdef AVL_LOCK_WITH_MUTEX
	pthread_mutex_unlock(&t->mutex);
#else
	pthread_rwlock_unlock(&t->rwlock);
#endif
#endif /* AVL_WITHOUT_PTHREADS */

	return ret2;
}

/* Iterate through elements in t equal to a
 * for each element calls iter(a) until it returns 0
 * returns the last value returned by iterator or 0 if there were no calls
 */
int avl_search(avl_tree* t, avl* a, int (*iter)(avl* a), avl** ret) {
	return avl_range(t, a, a, iter, ret);
}

void avl_init(avl_tree* t, int (*compar)(void* a, void* b)) {
	t->root = NULL;
	t->compar = compar;

#ifndef AVL_WITHOUT_PTHREADS
	int lock;

#ifdef AVL_LOCK_WITH_MUTEX
	lock = pthread_mutex_init(&t->mutex, NULL);
#else
	lock = pthread_rwlock_init(&t->rwlock, NULL);
#endif

	if(lock != 0)
		fatal("Failed to initialize AVL mutex/rwlock, error: %d", lock);

#endif /* AVL_WITHOUT_PTHREADS */

}