-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmystl_bst.hpp
403 lines (333 loc) · 10.2 KB
/
mystl_bst.hpp
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
#ifndef _MYSTL_BST_H
#define _MYSTL_BST_H
#include "mystl_algobase.hpp"
#include "mystl_iterator_base.hpp"
#include "mystl_pair.hpp"
namespace numb {
template <typename _Val>
struct _bst_node {
typedef _bst_node* _Link_type;
typedef const _bst_node* _Const_Link_type;
_Link_type _parent;
_Link_type _left;
_Link_type _right;
_Val _value_field;
_bst_node() {}
explicit _bst_node(_Val _x, _Link_type _p = NULL, _Link_type _l = NULL,
_Link_type _r = NULL)
: _value_field(_x), _parent(_p), _left(_l), _right(_r) {}
};
template <typename _Tp>
struct _BST_iterator {
typedef _Tp value_type;
typedef _Tp& reference;
typedef _Tp* pointer;
typedef Bidirectional_iterator_tag iterator_category;
typedef ptrdiff_t difference_type;
typedef _BST_iterator<_Tp> _Self;
typedef _bst_node<_Tp>* _Link_type;
_Link_type _node;
_BST_iterator() : _node() {}
explicit _BST_iterator(_Link_type _x) : _node(_x) {}
reference operator*() const { return _node->_value_field; }
pointer operator->() const { return __Addressof(_node->_value_field); }
_Self& operator++() {
_node = _bst_increment(_node);
return *this;
}
_Self operator++(int) {
_Self _tmp = *this;
_node = _bst_increment(_node);
return _tmp;
}
_Self& operator--() {
_node = _bst_decrement(_node);
return *this;
}
_Self operator--(int) {
_Self _tmp = *this;
_node = _bst_decrement(_node);
return _tmp;
}
bool operator==(const _Self& _x) const { return _node == _x._node; }
bool operator!=(const _Self& _x) const { return _node != _x._node; }
_Link_type _bst_increment(_Link_type _x) throw() {
if (_x->_right != NULL) {
_x = _x->_right;
while (_x->_left != NULL) _x = _x->_left;
return _x;
} else {
_Link_type _y = _x->_parent;
while (_y != NULL && _x == _y->_right) {
_x = _y;
_y = _y->_parent;
}
return _y;
}
}
_Link_type _bst_decrement(_Link_type _x) throw() {
if (_x->_left != NULL) {
_x = _x->_left;
while (_x->_right != NULL) _x = _x->_right;
return _x;
} else {
_Link_type _y = _x->_parent;
while (_y != NULL && _x == _y->_left) {
_x = _y;
_y = _y->_parent;
}
return _y;
}
}
};
template <typename _Tp>
struct _BST_const_iterator {
typedef _Tp value_type;
typedef _Tp& reference;
typedef _Tp* pointer;
typedef _BST_iterator<_Tp>* BIterator;
typedef Bidirectional_iterator_tag iterator_category;
typedef ptrdiff_t difference_type;
typedef _BST_const_iterator<_Tp> _Self;
typedef _bst_node<_Tp>* _Link_type;
_Link_type _node;
_BST_const_iterator() : _node() {}
explicit _BST_const_iterator(_Link_type _x) : _node(_x) {}
//explicit _BST_const_iterator(const BIterator& _it) : _node(_it._node) {}
//BIterator _const_cast() const
//{ return BIterator(const_cast<typename BIterator::_Link_type>(_node)); }
reference operator*() const { return _node->_value_field; }
pointer operator->() const { return __Addressof(_node->_value_field); }
_Self& operator++() {
_node = _bst_increment(_node);
return *this;
}
_Self operator++(int) {
_Self _tmp = *this;
_node = _bst_increment(_node);
return _tmp;
}
_Self& operator--() {
_node = _bst_decrement(_node);
return *this;
}
_Self operator--(int) {
_Self _tmp = *this;
_node = _bst_decrement(_node);
return _tmp;
}
bool operator==(const _Self& _x) const { return _node == _x._node; }
bool operator!=(const _Self& _x) const { return _node != _x._node; }
_Link_type _bst_increment(_Link_type _x) throw() {
if (_x->_right != NULL) {
_x = _x->_right;
while (_x->_left != NULL) _x = _x->_left;
return _x;
} else {
_Link_type _y = _x->_parent;
while (_y != NULL && _x == _y->_right) {
_x = _y;
_y = _y->_parent;
}
return _y;
}
}
_Link_type _bst_decrement(_Link_type _x) throw() {
if (_x->_left != NULL) {
_x = _x->_left;
while (_x->_right != NULL) _x = _x->_right;
return _x;
} else {
_Link_type _y = _x->_parent;
while (_y != NULL && _x == _y->_left) {
_x = _y;
_y = _y->_parent;
}
return _y;
}
}
};
template <typename _Val>
inline bool operator==(const _BST_iterator<_Val>& _x,
const _BST_iterator<_Val>& _y) {
return _x._node == _y._node;
}
template <typename _Val>
inline bool operator!=(const _BST_iterator<_Val>& _x,
const _BST_iterator<_Val>& _y) {
return _x._node != _y._node;
}
template <typename _Key, typename _Val, typename _KeyOfValue, typename _Compare>
class _BST {
public:
typedef _Key key_type;
typedef _Val value_type;
typedef value_type* pointer;
typedef const value_type* const_pointer;
typedef value_type& reference;
typedef const value_type& const_reference;
typedef _bst_node<_Val>* _Link_type;
typedef const _bst_node<_Val>* _Const_Link_type;
typedef size_t size_type;
typedef ptrdiff_t difference_type;
typedef _BST_iterator<value_type> Iterator;
typedef _BST_const_iterator<value_type> Const_iterator;
private:
size_type _node_count;
_Link_type _root;
_Compare key_comp;
protected:
static const _Key& _S_key(_Const_Link_type _x) {
return _KeyOfValue()(_S_value(_x));
}
static const_reference _S_value(_Const_Link_type _x) {
return _x->_value_field;
}
_Link_type _find(const key_type& _x)
{
_Link_type _p = _root;
while (_p != NULL && _KeyOfValue()(_x) != _S_key(_p)) {
if (key_comp(_KeyOfValue()(_x), _S_key(_p)))
_p = _p->_left;
else
_p = _p->_right;
}
return _p;
}
_Link_type _succ(_Link_type _x) {
if (_x->_right != NULL) {
_x = _x->_right;
while (_x->_left != NULL) _x = _x->_left;
return _x;
} else {
_Link_type _y = _x->_parent;
while (_y != NULL && _x == _y->_right) {
_x = _y;
_y = _y->_parent;
}
return _y;
}
}
void _destroy(_Link_type _r) {
if (_r) {
_destroy(_r->_left);
_destroy(_r->_right);
delete _r;
}
_r = NULL;
}
public:
_BST()
: _node_count(0),
_root(NULL),
key_comp(_Compare()) {}
~_BST() { _destroy(_root); }
_BST(const _BST& _rhs)
{
_node_count = 0;
_root = NULL;
key_comp(_rhs.key_comp());
for (Iterator _it = _rhs.begin(); _it != _rhs.end; ++_it)
_insert_unique(*_it);
}
_BST& operator=(const _BST& _rhs)
{
_BST _copy = _rhs;
swap(_copy);
return *this;
}
void swap(_BST& _rhs) {
using numb::Swap;
Swap(_node_count, _rhs._node_count);
Swap(_root, _rhs._root);
Swap(key_comp, _rhs.key_comp);
}
template <typename _InputIterator>
_BST(_InputIterator _first, _InputIterator _last)
{
_node_count = 0;
_root = NULL;
for (; _first != _last; ++_first)
_insert_unique(*_first);
}
Iterator begin() throw() {
_Link_type _leftmost = _root;
while (_leftmost->_left != NULL)
_leftmost = _leftmost->_left;
return Iterator(_leftmost);
}
Iterator end() throw() { return Iterator(NULL); }
Const_iterator begin() const throw() {
_Link_type _leftmost = _root;
while (_leftmost->_left != NULL)
_leftmost = _leftmost->_left;
return Const_iterator(_leftmost);
}
Const_iterator end() const throw() { return Const_iterator(NULL); }
size_type size() const { return _node_count; }
bool empty() const { return _node_count == 0; }
Pair<Iterator, bool> _insert_unique(const value_type& _x) {
_Link_type _hot = NULL;
_Link_type _p = _root;
while (_p != NULL) {
_hot = _p;
if (key_comp(_KeyOfValue()(_x), _S_key(_p)))
_p = _p->_left;
else if (key_comp(_S_key(_p), _KeyOfValue()(_x)))
_p = _p->_right;
else
return Make_pair(Iterator(NULL), false);
}
_Link_type _cur = new _bst_node<value_type>(_x, _hot);
if (_hot == NULL)
_root = _cur;
else {
if (key_comp(_KeyOfValue()(_x), _S_key(_hot)))
_hot->_left = _cur;
else
_hot->_right = _cur;
}
Iterator _iter(_cur);
++_node_count;
return Make_pair(_iter, true);
}
size_type erase(const key_type& _x)
{
_Link_type _del = _find(_x);
if (_del == NULL)
throw;
_Link_type _real;
if (_del->_left == NULL || _del->_right == NULL)
_real = _del;
else
_real = _succ(_del);
_Link_type _child;
if (_real->_left != NULL)
_child = _real->_left;
else
_child = _real->_right;
if (_child != NULL)
_child->_parent = _real->_parent;
if (_real->_parent == NULL)
_root = _child;
else {
if (_real == _real->_parent->_left)
_real->_parent->_left = _child;
else
_real->_parent->_right = _child;
}
if (_real != _del)
_del->_value_field = _real->_value_field;
value_type _tmp = _S_value(_del);
delete _real;
--_node_count;
return _tmp;
}
Iterator find(const key_type& _x)
{
return Iterator(_find(_x));
}
Iterator _insert_equal(const value_type& _x);
};
}
#endif