22// This file is part of the CPP-GL project (https://github.com/SpectraL519/cpp-gl).
33// Licensed under the MIT License. See the LICENSE file in the project root for full license information.
44
5+ // / @file gl/topology/binary_tree.hpp
6+ // / @brief Generators for regular and bidirectional binary tree topologies.
7+
58#pragma once
69
710#include " gl/constants.hpp"
@@ -15,7 +18,7 @@ namespace gl::topology {
1518
1619namespace detail {
1720
18- [[nodiscard]] gl_attr_force_inline auto get_binary_target_ids (traits::c_id_type auto source_id) {
21+ [[nodiscard]] gl_attr_force_inline auto get_bintree_target_ids (traits::c_id_type auto source_id) {
1922 using id_type = std::decay_t <decltype (source_id)>;
2023 return std::make_pair (
2124 static_cast <id_type>(2 ) * source_id + static_cast <id_type>(1 ),
@@ -27,6 +30,26 @@ constexpr size_type min_non_trivial_bin_tree_depth = 2uz;
2730
2831} // namespace detail
2932
33+ // / @ingroup GL GL-Topology
34+ // / @brief Generates a regular (regular) binary tree of a specified depth.
35+ // /
36+ // / A regular binary tree is a tree where all internal vertices have exactly two children
37+ // / and all leaf vertices are at the same depth. For a given depth \f$d\f$, the graph
38+ // / will contain exactly \f$2^d - 1\f$ vertices.
39+ // /
40+ // / If the requested `GraphType` is directed, edges are created pointing from the parent
41+ // / vertex to its children.
42+ // /
43+ // / > [!NOTE] Performance for Flat List Graphs
44+ // / >
45+ // / > If the requested `GraphType` satisfies [**c_flat_list_graph**](gl_concepts.md#gl-traits-c-flat-list-graph),
46+ // / > an optimized overload is automatically selected. It internally constructs a standard adjacency list graph
47+ // / > first, and then utilizes the @ref gl::to "to" conversion to flatten it. This is significantly faster than
48+ // / > inserting edges one-by-one into a flat representation.
49+ // /
50+ // / @tparam GraphType The target graph type to generate.
51+ // / @param depth The depth (number of levels) of the binary tree. A depth of `1` yields a single root vertex.
52+ // / @return A newly constructed graph representing the regular binary tree.
3053template <traits::c_graph GraphType>
3154[[nodiscard]] GraphType regular_binary_tree (size_type depth) {
3255 using id_type = typename GraphType::id_type;
@@ -44,7 +67,7 @@ template <traits::c_graph GraphType>
4467 const auto n_source_vertices = n_vertices - util::upow (base, i_end);
4568
4669 for (id_type source_id = initial_id; source_id < n_source_vertices; ++source_id) {
47- const auto target_ids = detail::get_binary_target_ids (source_id);
70+ const auto target_ids = detail::get_bintree_target_ids (source_id);
4871 graph.add_edges_from (
4972 source_id, std::initializer_list<id_type>{target_ids.first , target_ids.second }
5073 );
@@ -59,6 +82,25 @@ template <traits::c_flat_list_graph GraphType>
5982 return to<impl::flat_list_t >(regular_binary_tree<base_graph_type>(depth));
6083}
6184
85+ // / @ingroup GL GL-Topology
86+ // / @brief Generates a regular binary tree with bidirectional edges.
87+ // /
88+ // / For directed graphs, this function ensures that for every parent-to-child edge,
89+ // / a reciprocal child-to-parent edge is also created.
90+ // /
91+ // / For undirected graphs, this function simply falls back to @ref gl::topology::regular_binary_tree "regular_binary_tree",
92+ // / as undirected edges are inherently bidirectional.
93+ // /
94+ // / > [!NOTE] Performance for Flat List Graphs
95+ // / >
96+ // / > If the requested `GraphType` satisfies [**c_flat_list_graph**](gl_concepts.md#gl-traits-c-flat-list-graph),
97+ // / > an optimized overload is automatically selected. It internally constructs a standard adjacency list graph
98+ // / > first, and then utilizes the @ref gl::to "to" conversion to flatten it. This is significantly faster than
99+ // / > inserting edges one-by-one into a flat representation.
100+ // /
101+ // / @tparam GraphType The target graph type to generate.
102+ // / @param depth The depth of the binary tree.
103+ // / @return A newly constructed graph representing the bidirectional binary tree.
62104template <traits::c_graph GraphType>
63105[[nodiscard]] GraphType bidirectional_regular_binary_tree (size_type depth) {
64106 if constexpr (traits::c_directed_graph<GraphType>) {
@@ -77,7 +119,7 @@ template <traits::c_graph GraphType>
77119 const auto n_source_vertices = n_vertices - util::upow (base, i_end);
78120
79121 for (id_type source_id = initial_id; source_id < n_source_vertices; ++source_id) {
80- const auto target_ids = detail::get_binary_target_ids (source_id);
122+ const auto target_ids = detail::get_bintree_target_ids (source_id);
81123 graph.add_edges_from (
82124 source_id, std::initializer_list<id_type>{target_ids.first , target_ids.second }
83125 );
0 commit comments