/
binarytree.h
550 lines (488 loc) · 15.2 KB
/
binarytree.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
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
/** @file binarytree.h Binary tree template.
*
* @authors Copyright © 2009-2013 Daniel Swanson <danij@dengine.net>
* @authors Copyright © 2012-2017 Jaakko Keränen <jaakko.keranen@iki.fi>
*
* @par License
* LGPL: http://www.gnu.org/licenses/lgpl.html
*
* <small>This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 3 of the License, or (at your
* option) any later version. This program is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
* General Public License for more details. You should have received a copy of
* the GNU Lesser General Public License along with this program; if not, see:
* http://www.gnu.org/licenses</small>
*/
#ifndef LIBCORE_BINARYTREE_H
#define LIBCORE_BINARYTREE_H
#include "../libcore.h"
#include "../Error"
#include "../String"
#include <algorithm>
namespace de {
/**
* Tree data structure where each node has a left and a right child.
*
* BinaryTree owns the child nodes and deletes them when the parent node is
* deleted. Each node additionally has a templated data payload.
*
* @ingroup data
*/
template <typename Type>
class BinaryTree
{
public:
/// The referenced parent is not present. @ingroup errors
DE_ERROR(MissingParentError);
/// The referenced child is not present. @ingroup errors
DE_ERROR(MissingChildError);
/**
* Child node identifiers.
*/
enum ChildId
{
Right, Left
};
private:
static inline void assertValidChildId(ChildId DE_DEBUG_ONLY(child))
{
DE_ASSERT(child == Right || child == Left);
}
public:
/**
* Constructs a new binary subtree.
*
* @param userData User data value for the node.
* @param parent Parent node of this node.
* @param right Right child of this node. This node takes ownership.
* @param left Left child of this node. This node takes ownership.
*/
BinaryTree(Type const &userData = Type(), BinaryTree *parent = 0, BinaryTree *right = 0, BinaryTree *left = 0)
: _parent(parent), _rightChild(right), _leftChild(left), _userDataValue(userData) {}
virtual ~BinaryTree()
{
clear();
}
/**
* Removes both branches of the tree.
*/
void clear()
{
delete _rightChild; _rightChild = 0;
delete _leftChild; _leftChild = 0;
}
/**
* Is this node a leaf?
* @return @c true iff this node is a leaf (no children).
*/
inline bool isLeaf() const
{
return !_rightChild && !_leftChild;
}
/**
* Retrieve the user data value associated with this node.
* @return User data pointer associated with this tree node else @c NULL.
*/
Type userData() const
{
return _userDataValue;
}
/**
* Set the user data value associated with this node.
*
* @param userData User data value to set with this tree node.
* @return Reference to this BinaryTree.
*/
BinaryTree &setUserData(Type userData)
{
_userDataValue = userData;
return *this;
}
/**
* Returns @c true iff subtree node has a parent node.
*/
inline bool hasParent() const { return _parent != 0; }
/**
* Returns the parent of the subtree.
*
* @see hasChild()
*/
BinaryTree &parent() const
{
if (_parent)
{
return *_parent;
}
/// @throw MissingParentError Attempted with no parent linked.
throw MissingParentError("BinaryTree::parent", "No parent is linked");
}
/**
* Returns a pointer to the parent of the subtree.
*
* @return Pointer to the parent; otherwise @c 0.
*
* @see hasParent(), parent()
*/
inline BinaryTree *parentPtr() const
{
return _parent;
}
/**
* Set the parent node of this node.
*
* @param parent Parent node to be linked (can be @c NULL).
* @return Reference to this BinaryTree.
*/
BinaryTree &setParent(BinaryTree *parent)
{
_parent = parent;
return *this;
}
/**
* Returns @c true iff this node has the specifed @a childId node.
*/
inline bool hasChild(ChildId which) const
{
assertValidChildId(which);
if (which) return _leftChild != 0;
else return _rightChild != 0;
}
/**
* Returns @c true iff a Right child subtree is linked to the binary tree.
*/
inline bool hasRight() const { return hasChild(Right); }
/**
* Returns @c true iff a Right child subtree is linked to the binary tree.
*/
inline bool hasLeft() const { return hasChild(Left); }
/**
* Convenient method for determining if a leaf is linked as the specified
* child subtree of the binary tree.
*
* @param which ChildId identifier of the child to inspect.
*
* @see hasChild(), isLeaf()
*/
inline bool hasChildLeaf(ChildId which) const
{
return hasChild(which) && child(which).isLeaf();
}
/**
* Convenient method for determining if a leaf is linked as the Right child
* of the binary tree.
*
* @see hasChildLeaf()
*/
inline bool hasRightLeaf() const { return hasChildLeaf(Right); }
/**
* Convenient method for determining if a leaf is linked as the Left child
* of the binary tree.
*
* @see hasChildLeaf()
*/
inline bool hasLeftLeaf() const { return hasChildLeaf(Left); }
/**
* Convenient method for determining if a subtree is linked as the specified
* child of the binary tree.
*
* @param which ChildId identifier of the child to inspect.
*
* @see hasChild(), isLeaf()
*/
inline bool hasChildSubtree(ChildId which) const
{
return hasChild(which) && !child(which).isLeaf();
}
/**
* Convenient method for determining if a subtree is linked as the Right
* child of the binary tree.
*
* @see hasChildSubtree()
*/
inline bool hasRightSubtree() const { return hasChildSubtree(Right); }
/**
* Convenient method for determining if a subtree is linked as the Left
* child of the binary tree.
*
* @see hasChildSubtree()
*/
inline bool hasLeftSubtree() const { return hasChildSubtree(Left); }
/**
* Returns the identified child subtree of the binary tree.
*
* @param which ChildId identifier of the child to return.
*
* @return The identified child subtree.
*
* @see hasChild()
*/
BinaryTree &child(ChildId which) const
{
assertValidChildId(which);
BinaryTree * const *adr = which? &_leftChild : &_rightChild;
if (*adr)
{
return **adr;
}
/// @throw MissingChildError Attempted with no child linked.
throw MissingChildError("BinaryTree::child",
stringf("No %s child is linked", which ? "Left" : "Right"));
}
/**
* Returns the Right child subtree of the binary tree.
*
* @see child()
*/
inline BinaryTree &right() const { return child(Right); }
/**
* Returns the Left child subtree of the binary tree.
*
* @see child()
*/
inline BinaryTree &left() const { return child(Left); }
/**
* Returns a pointer to the identified child of the node.
*
* @param which Identifier of the child to return.
*
* @return Pointer to the identified child; otherwise @c 0.
*
* @see hasChild(), child()
*/
inline BinaryTree *childPtr(ChildId which) const
{
return which == Left? _leftChild : _rightChild;
}
/**
* Returns a pointer to the Right subtree of the binary tree.
*
* @return Pointer to the Right subtree; otherwise @c 0.
*
* @see childPtr()
*/
inline BinaryTree *rightPtr() const { return childPtr(Right); }
/**
* Returns a pointer to the Left subtree of the binary tree.
*
* @return Pointer to the Left subtree; otherwise @c 0.
*
* @see childPtr()
*/
inline BinaryTree *leftPtr() const { return childPtr(Left); }
/**
* Set the specified node as a child of this node.
*
* @param child Identifier of the child to return.
* @param subtree Child subtree to be linked (can be @c NULL).
*
* @return Reference to this BinaryTree.
*/
BinaryTree &setChild(ChildId child, BinaryTree *subtree)
{
assertValidChildId(child);
if (child == Left)
_leftChild = subtree;
else
_rightChild = subtree;
return *this;
}
/// Convenience methods for setting the right and left subtrees respectively.
inline BinaryTree &setRight(BinaryTree *subtree) { return setChild(Right, subtree); }
inline BinaryTree &setLeft(BinaryTree *subtree) { return setChild(Left, subtree); }
/**
* Retrieve the height of this tree.
* @return Tree height.
*/
dsize height() const
{
if (!isLeaf())
{
size_t right = _rightChild? _rightChild->height() : 0;
size_t left = _leftChild ? _leftChild->height() : 0;
return (right > left? right : left) + 1;
}
return 0;
}
/**
* Determine the total number of nodes in the tree.
*/
int nodeCount() const
{
countsubtreeworker_params_t parm;
parm.countNodes = true;
parm.countLeafs = false;
parm.total = 0;
const_cast<BinaryTree *>(this)->traversePreOrder(countSubtreeWorker, &parm);
return parm.total;
}
/**
* Determine the total number of leafs in the tree.
*/
int leafCount() const
{
countsubtreeworker_params_t parm;
parm.countNodes = false;
parm.countLeafs = true;
parm.total = 0;
const_cast<BinaryTree *>(this)->traversePreOrder(countSubtreeWorker, &parm);
return parm.total;
}
/**
* Makes a copy of another tree. The structure of the other tree is
* duplicated in full, with copies of its user data.
*
* @param other Tree to make copy of.
*/
BinaryTree &operator = (BinaryTree const &other)
{
// Remove old children.
clear();
setUserData(other.userData());
if (other.hasLeft())
{
setLeft(new BinaryTree);
left() = other.left();
left().setParent(this);
}
if (other.hasRight())
{
setRight(new BinaryTree);
right() = other.right();
right().setParent(this);
}
return *this;
}
/**
* Traverse a binary tree in Preorder.
*
* Make a callback for all nodes of the tree (including the root). Traversal
* continues until all nodes have been visited or a callback returns non-zero at
* which point traversal is aborted.
*
* @param callback Function to call for each object of the tree.
* @param parameters Passed to the callback.
*
* @return @c 0= iff all callbacks complete wholly else the return value of the
* callback last made.
*/
int traversePreOrder(int (*callback) (BinaryTree &, void *), void *parameters = nullptr)
{
if (!callback) return false; // Continue iteration.
// Visit this node.
if (int result = callback(*this, parameters)) return result;
if (hasRight())
{
int result = right().traversePreOrder(callback, parameters);
if (result) return result;
}
if (hasLeft())
{
int result = left().traversePreOrder(callback, parameters);
if (result) return result;
}
return false; // Continue iteration.
}
/**
* Traverse a binary tree in Inorder.
*
* Make a callback for all nodes of the tree (including the root). Traversal
* continues until all nodes have been visited or a callback returns non-zero at
* which point traversal is aborted.
*
* @param callback Function to call for each object of the tree.
* @param parameters Passed to the callback.
*
* @return @c 0= iff all callbacks complete wholly else the return value of the
* callback last made.
*/
int traverseInOrder(int (*callback) (BinaryTree &, void *), void *parameters = nullptr)
{
if (!callback) return false; // Continue iteration.
if (hasRight())
{
int result = right().traverseInOrder(callback, parameters);
if (result) return result;
}
// Visit this node.
if (int result = callback(*this, parameters)) return result;
if (hasLeft())
{
int result = left().traverseInOrder(callback, parameters);
if (result) return result;
}
return false; // Continue iteration.
}
/**
* Traverse a binary tree in Postorder.
*
* Make a callback for all nodes of the tree (including the root). Traversal
* continues until all nodes have been visited or a callback returns non-zero at
* which point traversal is aborted.
*
* @param callback Function to call for each object of the tree.
* @param parameters Passed to the callback.
*
* @return @c 0= iff all callbacks complete wholly else the return value of the
* callback last made.
*/
int traversePostOrder(int (*callback) (BinaryTree &, void *), void *parameters = nullptr)
{
if (!callback) return false; // Continue iteration.
if (hasRight())
{
int result = right().traversePostOrder(callback, parameters);
if (result) return result;
}
if (hasLeft())
{
int result = left().traversePostOrder(callback, parameters);
if (result) return result;
}
// Visit this node.
return callback(*this, parameters);
}
/**
* Provides a textual summary of the tree structure.
*/
String summary() const
{
String text = String::format("%i nodes, %i leafs", nodeCount(), leafCount());
if (!isLeaf())
{
text += String::format(" (balance is %zu:%zu)",
hasRight() ? right().height() : 0,
hasLeft() ? left().height() : 0);
}
return text;
}
private:
struct countsubtreeworker_params_t
{
bool countNodes, countLeafs;
int total;
};
static int countSubtreeWorker(BinaryTree &subtree, void *context)
{
countsubtreeworker_params_t &p = *static_cast<countsubtreeworker_params_t *>(context);
if (subtree.isLeaf() && p.countLeafs)
{
p.total++;
}
if (!subtree.isLeaf() && p.countNodes)
{
p.total++;
}
return false; // Continue iteration.
}
/// Parent of this subtree (if any).
BinaryTree *_parent;
/// Subtrees (owned).
BinaryTree *_rightChild, *_leftChild;
/// User data at this node.
Type _userDataValue;
};
} // namespace de
#endif // LIBCORE_BINARYTREE_H