Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
tree-node.cc
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 version 2.0.0b
7 https://github.com/lrleon/Aleph-w
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2026 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31
32
33
38# include <gtest/gtest.h>
39
40# include <algorithm>
41# include <cstring>
42# include <map>
43# include <new>
44# include <random>
45# include <stdexcept>
46# include <vector>
47
48# include <tpl_binNode.H>
49# include <tpl_tree_node.H>
50# include <ah-zip.H>
51# include <ah-string-utils.H>
52
53# include "tree-node-common.H"
54
55using namespace std;
56using namespace Aleph;
57
58// The key of a node built by default is T(): for a scalar it was left
59// indeterminate, and reading it, even to copy it, was undefined behavior.
60// The node is built over memory with a pattern, which its key must not
61// keep
62TEST(Tree_Node, default_key_is_value_initialized)
63{
64 alignas(Tree_Node<long>) unsigned char buf[sizeof(Tree_Node<long>)];
65 memset(buf, 0x5a, sizeof(buf));
66 auto * p = ::new (static_cast<void *>(buf)) Tree_Node<long>;
67 EXPECT_EQ(p->get_key(), 0);
68 EXPECT_TRUE(p->is_root());
69 EXPECT_TRUE(p->is_leaf());
70 p->~Tree_Node<long>();
71
73 EXPECT_EQ(q.get_key(), nullptr);
74}
75
76TEST(Tree_Node, on_isolated_node)
77{
79
80 ASSERT_TRUE(p.is_root());
81 ASSERT_TRUE(p.is_leaf());
82 ASSERT_TRUE(p.is_leftmost());
83 ASSERT_TRUE(p.is_rightmost());
84 ASSERT_TRUE(p.get_child_list()->is_empty());
85 ASSERT_TRUE(p.get_sibling_list()->is_empty());
86
87 ASSERT_EQ(p.get_right_child(), nullptr);
88 ASSERT_EQ(p.get_left_child(), nullptr);
89
90 ASSERT_EQ(p.get_child(0), nullptr);
91 ASSERT_EQ(p.get_parent(), nullptr);
92
93 ASSERT_EQ(p.get_left_tree(), nullptr);
94 ASSERT_EQ(p.get_right_tree(), nullptr);
95
96 ASSERT_TRUE(p.children().is_empty());
97
98 ASSERT_TRUE(p.traverse([] (auto) { return true; }));
99 ASSERT_FALSE(p.traverse([] (auto) { return false; }));
100
102 ASSERT_TRUE(it.has_curr());
103 ASSERT_EQ(it.get_curr(), &p);
104 ASSERT_NO_THROW(it.next());
105 ASSERT_FALSE(it.has_curr());
106 ASSERT_THROW(it.get_curr(), overflow_error);
107 ASSERT_THROW(it.next(), overflow_error);
108 it.reset_first();
109 ASSERT_TRUE(it.has_curr());
110 ASSERT_EQ(it.get_curr(), &p);
111 ASSERT_NO_THROW(it.next());
112 ASSERT_FALSE(it.has_curr());
113 ASSERT_THROW(it.get_curr(), overflow_error);
114 ASSERT_THROW(it.next(), overflow_error);
115
117 ASSERT_FALSE(cit.has_curr());
118 ASSERT_THROW(cit.get_curr(), overflow_error);
119 ASSERT_THROW(cit.next(), overflow_error);
120}
121
122TEST(Tree_Node, get_parent_tracks_multi_level_tree)
123{
125 Tree_Node<int> left(1);
126 Tree_Node<int> right(2);
127 Tree_Node<int> left_left(3);
128 Tree_Node<int> left_right(4);
129 Tree_Node<int> deep(5);
130
131 root.insert_rightmost_child(&left);
132 root.insert_rightmost_child(&right);
133 left.insert_rightmost_child(&left_left);
134 left.insert_rightmost_child(&left_right);
135 left_left.insert_rightmost_child(&deep);
136
137 ASSERT_EQ(root.get_parent(), nullptr);
138 ASSERT_EQ(left.get_parent(), &root);
139 ASSERT_EQ(right.get_parent(), &root);
140 ASSERT_EQ(left_left.get_parent(), &left);
141 ASSERT_EQ(left_right.get_parent(), &left);
142 ASSERT_EQ(deep.get_parent(), &left_left);
143}
144
145TEST(Tree_Node, insert_leftmost_child_preserves_existing_subtree_parent_links)
146{
148 Tree_Node<int> old_left(1);
149 Tree_Node<int> old_right(2);
150 Tree_Node<int> old_left_child(3);
151 Tree_Node<int> new_left(4);
152
153 root.insert_rightmost_child(&old_left);
154 root.insert_rightmost_child(&old_right);
155 old_left.insert_rightmost_child(&old_left_child);
156
157 root.insert_leftmost_child(&new_left);
158
159 ASSERT_EQ(root.get_left_child(), &new_left);
160 ASSERT_EQ(root.get_child(0), &new_left);
161 ASSERT_EQ(root.get_child(1), &old_left);
162 ASSERT_EQ(root.get_child(2), &old_right);
163
164 ASSERT_EQ(new_left.get_parent(), &root);
165 ASSERT_EQ(old_left.get_parent(), &root);
166 ASSERT_EQ(old_right.get_parent(), &root);
167 ASSERT_EQ(old_left_child.get_parent(), &old_left);
168
169 ASSERT_TRUE(new_left.is_leftmost());
170 ASSERT_FALSE(new_left.is_rightmost());
171 ASSERT_FALSE(old_left.is_leftmost());
172 ASSERT_FALSE(old_right.is_leftmost());
173 ASSERT_TRUE(old_right.is_rightmost());
174}
175
176TEST(Tree_Node, insert_left_sibling_before_leftmost_child_updates_spine)
177{
179 Tree_Node<int> old_left(1);
180 Tree_Node<int> right(2);
181 Tree_Node<int> old_left_child(3);
182 Tree_Node<int> new_left(4);
183
184 root.insert_rightmost_child(&old_left);
185 root.insert_rightmost_child(&right);
186 old_left.insert_rightmost_child(&old_left_child);
187
188 old_left.insert_left_sibling(&new_left);
189
190 ASSERT_EQ(root.get_left_child(), &new_left);
191 ASSERT_EQ(root.get_child(0), &new_left);
192 ASSERT_EQ(root.get_child(1), &old_left);
193 ASSERT_EQ(root.get_child(2), &right);
194
195 ASSERT_EQ(new_left.get_parent(), &root);
196 ASSERT_EQ(old_left.get_parent(), &root);
197 ASSERT_EQ(right.get_parent(), &root);
198 ASSERT_EQ(old_left_child.get_parent(), &old_left);
199
200 ASSERT_TRUE(new_left.is_leftmost());
201 ASSERT_FALSE(new_left.is_rightmost());
202 ASSERT_FALSE(old_left.is_leftmost());
203 ASSERT_FALSE(right.is_leftmost());
204 ASSERT_TRUE(right.is_rightmost());
205}
206
207TEST(Tree_Node, bin_to_forest_preserves_each_forest_root_flag)
208{
209 BinNode<int> n1(1);
210 BinNode<int> n2(2);
211 BinNode<int> n3(3);
212
213 RLINK(&n1) = &n2;
214 RLINK(&n2) = &n3;
215
216 auto * forest = bin_to_forest<Tree_Node<int>, BinNode<int>>(&n1);
217 ASSERT_NE(forest, nullptr);
218
219 size_t count = 0;
220 for (auto * t = forest; t != nullptr; t = t->get_right_sibling(), ++count)
221 {
222 EXPECT_TRUE(t->is_root()) << "tree key " << t->get_key()
223 << " must remain a forest root";
224 EXPECT_EQ(t->get_parent(), nullptr);
225 }
226
227 EXPECT_EQ(count, 3);
228 destroy_forest(forest);
229}
230
231TEST(Tree_Node, insert_right_sibling_on_root_links_forest_roots)
232{
233 Tree_Node<int> tree1(1);
234 Tree_Node<int> tree2(2);
235 Tree_Node<int> tree3(3);
236
237 tree1.insert_right_sibling(&tree2);
238 tree2.insert_right_sibling(&tree3);
239
240 ASSERT_EQ(tree1.get_right_tree(), &tree2);
241 ASSERT_EQ(tree2.get_right_tree(), &tree3);
242 ASSERT_EQ(tree3.get_right_tree(), nullptr);
243
244 ASSERT_TRUE(tree1.is_root());
245 ASSERT_TRUE(tree2.is_root());
246 ASSERT_TRUE(tree3.is_root());
247
248 ASSERT_EQ(tree1.get_parent(), nullptr);
249 ASSERT_EQ(tree2.get_parent(), nullptr);
250 ASSERT_EQ(tree3.get_parent(), nullptr);
251}
252
253TEST(Tree_Node, unique_sibling_macro_tracks_sibling_links)
254{
255 Tree_Node<int> isolated(0);
256 ASSERT_TRUE(IS_UNIQUE_SIBLING(&isolated));
257
259 Tree_Node<int> only_child(2);
260 root.insert_rightmost_child(&only_child);
261 ASSERT_TRUE(IS_UNIQUE_SIBLING(&only_child));
262
263 Tree_Node<int> other_child(3);
264 root.insert_rightmost_child(&other_child);
265 ASSERT_FALSE(IS_UNIQUE_SIBLING(&only_child));
266 ASSERT_FALSE(IS_UNIQUE_SIBLING(&other_child));
267
268 Tree_Node<int> tree1(4);
269 Tree_Node<int> tree2(5);
270 tree1.insert_tree_to_right(&tree2);
271 ASSERT_FALSE(IS_UNIQUE_SIBLING(&tree1));
272 ASSERT_FALSE(IS_UNIQUE_SIBLING(&tree2));
273}
274
275TEST(Tree_Node, destroy_tree_on_non_leftmost_child_regression)
276{
278 Tree_Node<int>* child1 = new Tree_Node<int>(2);
279 Tree_Node<int>* child2 = new Tree_Node<int>(3);
280
281 root->insert_rightmost_child(child1);
282 root->insert_rightmost_child(child2);
283
284 // Layout: child1 -> child2 (rightmost)
285 // Destroying a non-leftmost sibling with a left sibling
286 destroy_tree(child2);
287
288 ASSERT_EQ(root->get_left_child(), child1);
289 ASSERT_EQ(root->get_right_child(), child1);
290 ASSERT_TRUE(IS_UNIQUE_SIBLING(child1));
291 ASSERT_TRUE(child1->is_rightmost());
292 ASSERT_EQ(child1->get_right_sibling(), nullptr);
293
295}
296
297TEST(Tree_Node, destroy_tree_restores_is_rightmost_on_surviving_sibling)
298{
299 // Regression: destroy_tree() unlinked a node from its sibling list
300 // without restoring is_rightmost()/is_leftmost() on the neighbor taking
301 // its place. get_right_child()/get_left_child() use the raw (flag-blind)
302 // Dlink links and so cannot detect this; get_right_sibling() and anything
303 // built on it (for_each_child(), Children_Iterator) are flag-gated and
304 // did loop forever on the surviving sibling before this fix.
305 auto *root = new Tree_Node<int>(0);
306 auto *a = new Tree_Node<int>(1);
307 auto *b = new Tree_Node<int>(2);
308 auto *c = new Tree_Node<int>(3);
309 root->insert_rightmost_child(a);
310 root->insert_rightmost_child(b);
311 root->insert_rightmost_child(c); // root -> a, b, c (c is rightmost)
312
313 destroy_tree(c);
314
315 EXPECT_TRUE(b->is_rightmost());
316 EXPECT_EQ(b->get_right_sibling(), nullptr);
317
318 int count = 0;
319 root->for_each_child([&count](auto) { ++count; });
320 EXPECT_EQ(count, 2); // a, b -- would loop forever before the fix
321
323}
324
325TEST(Tree_Node, destroy_tree_restores_is_leftmost_on_surviving_sibling)
326{
327 // Symmetric case: destroying the current leftmost child must promote
328 // its former right neighbor to is_leftmost().
329 auto *root = new Tree_Node<int>(0);
330 auto *a = new Tree_Node<int>(1);
331 auto *b = new Tree_Node<int>(2);
332 auto *c = new Tree_Node<int>(3);
333 root->insert_rightmost_child(a);
334 root->insert_rightmost_child(b);
335 root->insert_rightmost_child(c); // root -> a (leftmost), b, c
336
337 destroy_tree(a);
338
339 EXPECT_TRUE(b->is_leftmost());
340 EXPECT_EQ(b->get_left_sibling(), nullptr);
341 EXPECT_EQ(root->get_left_child(), b);
342
343 int count = 0;
344 root->for_each_child([&count](auto) { ++count; });
345 EXPECT_EQ(count, 2); // b, c
346
348}
349
350// destroy_tree() gave the place of a leftmost child to its right sibling
351// only if that sibling was a leaf: the children of one with children were
352// lost, and the next traversal of the parent did not end
353TEST(Tree_Node, destroy_tree_of_a_leftmost_child_whose_sibling_has_children)
354{
355 auto *root = new Tree_Node<int>(0);
356 auto *a = new Tree_Node<int>(1);
357 auto *b = new Tree_Node<int>(2);
358 auto *b1 = new Tree_Node<int>(21);
359 auto *b2 = new Tree_Node<int>(22);
360 root->insert_rightmost_child(a);
361 root->insert_rightmost_child(b);
362 a->insert_rightmost_child(new Tree_Node<int>(11));
363 b->insert_rightmost_child(b1);
364 b->insert_rightmost_child(b2);
365
366 destroy_tree(a);
367
368 EXPECT_EQ(root->get_left_child(), b);
369 EXPECT_EQ(root->get_right_child(), b);
370 EXPECT_TRUE(b->is_leftmost());
371 EXPECT_EQ(b->get_parent(), root);
372 EXPECT_EQ(b->get_left_child(), b1);
373 EXPECT_EQ(b->get_right_child(), b2);
374 EXPECT_EQ(b1->get_parent(), b);
375 EXPECT_EQ(b2->get_parent(), b);
376
378}
379
380// The parent of the only child was left as a node with children
381TEST(Tree_Node, destroy_tree_of_the_only_child_makes_the_parent_a_leaf)
382{
383 auto *root = new Tree_Node<int>(0);
384 auto *a = new Tree_Node<int>(1);
385 root->insert_rightmost_child(a);
386 a->insert_rightmost_child(new Tree_Node<int>(11));
387
388 destroy_tree(a);
389
390 EXPECT_TRUE(root->is_leaf());
391 EXPECT_EQ(root->get_left_child(), nullptr);
392
393 root->insert_rightmost_child(new Tree_Node<int>(2)); // the root still works
394 ASSERT_NE(root->get_left_child(), nullptr);
395 EXPECT_EQ(root->get_left_child()->get_key(), 2);
396 EXPECT_EQ(root->get_left_child()->get_parent(), root);
397
399}
400
401TEST(Tree_Node, destroy_tree_of_a_child_with_children_that_is_not_leftmost)
402{
403 auto *root = new Tree_Node<int>(0);
404 auto *a = new Tree_Node<int>(1);
405 auto *b = new Tree_Node<int>(2);
406 auto *c = new Tree_Node<int>(3);
407 root->insert_rightmost_child(a);
408 root->insert_rightmost_child(b);
409 root->insert_rightmost_child(c);
410 b->insert_rightmost_child(new Tree_Node<int>(21));
411 b->get_left_child()->insert_rightmost_child(new Tree_Node<int>(211));
412
413 destroy_tree(b);
414
415 EXPECT_EQ(a->get_right_sibling(), c);
416 EXPECT_EQ(c->get_left_sibling(), a);
417 EXPECT_EQ(c->get_parent(), root);
418
420}
421
422TEST(Tree_Node, destroy_tree_of_a_tree_of_a_forest)
423{
424 auto *t1 = new Tree_Node<int>(1);
425 auto *t2 = new Tree_Node<int>(2);
426 auto *t3 = new Tree_Node<int>(3);
427 t1->insert_right_sibling(t2);
428 t2->insert_right_sibling(t3);
429 t2->insert_rightmost_child(new Tree_Node<int>(21));
430
431 destroy_tree(t2);
432
433 EXPECT_EQ(t1->get_right_tree(), t3);
434 EXPECT_EQ(t3->get_left_tree(), t1);
435
436 destroy_tree(t1); // the last tree of the forest is t3 then
437 EXPECT_TRUE(t3->is_leftmost());
438 EXPECT_EQ(t3->get_left_tree(), nullptr);
439 destroy_tree(t3);
440}
441
442// destroy_tree() recursed as deep as the tree
443TEST(Tree_Node, destroy_tree_of_a_deep_chain)
444{
445 auto *root = new Tree_Node<int>(0);
446 Tree_Node<int> *last = root;
447 for (int i = 1; i < 1000000; ++i)
448 {
449 auto *p = new Tree_Node<int>(i);
450 last->insert_rightmost_child(p);
451 last = p;
452 }
453
455}
456
457namespace
458{
459 using Model_Node = Tree_Node<int>;
460
461 // The parent and the children, in their order, of each node of a tree
462 struct Tree_Model
463 {
464 std::map<Model_Node *, Model_Node *> parent;
465 std::map<Model_Node *, std::vector<Model_Node *>> children;
466
467 void add(Model_Node *p, Model_Node *par)
468 {
469 parent[p] = par;
470 children[p];
471 }
472
473 // Takes p, and the nodes below it, out of the model
474 void erase(Model_Node *p)
475 {
476 auto &siblings = children[parent[p]];
477 siblings.erase(std::find(siblings.begin(), siblings.end(), p));
478 std::vector<Model_Node *> stack = {p};
479 while (not stack.empty())
480 {
481 auto *q = stack.back();
482 stack.pop_back();
483 for (auto *c : children[q])
484 stack.push_back(c);
485 children.erase(q);
486 parent.erase(q);
487 }
488 }
489 };
490
491 // The tree of root is that of the model
492 void expect_model(Model_Node *root, const Tree_Model &model)
493 {
494 for (const auto &[p, kids] : model.children)
495 {
496 if (p != root)
497 {
498 ASSERT_EQ(p->get_parent(), model.parent.at(p));
499 }
500 std::vector<Model_Node *> seen;
501 for (auto *c = p->get_left_child(); c != nullptr and seen.size() <= kids.size();
502 c = c->get_right_sibling())
503 seen.push_back(c);
504 ASSERT_EQ(seen, kids);
505 ASSERT_EQ(p->is_leaf(), kids.empty());
506 if (not kids.empty())
507 {
508 ASSERT_EQ(p->get_right_child(), kids.back());
509 }
510 for (size_t i = 0; i < kids.size(); ++i)
511 {
512 ASSERT_EQ(kids[i]->is_leftmost(), i == 0);
513 ASSERT_EQ(kids[i]->is_rightmost(), i + 1 == kids.size());
514 }
515 }
516 }
517}
518
519// Random insertions, and destroy_tree() of random nodes, which must leave
520// the rest of the tree as if they had not been inserted
521TEST(Tree_Node, destroy_tree_keeps_the_rest_of_the_tree)
522{
523 std::mt19937 gen(7);
524 int key = 0;
525 for (int round = 0; round < 20; ++round)
526 {
527 SCOPED_TRACE(round);
528 Tree_Model model;
529 auto *root = new Model_Node(key++);
530 model.add(root, nullptr);
531 for (int op = 0; op < 300; ++op)
532 {
533 std::vector<Model_Node *> nodes;
534 for (const auto &kv : model.parent)
535 nodes.push_back(kv.first);
536 auto *p = nodes[gen() % nodes.size()];
537 if (p == root or gen() % 10 < 6)
538 {
539 auto *q = new Model_Node(key++);
540 const auto how = p == root ? gen() % 2 : gen() % 4;
541 if (how < 2)
542 {
543 auto &kids = model.children[p];
544 if (how == 0)
545 {
546 p->insert_rightmost_child(q);
547 kids.push_back(q);
548 }
549 else
550 {
551 p->insert_leftmost_child(q);
552 kids.insert(kids.begin(), q);
553 }
554 model.add(q, p);
555 continue;
556 }
557
558 auto *par = model.parent[p];
559 auto &kids = model.children[par];
560 auto pos = std::find(kids.begin(), kids.end(), p);
561 if (how == 2)
562 {
563 p->insert_left_sibling(q);
564 kids.insert(pos, q);
565 }
566 else
567 {
568 p->insert_right_sibling(q);
569 kids.insert(pos + 1, q);
570 }
571 model.add(q, par);
572 }
573 else
574 {
575 model.erase(p);
576 destroy_tree(p);
577 }
578 expect_model(root, model);
579 if (HasFatalFailure())
580 return;
581 }
583 }
584}
585
586TEST(Tree_Node, simple_tree_construction_and_destruction)
587{
588 Tree_Node<int> p1 = 1;
589 Tree_Node<int> p2 = 2;
590 Tree_Node<int> p3 = 3;
591 Tree_Node<int> p4 = 4;
592 Tree_Node<int> p5 = 5;
593
594 /* 1 insert_leftmost_child() test
595 |
596 2
597 */
598 p1.insert_leftmost_child(&p2);
599 ASSERT_TRUE(p1.is_root());
600 ASSERT_TRUE(p1.is_leftmost());
601 ASSERT_TRUE(p1.is_rightmost());
602 ASSERT_FALSE(p1.is_leaf());
603 ASSERT_FALSE(p2.is_root());
604 ASSERT_TRUE(p2.is_leftmost());
605 ASSERT_TRUE(p2.is_rightmost());
606
607 /* 1 insert_rightmost_child() test
608 /\
609 2 3
610 */
612 ASSERT_TRUE(p1.is_root());
613 ASSERT_TRUE(p1.is_leftmost());
614 ASSERT_TRUE(p1.is_rightmost());
615 ASSERT_FALSE(p1.is_leaf());
616 ASSERT_TRUE(p2.is_leaf());
617 ASSERT_FALSE(p2.is_root());
618 ASSERT_TRUE(p2.is_leftmost());
619 ASSERT_FALSE(p2.is_rightmost());
620 ASSERT_TRUE(p2.is_leaf());
621 ASSERT_FALSE(p3.is_root());
622 ASSERT_FALSE(p3.is_leftmost());
623 ASSERT_TRUE(p3.is_rightmost());
624
625 /* 0
626 / | \
627 2 3 5
628 */
629 p3.insert_right_sibling(&p5);
630 ASSERT_TRUE(p1.is_root());
631 ASSERT_TRUE(p1.is_rightmost());
632 ASSERT_TRUE(p1.is_leftmost());
633 ASSERT_FALSE(p1.is_leaf());
634 ASSERT_FALSE(p2.is_root());
635 ASSERT_TRUE(p2.is_leftmost());
636 ASSERT_FALSE(p2.is_rightmost());
637 ASSERT_TRUE(p2.is_leaf());
638 ASSERT_FALSE(p3.is_root());
639 ASSERT_FALSE(p3.is_leftmost());
640 ASSERT_FALSE(p3.is_rightmost());
641 ASSERT_TRUE(p3.is_leaf());
642 ASSERT_FALSE(p5.is_leftmost());
643 ASSERT_TRUE(p5.is_rightmost());
644 ASSERT_FALSE(p5.is_root());
645 ASSERT_TRUE(p5.is_leaf());
646
647 /* 1
648 / / | |
649 2 3 4 5
650 */
651 p5.insert_left_sibling(&p4);
652 ASSERT_TRUE(p1.is_root());
653 ASSERT_TRUE(p1.is_rightmost());
654 ASSERT_TRUE(p1.is_leftmost());
655 ASSERT_FALSE(p1.is_leaf());
656 ASSERT_FALSE(p2.is_root());
657 ASSERT_TRUE(p2.is_leftmost());
658 ASSERT_FALSE(p2.is_rightmost());
659 ASSERT_TRUE(p2.is_leaf());
660
661 ASSERT_FALSE(p3.is_root());
662 ASSERT_FALSE(p3.is_leftmost());
663 ASSERT_FALSE(p3.is_rightmost());
664 ASSERT_TRUE(p3.is_leaf());
665
666 ASSERT_FALSE(p4.is_root());
667 ASSERT_FALSE(p4.is_leftmost());
668 ASSERT_FALSE(p4.is_rightmost());
669 ASSERT_TRUE(p4.is_leaf());
670
671 ASSERT_FALSE(p5.is_leftmost());
672 ASSERT_TRUE(p5.is_rightmost());
673 ASSERT_FALSE(p5.is_root());
674 ASSERT_TRUE(p5.is_leaf());
675
676 ASSERT_EQ(p1.get_left_child(), &p2);
677 ASSERT_EQ(p1.get_right_child(), &p5);
678
679 ASSERT_EQ(p2.get_left_sibling(), nullptr);
680 ASSERT_EQ(p2.get_right_sibling(), &p3);
681
682 ASSERT_EQ(p3.get_left_sibling(), &p2);
683 ASSERT_EQ(p3.get_right_sibling(), &p4);
684
685 ASSERT_EQ(p4.get_left_sibling(), &p3);
686 ASSERT_EQ(p4.get_right_sibling(), &p5);
687
688 ASSERT_EQ(p5.get_left_sibling(), &p4);
689 ASSERT_EQ(p5.get_right_sibling(), nullptr);
690
691 ASSERT_EQ(p1.get_child(0), &p2);
692 ASSERT_EQ(p1.get_child(1), &p3);
693 ASSERT_EQ(p1.get_child(2), &p4);
694 ASSERT_EQ(p1.get_child(3), &p5);
695
696 int k = 0;
697 ASSERT_TRUE(p1.traverse([&k] (auto p) { return p->get_key() == ++k; }));
698 ASSERT_EQ(k, 5);
699 k = 1;
700 ASSERT_TRUE(p1.children_nodes().traverse([&k] (auto p)
701 { return p->get_key() == ++k; }));
702 ASSERT_EQ(k, 5);
703 k = 1;
704 ASSERT_TRUE(p1.children().traverse([&k] (auto i) { return i == ++k; }));
705 ASSERT_EQ(k, 5);
706}
707
708TEST(Tree_Node, Iterator_on_extreme_cases)
709{
710 {
711 Tree_Node<int>::Iterator it = nullptr;
712 ASSERT_FALSE(it.has_curr());
713 ASSERT_THROW(it.get_curr(), overflow_error);
714 ASSERT_THROW(it.next(), overflow_error);
715 }
716
717 {
718 Tree_Node<int> p(0);
719 auto it = p.get_it();
720 ASSERT_TRUE(it.has_curr());
721 ASSERT_EQ(it.get_pos(), 0);
722 ASSERT_NO_THROW(it.next());
723 ASSERT_FALSE(it.has_curr());
724 ASSERT_EQ(it.get_pos(), 1);
725 ASSERT_THROW(it.get_curr(), overflow_error);
726 ASSERT_THROW(it.next(), overflow_error);
727 }
728
729 {
730 Tree_Node<int> p0(0);
731 Tree_Node<int> p1(1);
732 p0.insert_leftmost_child(&p1);
733 auto it = p0.get_it();
734 ASSERT_TRUE(it.has_curr());
735 ASSERT_EQ(it.get_curr(), &p0);
736 ASSERT_NO_THROW(it.next());
737 ASSERT_TRUE(it.has_curr());
738 ASSERT_EQ(it.get_curr(), &p1);
739 ASSERT_NO_THROW(it.next());
740 ASSERT_FALSE(it.has_curr());
741 ASSERT_THROW(it.get_curr(), overflow_error);
742 ASSERT_THROW(it.next(), overflow_error);
743
744 ASSERT_NO_THROW(it.reset_first());
745 ASSERT_TRUE(it.has_curr());
746 ASSERT_EQ(it.get_curr(), &p0);
747 ASSERT_NO_THROW(it.next());
748 ASSERT_TRUE(it.has_curr());
749 ASSERT_EQ(it.get_curr(), &p1);
750 ASSERT_NO_THROW(it.next());
751 ASSERT_FALSE(it.has_curr());
752 ASSERT_THROW(it.get_curr(), overflow_error);
753 ASSERT_THROW(it.next(), overflow_error);
754 }
755}
756
758{
759 auto itl = l.get_it();
760 size_t k = 0;
761 for (auto it = root->get_it(); it.has_curr(); it.next(), itl.next(), ++k)
762 ASSERT_EQ(it.get_curr()->get_key(), itl.get_curr());
763 ASSERT_GT(k, 0);
764}
765
766TEST(Tree_Node, clone_on_extreme_cases)
767{
768 {
769 Tree_Node<int> * root = nullptr;
770 ASSERT_EQ(clone_tree(root), nullptr);
771 }
772 {
774 auto rootp = clone_tree(&root);
775 ASSERT_NE(rootp, nullptr);
776 ASSERT_EQ(root.get_key(), rootp->get_key());
777 destroy_tree(rootp);
778 }
779}
780
781TEST_F(Simple_Tree, level_traversal)
782{
783 int i = 0;
784 ASSERT_TRUE(root->level_traverse([&i] (auto p)
785 {
786 return p->get_key() == i++;
787 }));
788 ASSERT_EQ(i, 31);
789}
790
792{
793 Tree_Node<int> * clone = clone_tree(root);
795 using Pit = Pair_Iterator<It>;
796 for (Pit it{It(root), It(clone)}; it.has_curr(); it.next())
797 {
798 auto p = it.get_curr();
799 ASSERT_EQ(p.first->get_key(), p.second->get_key());
800 }
801 destroy_tree(clone);
802}
803
804TEST(Tree_Node, traverse_on_extreme_cases)
805{
806 {
807 Tree_Node<int> * root = nullptr;
808 ASSERT_EQ(root, nullptr);
809 }
810 {
812 size_t k = 0;
813 ASSERT_TRUE(root.traverse([&k] (auto p) { k++; return p->get_key() == 5; }));
814 ASSERT_EQ(k, 1);
815 }
816}
817
818TEST(Tree_Node, are_tree_equal_uses_custom_comparator_recursively)
819{
821
822 auto * t1 = new Node("root");
823 t1->insert_rightmost_child(new Node("A"));
824 t1->insert_rightmost_child(new Node("B"));
825
826 auto * t2 = new Node("ROOT");
827 t2->insert_rightmost_child(new Node("a"));
828 t2->insert_rightmost_child(new Node("b"));
829
830 auto nocase = [] (const std::string & a, const std::string & b)
831 {
832 if (a.size() != b.size())
833 return false;
834 for (size_t i = 0; i < a.size(); ++i)
835 if (std::tolower(static_cast<unsigned char>(a[i])) !=
836 std::tolower(static_cast<unsigned char>(b[i])))
837 return false;
838 return true;
839 };
840
841 ASSERT_TRUE((are_tree_equal<Node, decltype(nocase)>(t1, t2, nocase)));
842
843 destroy_tree(t1);
844 destroy_tree(t2);
845}
846
848{
849 auto it = l.get_it();
850 size_t k = 0;
851 auto ret = root->traverse([&it, &k] (auto p)
852 {
853 bool r = p->get_key() == it.get_curr();
854 it.next(); ++k;
855 return r;
856 });
857 ASSERT_TRUE(ret);
858 ASSERT_EQ(k, l.size());
859}
860
862{
863 int d[100];
864 size_t sz;
865 auto p = search_deway(root, 14, d, 100, sz);
866 ASSERT_EQ(p->get_key(), 14);
867 ASSERT_EQ(sz, 3);
868 ASSERT_EQ(d[0], 0);
869 ASSERT_EQ(d[1], 1);
870 ASSERT_EQ(d[2], 3);
871}
872
873/* 0
874
875 1 2 3 4 5
876
8776 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
878
879*/
880
881TEST_F(Three_Trees, insertion_of_trees)
882{
883 auto t1 = clone_tree(root1);
884 auto t2 = clone_tree(root2);
885 auto t3 = clone_tree(root3);
886
887 t1->insert_tree_to_right(t3);
888 t1->insert_tree_to_right(t2);
889
890 DynList<Tree_Node<int>*> tlist = { t1, t2, t3 };
891 DynList<Tree_Node<int>*> flist = t1->trees();
892
893 zip_for_each([] (auto t) { ASSERT_EQ(get<0>(t), get<1>(t)); }, tlist, flist);
894
895 destroy_forest(t1);
896}
897
899{
900 auto t = clone_tree(root1);
901 auto t2 = clone_tree(root2);
902
903 t->join(t2);
904
905 DynList<int> l = { 0, 1, 2, 3, 4, 5, 31, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
906 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
907 30, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43 };
908
909 DynList<int> order;
910 t->level_traverse([&order] (auto p) { order.append(p->get_key()); return true; });
911
912 ASSERT_TRUE(eq(l, order));
913
915}
916
917// =============================================================================
918// Deep and wide trees, and the functions that recursed over them
919// =============================================================================
920
921namespace
922{
923 // A chain of n nodes, keyed 0 to n - 1: a tree of n levels
924 Tree_Node<int> *chain_of(const int n)
925 {
926 auto *root = new Tree_Node<int>(0);
927 Tree_Node<int> *last = root;
928 for (int i = 1; i < n; ++i)
929 {
930 auto *p = new Tree_Node<int>(i);
931 last->insert_rightmost_child(p);
932 last = p;
933 }
934 return root;
935 }
936
937 // A root, keyed 0, with n - 1 children, keyed 1 to n - 1
938 Tree_Node<int> *star_of(const int n)
939 {
940 auto *root = new Tree_Node<int>(0);
941 for (int i = 1; i < n; ++i)
942 root->insert_rightmost_child(new Tree_Node<int>(i));
943 return root;
944 }
945
946 // The deepest node of the first children
947 Tree_Node<int> *deepest_of(Tree_Node<int> *p)
948 {
949 while (p->get_left_child() != nullptr)
950 p = p->get_left_child();
951 return p;
952 }
953
954 // Frees a binary tree without recursion: a node with a left child
955 // turns into the right child of that child first
956 template <class BNode>
957 void free_binary(BNode *root)
958 {
959 while (root != BNode::NullPtr)
960 if (LLINK(root) != BNode::NullPtr)
961 {
962 BNode *left = LLINK(root);
963 LLINK(root) = RLINK(left);
964 RLINK(left) = root;
965 root = left;
966 }
967 else
968 {
969 BNode *right = RLINK(root);
970 delete root;
971 root = right;
972 }
973 }
974
975 long visit_sum = 0;
976 long visit_count = 0;
977
978 void sum_visit(Tree_Node<int> *p, int level, int pos)
979 {
980 visit_sum += long(p->get_key()) + level + pos;
981 ++visit_count;
982 }
983}
984
985// These functions recursed as deep as the tree, which a chain of a million
986// nodes overflows
987TEST(Tree_Node, the_functions_on_a_deep_chain_need_no_recursion)
988{
989 constexpr int n = 1000000;
990 Tree_Node<int> *root = chain_of(n);
991 Tree_Node<int> *deepest = deepest_of(root);
992
993 long count = 0;
994 EXPECT_TRUE(root->traverse([&count](Tree_Node<int> *) { ++count; return true; }));
995 EXPECT_EQ(count, n);
996
997 const long sum = 2 * (long(n) * (n - 1) / 2); // keys and levels; positions are 0
998 visit_sum = visit_count = 0;
999 tree_preorder_traversal(root, sum_visit);
1000 EXPECT_EQ(visit_count, n);
1001 EXPECT_EQ(visit_sum, sum);
1002
1003 visit_sum = visit_count = 0;
1004 tree_postorder_traversal(root, sum_visit);
1005 EXPECT_EQ(visit_count, n);
1006 EXPECT_EQ(visit_sum, sum);
1007
1008 EXPECT_EQ(compute_height(root), size_t(n));
1009
1011 EXPECT_TRUE(are_tree_equal(root, copy));
1012 deepest_of(copy)->get_key() = -1;
1013 EXPECT_FALSE(are_tree_equal(root, copy));
1015
1016 std::vector<int> dewey(n + 1, -7);
1017 size_t len = 0;
1018 EXPECT_EQ(search_deway(root, n - 1, dewey.data(), dewey.size(), len), deepest);
1019 EXPECT_EQ(len, size_t(n));
1020 EXPECT_TRUE(std::all_of(dewey.begin(), dewey.begin() + n, [](int i) { return i == 0; }));
1021
1022 std::vector<int> path(n + 1, 0);
1023 path[n] = -1;
1024 EXPECT_EQ(deway_search(root, path.data(), path.size()), deepest);
1025
1026 auto *bin = forest_to_bin<Tree_Node<int>, BinNode<int>>(root);
1027 Tree_Node<int> *back = bin_to_forest<Tree_Node<int>, BinNode<int>>(bin);
1028 EXPECT_TRUE(are_tree_equal(root, back));
1029 destroy_forest(back);
1030 free_binary(bin);
1031
1033}
1034
1035// forest_to_bin() and bin_to_forest() recursed along the siblings too, as
1036// many as the children of a node
1037TEST(Tree_Node, the_conversions_of_a_wide_tree_need_no_recursion)
1038{
1039 constexpr int n = 1000000;
1040 Tree_Node<int> *root = star_of(n);
1041
1042 auto *bin = forest_to_bin<Tree_Node<int>, BinNode<int>>(root);
1043 Tree_Node<int> *back = bin_to_forest<Tree_Node<int>, BinNode<int>>(bin);
1044 EXPECT_TRUE(are_tree_equal(root, back));
1045 EXPECT_EQ(compute_height(back), 2u);
1046
1047 visit_sum = visit_count = 0;
1048 tree_preorder_traversal(back, sum_visit);
1049 EXPECT_EQ(visit_count, n);
1050 EXPECT_EQ(visit_sum, long(n) * (n - 1)); // keys, levels and positions
1051
1052 destroy_tree(back);
1053 free_binary(bin);
1055}
1056
1057// The operations of FunctionalMixin did not compile on a Tree_Node: the
1058// traversal gave them a const Tree_Node *
1059TEST(Tree_Node, the_functional_operations_take_the_nodes)
1060{
1061 auto *root = new Tree_Node<int>(1);
1062 root->insert_rightmost_child(new Tree_Node<int>(2));
1063 root->insert_rightmost_child(new Tree_Node<int>(3));
1064 root->get_left_child()->insert_rightmost_child(new Tree_Node<int>(4));
1065
1066 // in preorder: 1, 2, 4, 3
1067 EXPECT_EQ(root->foldl<long>(0, [](long acc, Tree_Node<int> *p) { return acc * 10 + p->get_key(); }),
1068 1243);
1069 EXPECT_TRUE(root->exists([](Tree_Node<int> *p) { return p->get_key() == 4; }));
1070 EXPECT_TRUE(root->all([](Tree_Node<int> *p) { return p->get_key() > 0; }));
1071 EXPECT_EQ(root->length(), 4u);
1072
1073 int sum = 0;
1074 root->for_each([&sum](Tree_Node<int> *p) { sum += p->get_key(); });
1075 EXPECT_EQ(sum, 10);
1076 EXPECT_EQ(root->maps<int>([](Tree_Node<int> *p) { return p->get_key(); }).size(), 4u);
1077 EXPECT_EQ(root->filter([](Tree_Node<int> *p) { return p->get_key() % 2 == 1; }).size(), 2u);
1078
1079 const Tree_Node<int> *c = root;
1080 EXPECT_TRUE(c->traverse([](const Tree_Node<int> *p) { return p->get_key() < 10; }));
1081
1083}
1084
1085namespace
1086{
1087 // A key whose copies can throw, and that counts those alive
1088 struct Counted_Key
1089 {
1090 static inline int alive = 0;
1091 static inline int copies_left = 0; // the copy that throws, when it gets to 0
1092
1093 int value = 0;
1094
1095 Counted_Key(int v = 0) : value(v) { ++alive; }
1096
1097 Counted_Key(const Counted_Key &other) : value(other.value)
1098 {
1099 if (copies_left > 0 and --copies_left == 0)
1100 throw std::runtime_error("the copy of a key");
1101 ++alive;
1102 }
1103
1104 Counted_Key &operator=(const Counted_Key &) = default;
1105
1106 ~Counted_Key() { --alive; }
1107 };
1108}
1109
1110// clone_tree(), forest_to_bin() and bin_to_forest() lost what they had made
1111// when the copy of a key threw
1112TEST(Tree_Node, the_copies_of_a_tree_leave_nothing_when_a_key_throws)
1113{
1115 using Bin = BinNode<Counted_Key>;
1116
1117 std::mt19937 gen(3);
1118 std::vector<Node *> nodes{new Node(Counted_Key(0))};
1119 for (int i = 1; i < 200; ++i)
1120 {
1121 auto *q = new Node(Counted_Key(i));
1122 nodes[gen() % nodes.size()]->insert_rightmost_child(q);
1123 nodes.push_back(q);
1124 }
1125 Node *root = nodes[0];
1126 const int before = Counted_Key::alive;
1127
1128 for (const int k : {1, 2, 50, 200})
1129 {
1130 Counted_Key::copies_left = k;
1131 EXPECT_THROW((void) clone_tree(root), std::runtime_error);
1132 EXPECT_EQ(Counted_Key::alive, before);
1133
1134 Counted_Key::copies_left = k;
1135 EXPECT_THROW((void) (forest_to_bin<Node, Bin>(root)), std::runtime_error);
1136 EXPECT_EQ(Counted_Key::alive, before);
1137 }
1138
1139 Counted_Key::copies_left = 0;
1140 Bin *bin = forest_to_bin<Node, Bin>(root);
1141 const int with_bin = Counted_Key::alive;
1142 for (const int k : {1, 2, 50, 200})
1143 {
1144 Counted_Key::copies_left = k;
1145 EXPECT_THROW((void) (bin_to_forest<Node, Bin>(bin)), std::runtime_error);
1146 EXPECT_EQ(Counted_Key::alive, with_bin);
1147 }
1148
1149 Counted_Key::copies_left = 0;
1150 free_binary(bin);
1152 EXPECT_EQ(Counted_Key::alive, 0);
1153}
1154
1155// get_parent() goes at once from the last child to the first, around the
1156// circle of the siblings
1157TEST(Tree_Node, get_parent_of_every_child)
1158{
1159 std::mt19937 gen(11);
1160 std::vector<Tree_Node<int> *> nodes{new Tree_Node<int>(0)};
1161 std::vector<Tree_Node<int> *> parents{nullptr};
1162 for (int i = 1; i < 3000; ++i)
1163 {
1164 auto *q = new Tree_Node<int>(i);
1165 Tree_Node<int> *p = nodes[gen() % nodes.size()];
1166 if (gen() % 3 == 0)
1168 else
1170 nodes.push_back(q);
1171 parents.push_back(p);
1172 }
1173
1174 for (size_t i = 0; i < nodes.size(); ++i)
1175 EXPECT_EQ(nodes[i]->get_parent(), parents[i]);
1176
1177 destroy_tree(nodes[0]);
1178}
String manipulation utilities.
Zip iterators and functional operations for multiple containers.
WeightedDigraph::Node Node
size_t size_t int32_t value
Definition ca-c-api.h:116
Node for binary search tree.
Doubly-linked list (defined in tpl_dynList.H).
Definition htlist.H:1231
T & append(const T &item)
Definition htlist.H:1362
size_t size() const noexcept
Return the number of items of the list, in constant time.
Definition htlist.H:1264
bool has_curr() const noexcept
Definition htlist.H:969
Iterator that zips two other iterators.
auto get_curr() const
Get current pair (bounds-checked).
Iterator over the children of this.
Preorder iterator over a tree rooted at a Tree_Node.
bool has_curr() const noexcept
Tree_Node * get_curr() const
Forward declaration used by CRTP helpers before the full node definition.
constexpr bool is_leftmost() const noexcept
Returns true if this is the leftmost node among its siblings.
bool traverse(Operation op)
Preorder traversal over all nodes executing op.
Dlink * get_sibling_list() noexcept
Returns the embedded sibling-list link.
void insert_left_sibling(Tree_Node *p)
Inserts p as the left sibling of this.
Tree_Node * get_left_sibling() const noexcept
Returns the left sibling of this.
constexpr bool is_root() const noexcept
Returns true if this is the root of the general tree.
Tree_Node * get_left_child() const noexcept
Returns the leftmost child of this.
constexpr bool is_rightmost() const noexcept
Returns true if this is the rightmost node among its siblings.
Tree_Node * get_child(const size_t i) const noexcept
Returns the i-th child of this.
void insert_leftmost_child(Tree_Node *p) noexcept
Inserts p as the leftmost child of this.
constexpr bool is_leaf() const noexcept
Returns true if this is a leaf node.
Dlink * get_child_list() noexcept
Returns the embedded child-list link.
Tree_Node * get_right_sibling() const noexcept
Returns the right sibling of this.
Tree_Node * get_left_tree() const noexcept
Returns the tree to the left of this.
void insert_rightmost_child(Tree_Node *p) noexcept
Inserts p as the rightmost child of this.
Tree_Node * get_right_tree() const noexcept
Returns the tree to the right of this.
void insert_tree_to_right(Tree_Node *tree)
Insert tree to the right of this
Tree_Node * get_parent() const noexcept
Returns the parent of this.
Container< Tree_Node * > children_nodes() const
Returns a list with the child nodes of this.
Tree_Node * get_right_child() const noexcept
Returns the rightmost child of this.
void insert_right_sibling(Tree_Node *p) noexcept
Inserts p to the right of this node.
T & get_key() noexcept
Returns a modifiable reference to the node contents.
Iterator get_it() const
Container< T > children() const
Returns a list with the contents of the children of this.
auto get_it()
Return a properly initialized iterator positioned at the first item on the container.
Definition ah-dry.H:663
void deway(Tree_Node< int > *p, int prefix[], const int &len, const size_t &dim)
Recursively compute and print Deway numbering for a tree node.
Definition deway.C:118
#define TEST(name)
__gmp_expr< T, __gmp_binary_expr< __gmp_expr< T, U >, unsigned long int, __gmp_root_function > > root(const __gmp_expr< T, U > &expr, unsigned long int l)
Definition gmpfrxx.h:4138
DynArray< Graph::Node * > nodes
Definition graphpic.C:406
void destroy_tree(Node *root)
Destroys (frees memory) the tree whose root is root.
bool traverse(Node *root, Op op)
See infix_traverse().
size_t compute_height(Node *root)
Computes the height of the tree root.
void tree_postorder_traversal(Node *root, void(*visitFct)(Node *, int, int))
Postorder traversal of a tree.
static void clone_tree(Node *src, Node *tgt)
Copies the subtrees of the children of src as children of tgt.
bool are_tree_equal(Node *t1, Node *t2, Eq &eq)
Returns true if t1 is equal to t2.
void destroy_forest(Node *root)
Destroys (frees memory) the forest whose first tree is root.
void tree_preorder_traversal(Node *root, void(*visitFct)(Node *, int, int))
Preorder traversal of a tree.
Node * search_deway(Node *root, const typename Node::key_type &key, int deway[], const size_t &size, size_t &n)
Searches key in a forest and computes the Dewey number of the node containing the key.
Node * deway_search(Node *root, int path[], const size_t &size)
Returns a node of a forest given its Dewey number.
Main namespace for Aleph-w library functions.
bool eq(const C1 &c1, const C2 &c2, Eq e=Eq())
Check equality of two containers using a predicate.
void zip_for_each(Op &&op, const Cs &...cs)
Apply op to every zipped tuple.
Definition ah-zip.H:471
Itor2 copy(Itor1 sourceBeg, const Itor1 &sourceEnd, Itor2 destBeg)
Copy elements from one range to another.
Definition ahAlgo.H:584
and
Check uniqueness with explicit hash + equality functors.
std::ostream & join(const C &c, const std::string &sep, std::ostream &out)
Join elements of an Aleph-style container into a stream.
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
Definition ahAlgo.H:127
T sum(const Container &container, const T &init=T{})
Compute sum of all elements.
double add(double a, double b)
STL namespace.
#define RLINK(i, n)
#define LLINK(i, n)
static int * k
Gsl_Compatible_Rng r
Basic binary tree node definitions.
General tree (n-ary tree) node.
#define IS_UNIQUE_SIBLING(p)
DynList< int > l
TEST_F(Simple_Tree, Iterators)
Definition tree-node.cc:757