-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbst.hpp
More file actions
238 lines (194 loc) · 5.88 KB
/
bst.hpp
File metadata and controls
238 lines (194 loc) · 5.88 KB
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
#ifndef BST_HPP
# define BST_HPP
#include "node.hpp"
#include "bst_iter.hpp"
#include "pair.hpp"
namespace ft
{
template <class T, class Compare = std::less<T>, class Node = ft::BST_Node<T>,
class Type_Alloc = std::allocator<T>, class Node_Alloc = std::allocator<Node> >
class Binary_search_tree
{
public :
typedef Binary_search_tree self;
typedef self& self_reference;
typedef T value_type;
typedef Node node_type;
typedef Node * node_pointer;
typedef Node_Alloc node_alloc;
typedef ft::BST_iterator<Node, Compare> iterator;
typedef ft::BST_const_iterator<Node, Compare> const_iterator;
typedef size_t size_type;
Binary_search_tree (const node_alloc& node_alloc_init = node_alloc())
:
_node_alloc(node_alloc_init)
{
_last_node = _node_alloc.allocate(1);
_node_alloc.construct(_last_node, Node(_last_node, _last_node, _last_node));
}
~Binary_search_tree ()
{
_node_alloc.destroy(_last_node);
_node_alloc.deallocate(_last_node, 1);
}
ft::pair<iterator, bool> insertPair(value_type to_insert)
{
Node * new_node = _node_alloc.allocate(1);
Node * prev_node = _last_node;
Node * start_node = _last_node->parent;
bool side = true;
while (start_node != _last_node)
{
int curkey = start_node->value.first;
if (curkey == to_insert.first)
return (ft::make_pair(iterator(start_node, _last_node), false));
prev_node = start_node;
if (to_insert.first > curkey)
{
side = true;
start_node = start_node->right;
}
else
{
side = false;
start_node = start_node->left;
}
}
_node_alloc.construct(new_node, Node(to_insert, prev_node, _last_node, _last_node));
if (prev_node == _last_node)
_last_node->parent = new_node;
else if (side == true)
prev_node->right = new_node;
else
prev_node->left = new_node;
_last_node->left = _BST_get_lower_node(_last_node->parent);
_last_node->right = _BST_get_higher_node(_last_node->parent);
_last_node->value.first += 1;
return (ft::make_pair(iterator(new_node, _last_node), true));
}
void removeByKey(value_type to_remove)
{ _removeByKey(_last_node->parent, to_remove); }
node_pointer searchByKey(value_type to_remove)
{
node_pointer node = _last_node->parent;
while (node != _last_node)
{
if (node->value.first == to_remove.first)
return (node);
if (node->value.first > to_remove.first)
node = node->left;
else
node = node->right;
}
return (node);
}
void swap(self& x)
{
if (&x == this)
return ;
node_pointer save = this->_last_node;
this->_last_node = x._last_node;
x._last_node = save;
}
size_type max_size() const
{ return (node_alloc().max_size()); }
node_pointer _last_node;
node_alloc _node_alloc;
private :
node_pointer _BST_get_lower_node(node_pointer root)
{
while (root != _last_node && root->left != _last_node)
root = root->left;
return (root);
}
node_pointer _BST_get_higher_node(node_pointer root)
{
while (root != _last_node && root->right != _last_node)
root = root->right;
return (root);
}
void _replaceNodeInParent(node_pointer node, node_pointer new_node)
{
if (node->parent != _last_node)
{
if (_last_node->parent == node)
_last_node->parent = new_node;
if (node == node->parent->left)
node->parent->left = new_node;
else
node->parent->right = new_node;
}
else
_last_node->parent = new_node;
_last_node->left = _BST_get_lower_node(_last_node->parent);
_last_node->right = _BST_get_higher_node(_last_node->parent);
_last_node->value.first -= 1;
new_node->parent = node->parent;
_node_alloc.destroy(node);
_node_alloc.deallocate(node, 1);
}
void _replaceDoubleChildren(node_pointer& to_remove, node_pointer new_node)
{
if (new_node->parent != _last_node)
{
if (new_node->parent != to_remove)
new_node->parent->left = new_node->right;
}
new_node->parent = to_remove->parent;
if (to_remove->left != new_node)
new_node->left = to_remove->left;
if (to_remove->right != new_node)
new_node->right = to_remove->right;
if (to_remove->parent != _last_node)
{
if (to_remove->parent->left == to_remove)
to_remove->parent->left = new_node;
else if (to_remove->parent->right == to_remove)
to_remove->parent->right = new_node;
}
else
_last_node->parent = new_node;
if (to_remove->left != _last_node && to_remove->left != new_node)
to_remove->left->parent = new_node;
if (to_remove->right != _last_node && to_remove->right != new_node)
to_remove->right->parent = new_node;
if (to_remove->parent != _last_node)
{
to_remove->left = _last_node;
to_remove->right = _last_node;
to_remove->parent = new_node;
}
_last_node->left = _BST_get_lower_node(_last_node->parent);
_last_node->right = _BST_get_higher_node(_last_node->parent);
_last_node->value.first -= 1;
_node_alloc.destroy(to_remove);
_node_alloc.deallocate(to_remove, 1);
}
void _removeByKey(node_pointer node, value_type to_remove)
{
if (to_remove.first < node->value.first)
{
_removeByKey(node->left, to_remove);
return ;
}
if (to_remove.first > node->value.first)
{
_removeByKey(node->right, to_remove);
return ;
}
if (node->left != _last_node && node->right != _last_node)
{
node_pointer successor = _BST_get_lower_node(node->right);
_replaceDoubleChildren(node, successor);
return ;
}
else if (node->left != _last_node)
_replaceNodeInParent(node, node->left);
else if (node->right != _last_node)
_replaceNodeInParent(node, node->right);
else
_replaceNodeInParent(node, _last_node);
}
};
}
#endif