-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinary_search_tree.h
314 lines (281 loc) · 8.06 KB
/
binary_search_tree.h
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
#pragma once
#include <iterator>
namespace zzz
{
//incomplete
template<class interface_t>
class binary_search_tree
{
public:
typedef typename interface_t::key_t key_t;
typedef typename interface_t::node_t node_t;
typedef typename interface_t::value_node_t value_node_t;
protected:
binary_search_tree()
{
static_assert(std::is_base_of<node_t, value_node_t>::value, "node_t must be base of value_node_t");
set_nil_(nil_(), true);
set_root_(nil_());
set_most_left_(nil_());
set_most_right_(nil_());
}
protected:
node_t head_;
protected:
node_t *nil_()
{
return &head_;
}
node_t *get_root_()
{
return get_parent_(&head_);
}
void set_root_(node_t *root)
{
set_parent_(&head_, root);
}
node_t *get_most_left_()
{
return get_left_(&head_);
}
void set_most_left_(node_t *left)
{
set_left_(&head_, left);
}
node_t *get_most_right_()
{
return get_right_(&head_);
}
void set_most_right_(node_t *right)
{
set_right_(&head_, right);
}
static key_t const &get_key_(node_t *node)
{
return interface_t::get_key(static_cast<value_node_t *>(node));
}
static void set_nil_(node_t *node, bool nil)
{
return interface_t::set_nil(node, nil);
}
static bool is_nil_(node_t *node)
{
return interface_t::is_nil(node);
}
static node_t *get_parent_(node_t *node)
{
return interface_t::get_parent(node);
}
static void set_parent_(node_t *node, node_t *parent)
{
interface_t::set_parent(node, parent);
}
static node_t *get_left_(node_t *node)
{
return interface_t::get_left(node);
}
static void set_left_(node_t *node, node_t *left)
{
interface_t::set_left(node, left);
}
static node_t *get_right_(node_t *node)
{
return interface_t::get_right(node);
}
static void set_right_(node_t *node, node_t *right)
{
interface_t::set_right(node, right);
}
template<bool is_left>
static void set_child_(node_t *node, node_t *child)
{
if(is_left)
{
set_left_(node, child);
}
else
{
set_right_(node, child);
}
}
template<bool is_left>
static node_t *get_child_(node_t *node)
{
if(is_left)
{
return get_left_(node);
}
else
{
return get_right_(node);
}
}
static bool predicate(node_t *left, node_t *right)
{
return interface_t::predicate(get_key_(left), get_key_(right));
}
void bst_clear_()
{
set_root_(nil_());
set_most_left_(nil_());
set_most_right_(nil_());
}
template<class F>
static void bst_safe_destroy_(node_t *node, F const &callback)
{
if (!is_nil_(get_left_(node)))
{
bst_safe_destroy_(get_left_(node), callback);
}
if (!is_nil_(get_right_(node)))
{
bst_safe_destroy_(get_right_(node), callback);
}
callback(node);
}
node_t *bst_init_node_(node_t *parent, node_t *node)
{
set_nil_(node, false);
set_parent_(node, parent);
set_left_(node, nil_());
set_right_(node, nil_());
return node;
}
template<bool is_next>
static node_t *bst_move_(node_t *node)
{
if(!is_nil_(node))
{
if(!is_nil_(get_child_<!is_next>(node)))
{
node = get_child_<!is_next>(node);
while(!is_nil_(get_child_<is_next>(node)))
{
node = get_child_<is_next>(node);
}
}
else
{
node_t *parent;
while(!is_nil_(parent = get_parent_(node)) && node == get_child_<!is_next>(parent))
{
node = parent;
}
node = parent;
}
}
else
{
return get_child_<is_next>(node);
}
return node;
}
template<bool is_min>
static node_t *bst_most_(node_t *node)
{
while(!is_nil_(get_child_<is_min>(node)))
{
node = get_child_<is_min>(node);
}
return node;
}
node_t *bst_lower_bound_(key_t const &key)
{
node_t *node = get_root_(), *where = nil_();
while(!is_nil_(node))
{
if(interface_t::predicate(get_key_(node), key))
{
node = get_right_(node);
}
else
{
where = node;
node = get_left_(node);
}
}
return where;
}
node_t *bst_upper_bound_(key_t const &key)
{
node_t *node = get_root_(), *where = nil_();
while(!is_nil_(node))
{
if(interface_t::predicate(key, get_key_(node)))
{
where = node;
node = get_left_(node);
}
else
{
node = get_right_(node);
}
}
return where;
}
void bst_equal_range_(key_t const &key, node_t *&lower_node, node_t *&upper_node)
{
node_t *node = get_root_();
node_t *lower = nil_();
node_t *upper = nil_();
while(!is_nil_(node))
{
if(interface_t::predicate(get_key_(node), key))
{
node = get_right_(node);
}
else
{
if(is_nil_(upper) && interface_t::predicate(key, get_key_(node)))
{
upper = node;
}
lower = node;
node = get_left_(node);
}
}
node = is_nil_(upper) ? get_root_() : get_left_(upper);
while(!is_nil_(node))
{
if(interface_t::predicate(key, get_key_(node)))
{
upper = node;
node = get_left_(node);
}
else
{
node = get_right_(node);
}
}
lower_node = lower;
upper_node = upper;
}
template<bool is_left>
node_t *bst_rotate_(node_t *node)
{
node_t *child = get_child_<!is_left>(node), *parent = get_parent_(node);
set_child_<!is_left>(node, get_child_<is_left>(child));
if(!is_nil_(get_child_<is_left>(child)))
{
set_parent_(get_child_<is_left>(child), node);
}
set_parent_(child, parent);
if(node == get_root_())
{
set_root_(child);
}
else if(node == get_child_<is_left>(parent))
{
set_child_<is_left>(parent, child);
}
else
{
set_child_<!is_left>(parent, child);
}
set_child_<is_left>(child, node);
set_parent_(node, child);
return child;
}
};
}