38# include <gtest/gtest.h>
65 memset(buf, 0x5a,
sizeof(buf));
67 EXPECT_EQ(p->get_key(), 0);
68 EXPECT_TRUE(p->is_root());
69 EXPECT_TRUE(p->is_leaf());
70 p->~Tree_Node<
long>();
73 EXPECT_EQ(q.get_key(),
nullptr);
96 ASSERT_TRUE(p.
children().is_empty());
98 ASSERT_TRUE(p.
traverse([] (
auto) { return true; }));
99 ASSERT_FALSE(p.
traverse([] (
auto) { return false; }));
104 ASSERT_NO_THROW(it.
next());
106 ASSERT_THROW(it.
get_curr(), overflow_error);
107 ASSERT_THROW(it.
next(), overflow_error);
111 ASSERT_NO_THROW(it.
next());
113 ASSERT_THROW(it.
get_curr(), overflow_error);
114 ASSERT_THROW(it.
next(), overflow_error);
118 ASSERT_THROW(cit.
get_curr(), overflow_error);
119 ASSERT_THROW(cit.
next(), overflow_error);
131 root.insert_rightmost_child(&left);
132 root.insert_rightmost_child(&right);
137 ASSERT_EQ(
root.get_parent(),
nullptr);
145TEST(
Tree_Node, insert_leftmost_child_preserves_existing_subtree_parent_links)
153 root.insert_rightmost_child(&old_left);
154 root.insert_rightmost_child(&old_right);
157 root.insert_leftmost_child(&new_left);
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);
167 ASSERT_EQ(old_left_child.
get_parent(), &old_left);
184 root.insert_rightmost_child(&old_left);
185 root.insert_rightmost_child(&right);
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);
198 ASSERT_EQ(old_left_child.
get_parent(), &old_left);
216 auto * forest = bin_to_forest<Tree_Node<int>,
BinNode<int>>(&n1);
217 ASSERT_NE(forest,
nullptr);
220 for (
auto * t = forest; t !=
nullptr; t = t->get_right_sibling(), ++
count)
222 EXPECT_TRUE(t->is_root()) <<
"tree key " << t->get_key()
223 <<
" must remain a forest root";
224 EXPECT_EQ(t->get_parent(),
nullptr);
260 root.insert_rightmost_child(&only_child);
264 root.insert_rightmost_child(&other_child);
281 root->insert_rightmost_child(child1);
282 root->insert_rightmost_child(child2);
288 ASSERT_EQ(
root->get_left_child(), child1);
289 ASSERT_EQ(
root->get_right_child(), child1);
309 root->insert_rightmost_child(a);
310 root->insert_rightmost_child(b);
311 root->insert_rightmost_child(c);
315 EXPECT_TRUE(b->is_rightmost());
316 EXPECT_EQ(b->get_right_sibling(),
nullptr);
333 root->insert_rightmost_child(a);
334 root->insert_rightmost_child(b);
335 root->insert_rightmost_child(c);
339 EXPECT_TRUE(b->is_leftmost());
340 EXPECT_EQ(b->get_left_sibling(),
nullptr);
341 EXPECT_EQ(
root->get_left_child(), b);
360 root->insert_rightmost_child(a);
361 root->insert_rightmost_child(b);
363 b->insert_rightmost_child(b1);
364 b->insert_rightmost_child(b2);
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);
385 root->insert_rightmost_child(a);
390 EXPECT_TRUE(
root->is_leaf());
391 EXPECT_EQ(
root->get_left_child(),
nullptr);
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);
407 root->insert_rightmost_child(a);
408 root->insert_rightmost_child(b);
409 root->insert_rightmost_child(c);
411 b->get_left_child()->insert_rightmost_child(
new Tree_Node<int>(211));
415 EXPECT_EQ(a->get_right_sibling(), c);
416 EXPECT_EQ(c->get_left_sibling(), a);
417 EXPECT_EQ(c->get_parent(),
root);
427 t1->insert_right_sibling(t2);
428 t2->insert_right_sibling(t3);
433 EXPECT_EQ(t1->get_right_tree(), t3);
434 EXPECT_EQ(t3->get_left_tree(), t1);
437 EXPECT_TRUE(t3->is_leftmost());
438 EXPECT_EQ(t3->get_left_tree(),
nullptr);
447 for (
int i = 1; i < 1000000; ++i)
450 last->insert_rightmost_child(p);
464 std::map<Model_Node *, Model_Node *> parent;
465 std::map<Model_Node *, std::vector<Model_Node *>> children;
467 void add(Model_Node *p, Model_Node *par)
474 void erase(Model_Node *p)
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())
481 auto *q = stack.back();
483 for (
auto *c : children[q])
492 void expect_model(Model_Node *
root,
const Tree_Model &model)
494 for (
const auto &[p, kids] : model.children)
498 ASSERT_EQ(p->get_parent(), model.parent.at(p));
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())
504 ASSERT_EQ(seen, kids);
505 ASSERT_EQ(p->is_leaf(), kids.empty());
506 if (not kids.empty())
508 ASSERT_EQ(p->get_right_child(), kids.back());
510 for (
size_t i = 0; i < kids.size(); ++i)
512 ASSERT_EQ(kids[i]->is_leftmost(), i == 0);
513 ASSERT_EQ(kids[i]->is_rightmost(), i + 1 == kids.size());
525 for (
int round = 0; round < 20; ++round)
529 auto *
root =
new Model_Node(key++);
530 model.add(
root,
nullptr);
531 for (
int op = 0; op < 300; ++op)
533 std::vector<Model_Node *>
nodes;
534 for (
const auto &kv : model.parent)
535 nodes.push_back(kv.first);
537 if (p ==
root or gen() % 10 < 6)
539 auto *q =
new Model_Node(key++);
540 const auto how = p ==
root ? gen() % 2 : gen() % 4;
543 auto &kids = model.children[p];
546 p->insert_rightmost_child(q);
551 p->insert_leftmost_child(q);
552 kids.insert(kids.begin(), q);
558 auto *par = model.parent[p];
559 auto &kids = model.children[par];
560 auto pos = std::find(kids.begin(), kids.end(), p);
563 p->insert_left_sibling(q);
568 p->insert_right_sibling(q);
569 kids.insert(pos + 1, q);
578 expect_model(
root, model);
579 if (HasFatalFailure())
697 ASSERT_TRUE(p1.
traverse([&
k] (
auto p) { return p->get_key() == ++k; }));
701 { return p->get_key() == ++k; }));
704 ASSERT_TRUE(p1.
children().traverse([&
k] (
auto i) { return i == ++k; }));
713 ASSERT_THROW(it.
get_curr(), overflow_error);
714 ASSERT_THROW(it.
next(), overflow_error);
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);
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);
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);
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());
775 ASSERT_NE(rootp,
nullptr);
776 ASSERT_EQ(
root.get_key(), rootp->get_key());
784 ASSERT_TRUE(
root->level_traverse([&i] (
auto p)
786 return p->get_key() == i++;
799 ASSERT_EQ(p.first->get_key(), p.second->get_key());
808 ASSERT_EQ(
root,
nullptr);
813 ASSERT_TRUE(
root.traverse([&
k] (
auto p) { k++; return p->get_key() == 5; }));
822 auto * t1 =
new Node(
"root");
823 t1->insert_rightmost_child(
new Node(
"A"));
824 t1->insert_rightmost_child(
new Node(
"B"));
826 auto * t2 =
new Node(
"ROOT");
827 t2->insert_rightmost_child(
new Node(
"a"));
828 t2->insert_rightmost_child(
new Node(
"b"));
830 auto nocase = [] (
const std::string & a,
const std::string & b)
832 if (a.size() != b.size())
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])))
851 auto ret =
root->traverse([&it, &
k] (
auto p)
853 bool r = p->get_key() == it.get_curr();
866 ASSERT_EQ(p->get_key(), 14);
887 t1->insert_tree_to_right(t3);
888 t1->insert_tree_to_right(t2);
893 zip_for_each([] (
auto t) { ASSERT_EQ(get<0>(t), get<1>(t)); }, tlist, flist);
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 };
910 t->level_traverse([&order] (
auto p) { order.
append(p->get_key());
return true; });
912 ASSERT_TRUE(
eq(
l, order));
928 for (
int i = 1; i < n; ++i)
931 last->insert_rightmost_child(p);
941 for (
int i = 1; i < n; ++i)
956 template <
class BNode>
957 void free_binary(BNode *
root)
959 while (
root != BNode::NullPtr)
976 long visit_count = 0;
980 visit_sum += long(p->
get_key()) + level + pos;
989 constexpr int n = 1000000;
997 const long sum = 2 * (long(n) * (n - 1) / 2);
998 visit_sum = visit_count = 0;
1000 EXPECT_EQ(visit_count, n);
1001 EXPECT_EQ(visit_sum,
sum);
1003 visit_sum = visit_count = 0;
1005 EXPECT_EQ(visit_count, n);
1006 EXPECT_EQ(visit_sum,
sum);
1012 deepest_of(
copy)->get_key() = -1;
1016 std::vector<int> dewey(n + 1, -7);
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; }));
1022 std::vector<int> path(n + 1, 0);
1039 constexpr int n = 1000000;
1047 visit_sum = visit_count = 0;
1049 EXPECT_EQ(visit_count, n);
1050 EXPECT_EQ(visit_sum,
long(n) * (n - 1));
1067 EXPECT_EQ(
root->foldl<
long>(0, [](
long acc,
Tree_Node<int> *p) { return acc * 10 + p->get_key(); }),
1071 EXPECT_EQ(
root->length(), 4u);
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);
1090 static inline int alive = 0;
1091 static inline int copies_left = 0;
1095 Counted_Key(
int v = 0) :
value(v) { ++alive; }
1097 Counted_Key(
const Counted_Key &other) :
value(other.
value)
1099 if (copies_left > 0
and --copies_left == 0)
1100 throw std::runtime_error(
"the copy of a key");
1104 Counted_Key &operator=(
const Counted_Key &) =
default;
1106 ~Counted_Key() { --alive; }
1117 std::mt19937 gen(3);
1118 std::vector<Node *>
nodes{
new Node(Counted_Key(0))};
1119 for (
int i = 1; i < 200; ++i)
1121 auto *q =
new Node(Counted_Key(i));
1122 nodes[gen() %
nodes.size()]->insert_rightmost_child(q);
1126 const int before = Counted_Key::alive;
1128 for (
const int k : {1, 2, 50, 200})
1130 Counted_Key::copies_left =
k;
1132 EXPECT_EQ(Counted_Key::alive, before);
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);
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})
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);
1149 Counted_Key::copies_left = 0;
1152 EXPECT_EQ(Counted_Key::alive, 0);
1159 std::mt19937 gen(11);
1161 std::vector<Tree_Node<int> *> parents{
nullptr};
1162 for (
int i = 1; i < 3000; ++i)
1171 parents.push_back(p);
1174 for (
size_t i = 0; i <
nodes.size(); ++i)
1175 EXPECT_EQ(
nodes[i]->get_parent(), parents[i]);
String manipulation utilities.
Zip iterators and functional operations for multiple containers.
WeightedDigraph::Node Node
size_t size_t int32_t value
Node for binary search tree.
constexpr bool is_empty() const noexcept
Return true if this (as header node) is empty.
Doubly-linked list (defined in tpl_dynList.H).
T & append(const T &item)
size_t size() const noexcept
Return the number of items of the list, in constant time.
bool has_curr() const noexcept
Iterator that zips two other iterators.
auto get_curr() const
Get current pair (bounds-checked).
Iterator over the children of this.
Tree_Node * get_curr() const
bool has_curr() const noexcept
Preorder iterator over a tree rooted at a Tree_Node.
void reset_first() noexcept
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.
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.
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.
__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)
DynArray< Graph::Node * > nodes
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.
Itor2 copy(Itor1 sourceBeg, const Itor1 &sourceEnd, Itor2 destBeg)
Copy elements from one range to another.
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.
T sum(const Container &container, const T &init=T{})
Compute sum of all elements.
double add(double a, double b)
Basic binary tree node definitions.
General tree (n-ary tree) node.
#define IS_UNIQUE_SIBLING(p)
TEST_F(Simple_Tree, Iterators)