-
Notifications
You must be signed in to change notification settings - Fork 0
/
avl_tree.c
159 lines (141 loc) · 3.53 KB
/
avl_tree.c
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
#include "avl_tree.h"
#include <stddef.h>
#include "allocator.h"
typedef struct node
{
void * key;
void * value;
unsigned char height;
struct node * left;
struct node * right;
}node_t;
static node_t * node_create(void * key, void * value) {
node_t * ret = malloc(sizeof(node_t));
ret->key = key;
ret->value = value;
ret->left = ret->right = NULL;
ret->height = 1;
return ret;
}
static void node_destroy(node_t * node) {
free(node);
}
static unsigned char height(node_t * p)
{
return p ? p->height : 0;
}
static int bfactor(node_t* p)
{
return height(p->right)-height(p->left);
}
static void fix_height(node_t * p)
{
unsigned char hl = height(p->left);
unsigned char hr = height(p->right);
p->height = (hl>hr?hl:hr)+1;
}
static node_t * rotate_right(node_t * p)
{
node_t * q = p->left;
p->left = q->right;
q->right = p;
fix_height(p);
fix_height(q);
return q;
}
static node_t * rotate_left(node_t * q)
{
node_t * p = q->right;
q->right = p->left;
p->left = q;
fix_height(q);
fix_height(p);
return p;
}
static node_t * balance(node_t * p)
{
fix_height(p);
if( bfactor(p)==2 )
{
if( bfactor(p->right) < 0 )
p->right = rotate_right(p->right);
return rotate_left(p);
}
if( bfactor(p)==-2 )
{
if( bfactor(p->left) > 0 )
p->left = rotate_left(p->left);
return rotate_right(p);
}
return p;
}
static node_t * insert(node_t * p, node_t * new_node, int (*comparator)(void *, void *)) // insert k key in a tree with p root
{
if( !p )
return new_node;
if( comparator(new_node->key, p->key) < 0 )
p->left = insert(p->left, new_node, comparator);
else
p->right = insert(p->right, new_node, comparator);
return balance(p);
}
static node_t * findmin(node_t * p) // find a node with minimal key in a p tree
{
return p->left?findmin(p->left):p;
}
static node_t * removemin(node_t * p) // deleting a node with minimal key from a p tree
{
if( p->left==NULL )
return p->right;
p->left = removemin(p->left);
return balance(p);
}
static node_t * remove(node_t * p, void * k, int (*comparator)(void *, void *)) // deleting k key from p tree
{
if( !p ) return 0;
if( comparator(k, p->key) < 0 )
p->left = remove(p->left, k, comparator);
else if( comparator(k, p->key) > 0 )
p->right = remove(p->right, k, comparator);
else // k == p->key
{
node_t * q = p->left;
node_t * r = p->right;
node_destroy(p);
if( !r ) return q;
node_t * min = findmin(r);
min->right = removemin(r);
min->left = q;
return balance(min);
}
return balance(p);
}
static node_t * find(node_t * root, void * key, int (*comparator)(void *, void *)) {
while(1) {
if( comparator(key, root->key) < 0) {
root = root->left;
continue;
}
if( comparator(key, root->key) > 0) {
root = root->right;
continue;
}
break;
}
return root;
}
avl_tree_t * avl_tree_create(int (*comparator)(void * left, void *right)){
avl_tree_t * tree = malloc(sizeof(avl_tree_t));
tree->comparator = comparator;
tree->root = NULL;
return tree;
}
void avl_tree_insert(avl_tree_t * tree, void * key, void * value) {
tree->root = insert(tree->root, node_create(key, value), tree->comparator);
}
void * avl_tree_find(avl_tree_t * tree, void * key) {
return find(tree->root, key, tree->comparator)->value;
}
void avl_tree_remove(avl_tree_t * tree, void * key){
remove(tree->root, key, tree->comparator);
}