-
Notifications
You must be signed in to change notification settings - Fork 3
/
BNode.cc
344 lines (303 loc) · 6.67 KB
/
BNode.cc
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
//
// Copyright 2009-2017 M. Shulhan (ms@kilabit.info). All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
#include "BNode.hh"
namespace vos {
const char* BNode::__cname = "BNode";
/**
* `BNode()` will create new binary node that contain `item`.
* Note that you must pass an object that allocated with `new` not passed as
* `&item`, because it will be deleted by this object not automatically by
* system.
*/
BNode::BNode(Object* item) : Object()
, _left(NULL)
, _right(NULL)
, _item(item)
{}
/**
* `~BNode()` will release node memory. It is the job of BNode implementer to
* free left and right node (see List).
*/
BNode::~BNode()
{
_left = NULL;
_right = NULL;
if (_item) {
delete _item;
_item = NULL;
}
}
/**
* `delete_content()` will delete the content of node and set it to NULL.
*/
void BNode::delete_content()
{
delete _item;
_item = NULL;
}
/**
* `replace_content()` will replace content of current node with content in
* `node`. Old content will be deleted and content of `node` will be removed.
*/
void BNode::replace_content(BNode* node)
{
delete_content();
set_content(node->get_content());
node->set_content(NULL);
}
/**
* `set_content()` will set node content to `item`.
*/
void BNode::set_content(Object* item)
{
_item = item;
}
/**
* `get_content()` will return the content of node.
*/
Object* BNode::get_content()
{
return _item;
}
/**
* `swap_content()` will swap the content of node `x` with `y`.
*/
void BNode::swap_content(BNode* x)
{
Object* x_item = x->get_content();
x->set_content(get_content());
set_content(x_item);
}
/**
* `set_left()` will set the left child to `node`.
*/
void BNode::set_left(BNode* node)
{
_left = node;
}
/**
* `get_left()` will return the left node.
*/
BNode* BNode::get_left()
{
return _left;
}
/**
* `get_left_edge()` will return the outer left node of this node, or this
* node if left is NULL.
*/
BNode* BNode::get_left_edge()
{
BNode* p = this;
while (p->_left != NULL) {
p = p->_left;
}
return p;
}
/**
* `set_right()` will set the right child to `node`.
*/
void BNode::set_right(BNode* node)
{
_right = node;
}
/**
* `get_right()` will return the right node.
*/
BNode* BNode::get_right()
{
return _right;
}
/**
* `get_right_edge()` will return the outer right node of this node, or this
* node if right is NULL.
*/
BNode* BNode::get_right_edge()
{
BNode* p = this;
while (p->_right != NULL) {
p = p->_right;
}
return p;
}
/**
* `is_left_of()` will return `1` if current node is left child
* of `node`.
*/
int BNode::is_left_of(BNode* node)
{
if (node->get_left() == this) {
return 1;
}
return 0;
}
/**
* `is_right_of()` will return `1` if current node is right child of
* `node`.
*/
int BNode::is_right_of(BNode* node)
{
if (node->get_right() == this) {
return 1;
}
return 0;
}
/**
* `push_left()` will add new `node` to the outer left of this node.
*
* - If this node left pointer is not NULL then the new `node` will be
* inserted at the edge of left node.
*
* - If node right is not NULL then the left edge of this node will point to
* the right edge of node.
*
* Example:
*
* ```
* Precondition:
* - NULL <= ... <=> node <=> ... => NULL
* - NULL <= ... <=> this <=> ... => NULL
* Postcondition:
* - NULL <= ... <=> node <=> ... <=> this <=> ... => NULL
* ```
*/
void BNode::push_left(BNode* node)
{
if (!node) {
return;
}
BNode* left_edge = get_left_edge();
BNode* node_right_edge = node->get_right_edge();
node_right_edge->_right = left_edge;
left_edge->_left = node_right_edge;
}
/**
* `push_right()` will add new `node` to the outer right of this node.
*
* - If this node right pointer is not NULL then the new `node` will be
* inserted at the edge of right node.
*
* - If node left is not NULL then the right edge of this node will point to
* the left edge of node.
*
* Example:
*
* ```
* Precondition:
* * NULL <= ... <=> node <=> ... => NULL
* * NULL <= ... <=> this <=> ... => NULL
* Postcondition:
* * NULL <= ... <=> this <=> ... <=> node <=> ... => NULL
* ```
*/
void BNode::push_right(BNode* node)
{
if (!node) {
return;
}
BNode* right_edge = get_right_edge();
BNode* node_left_edge = node->get_left_edge();
node_left_edge->_left = right_edge;
right_edge->_right = node_left_edge;
}
/**
* `insert_left()` will add new `node` to the left pointer.
*
* - If this left pointer is not NULL then the new `node` will be inserted
* between left node and this node.
*
* Example:
* ```
* Precondition:
* * NULL <= ... <=> node <=> ... => NULL
* * NULL <= ... <=> left-node <=> this <=> right-node => NULL
* Postcondition:
* * NULL <= ... <=> left-node <=> ... <=> node <=> ... <=> this <=> ... => NULL
* ```
*/
void BNode::insert_left(BNode* node)
{
BNode* prev_node_left = _left;
BNode* node_right_edge = node->get_right_edge();
BNode* node_left_edge = node->get_left_edge();
_left = node_right_edge;
node_right_edge->_right = this;
if (prev_node_left) {
prev_node_left->_right = node_left_edge;
node_left_edge->_left = prev_node_left;
}
}
/**
* `insert_right()` will insert new `node` to the right pointer.
*
* - If right pointer is not NULL then the new `node` will be inserted between
* right node and this node.
*
* Example:
* ```
* Precondition:
* * NULL <= ... <=> node <=> ... => NULL
* * NULL <= ... <=> this <=> right-node <=> ... => NULL
* Postcondition:
* * NULL <= ... <=> this <=> ... <=> node <=> ... <=> right-node <=> ... => NULL
* ```
*/
void BNode::insert_right(BNode* node)
{
BNode* prev_node_right = _right;
BNode* node_left_edge = node->get_left_edge();
BNode* node_right_edge = node->get_right_edge();
_right = node_left_edge;
node_left_edge->_left = this;
if (prev_node_right) {
prev_node_right->_left = node_right_edge;
node_right_edge->_right = prev_node_right;
}
}
/**
* `pop_left_edge()` will detach the outer left node.
*
* (1) If left edge is this node, it will return NULL.
*/
BNode* BNode::pop_left_edge()
{
BNode* left_edge = get_left_edge();
// (1)
if (left_edge == this) {
return NULL;
}
left_edge->_right->_left = NULL;
left_edge->_right = NULL;
return left_edge;
}
/**
* `pop_right_edge()` will detach the outer right node.
*
* (1) If right edge is this node, it will return NULL.
*/
BNode* BNode::pop_right_edge()
{
BNode* right_edge = get_right_edge();
// (1)
if (right_edge == this) {
return NULL;
}
right_edge->_left->_right = NULL;
right_edge->_left = NULL;
return right_edge;
}
/**
* `chars()` will return representation of this node as string.
*/
const char* BNode::chars()
{
if (!_item) {
return NULL;
}
return _item->chars();
}
} // namespace vos
// vi: ts=8 sw=8 tw=80: