From 3e63e632b7a59555ff8d958b901a28cae0cedd75 Mon Sep 17 00:00:00 2001 From: Simon Bruder Date: Fri, 21 Apr 2023 23:56:14 +0200 Subject: [PATCH] u01: Add complete tree creation --- u01/main.cpp | 2 ++ u01/node.cpp | 11 +++++++++++ u01/node.h | 2 ++ u01/tests.cpp | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 69 insertions(+) diff --git a/u01/main.cpp b/u01/main.cpp index 3fa99de..c026832 100644 --- a/u01/main.cpp +++ b/u01/main.cpp @@ -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")); diff --git a/u01/node.cpp b/u01/node.cpp index 16e22f8..3be22e4 100644 --- a/u01/node.cpp +++ b/u01/node.cpp @@ -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; +} diff --git a/u01/node.h b/u01/node.h index 5659d42..1632899 100644 --- a/u01/node.h +++ b/u01/node.h @@ -17,3 +17,5 @@ private: std::vector children; static unsigned int node_id; }; + +node *create_complete_tree(std::size_t nr_child_nodes, unsigned int tree_depth); diff --git a/u01/tests.cpp b/u01/tests.cpp index 49ace8d..c0bbecc 100644 --- a/u01/tests.cpp +++ b/u01/tests.cpp @@ -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; +}