From ee1cc0816a2b429952562953d1e1d3768d4c741a Mon Sep 17 00:00:00 2001 From: Aryadev Chavali Date: Fri, 26 Jul 2024 01:46:31 +0100 Subject: Queue based iteration procedure Pops an item off the queue and generate left and right children for it, if those are empty. Then push those children into the queue for the next iteration. NOTE: Because we're using a queue, this does a breadth first generation of the tree, which is what we want. --- src/main.cpp | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) (limited to 'src') diff --git a/src/main.cpp b/src/main.cpp index 680a69b..e89a418 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -17,6 +17,7 @@ #include #include +#include #include #include #include @@ -86,6 +87,31 @@ struct Node }; std::vector nodes; +std::queue to_iterate; + +void iterate(void) +{ + if (to_iterate.empty()) + return; + Node *node = to_iterate.front(); + to_iterate.pop(); + if (!node->left) + { + Node new_node = Fraction{node->value.numerator, + node->value.numerator + node->value.denominator}; + nodes.push_back(new_node); + node->left = nodes.data() + (nodes.size() - 1); + } + else if (!node->right) + { + Node new_node = Fraction{node->value.numerator + node->value.denominator, + node->value.denominator}; + nodes.push_back(new_node); + node->right = nodes.data() + (nodes.size() - 1); + } + to_iterate.push(node->left); + to_iterate.push(node->right); +} int main(void) { -- cgit v1.2.3-13-gbd6f