u01: Add complete tree creation

This commit is contained in:
Simon Bruder 2023-04-21 23:56:14 +02:00
parent 8ea29904ea
commit 3e63e632b7
4 changed files with 69 additions and 0 deletions

View file

@ -1,6 +1,8 @@
#include "node.h"
int main(int argc, char **argv) {
node *t = create_complete_tree(2, 4);
delete t;
node *root = new node("root");
root->add_child(new node("left child"));
root->add_child(new node("right child"));

View file

@ -31,3 +31,14 @@ unsigned int node::get_nr_children() const { return children.size(); }
node *node::get_child(std::size_t i) const { return children[i]; }
void node::add_child(node *child) { children.push_back(child); }
node *create_complete_tree(std::size_t nr_child_nodes,
unsigned int tree_depth) {
node *n = new node();
if (tree_depth > 1) {
for (std::size_t i = 0; i < nr_child_nodes; i++) {
n->add_child(create_complete_tree(nr_child_nodes, tree_depth - 1));
}
}
return n;
}

View file

@ -17,3 +17,5 @@ private:
std::vector<node *> children;
static unsigned int node_id;
};
node *create_complete_tree(std::size_t nr_child_nodes, unsigned int tree_depth);

View file

@ -60,3 +60,57 @@ TEST_CASE("Global node counting") {
REQUIRE(n->get_child(5)->get_name() == "explicit name");
delete n;
}
TEST_CASE("Create complete tree") {
const std::size_t nr_child_nodes = 2;
const unsigned int tree_depth = 4;
node *t = create_complete_tree(nr_child_nodes, tree_depth);
const int node_id_start = get_node_id(t);
int count = 0;
REQUIRE(t->get_name() == "node_" + std::to_string(node_id_start + count++));
// this is very verbose,
// but without a serialisation,
// there is no other good way I could find (that does not also implement the
// logic to test).
REQUIRE(t->get_child(0)->get_name() ==
"node_" + std::to_string(node_id_start + count++));
REQUIRE(t->get_child(0)->get_child(0)->get_name() ==
"node_" + std::to_string(node_id_start + count++));
REQUIRE(t->get_child(0)->get_child(0)->get_child(0)->get_name() ==
"node_" + std::to_string(node_id_start + count++));
REQUIRE(t->get_child(0)->get_child(0)->get_child(1)->get_name() ==
"node_" + std::to_string(node_id_start + count++));
REQUIRE(t->get_child(0)->get_child(1)->get_name() ==
"node_" + std::to_string(node_id_start + count++));
REQUIRE(t->get_child(0)->get_child(1)->get_child(0)->get_name() ==
"node_" + std::to_string(node_id_start + count++));
REQUIRE(t->get_child(0)->get_child(1)->get_child(1)->get_name() ==
"node_" + std::to_string(node_id_start + count++));
REQUIRE(t->get_child(1)->get_name() ==
"node_" + std::to_string(node_id_start + count++));
REQUIRE(t->get_child(1)->get_child(0)->get_name() ==
"node_" + std::to_string(node_id_start + count++));
REQUIRE(t->get_child(1)->get_child(0)->get_child(0)->get_name() ==
"node_" + std::to_string(node_id_start + count++));
REQUIRE(t->get_child(1)->get_child(0)->get_child(1)->get_name() ==
"node_" + std::to_string(node_id_start + count++));
REQUIRE(t->get_child(1)->get_child(1)->get_name() ==
"node_" + std::to_string(node_id_start + count++));
REQUIRE(t->get_child(1)->get_child(1)->get_child(0)->get_name() ==
"node_" + std::to_string(node_id_start + count++));
REQUIRE(t->get_child(1)->get_child(1)->get_child(1)->get_name() ==
"node_" + std::to_string(node_id_start + count++));
REQUIRE(t->get_nr_children() == nr_child_nodes);
REQUIRE(t->get_child(0)->get_nr_children() == nr_child_nodes);
REQUIRE(t->get_child(0)->get_child(0)->get_nr_children() == nr_child_nodes);
REQUIRE(t->get_child(0)->get_child(1)->get_nr_children() == nr_child_nodes);
REQUIRE(t->get_child(1)->get_nr_children() == nr_child_nodes);
REQUIRE(t->get_child(1)->get_child(0)->get_nr_children() == nr_child_nodes);
REQUIRE(t->get_child(1)->get_child(1)->get_nr_children() == nr_child_nodes);
delete t;
}