File size: 9,490 Bytes
2492322 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 | // Copyright (c) 2023, QuantStack and Mamba Contributors
//
// Distributed under the terms of the BSD 3-Clause License.
//
// The full license is in the file LICENSE, distributed with this software.
#ifndef MAMBA_UTIL_FLAT_BINARY_TREE_HPP
#define MAMBA_UTIL_FLAT_BINARY_TREE_HPP
#include <utility>
#include <variant>
#include <vector>
namespace mamba::util
{
/**
* In array binary tree.
*
* A binary tree, each node is either a leaf, or a node with exactly two children.
* This data structure is light and nothing prevents the user from representing
* any kind of binary directed acyclic graph (e.g. there can be multiple trees,
* or nodes could have multiple parents)
*
* For efficiency (and simplicity), this data structure can currently only grow.
* The tree must also be grown from the leaves, adding children first and their
* parents afterwards.
*/
template <typename Branch, typename Leaf>
class flat_binary_tree
{
public:
using branch_type = Branch;
using leaf_type = Leaf;
struct branch_node
{
branch_type data;
std::size_t left_child = 0;
std::size_t right_child = 0;
// TODO(C++20): replace by the `= default` implementation of `operator==`
[[nodiscard]] auto operator==(const branch_node& other) const -> bool
{
return data == other.data //
&& left_child == other.left_child //
&& right_child == other.right_child;
}
[[nodiscard]] auto operator!=(const branch_node& other) const -> bool
{
return !(*this == other);
}
};
using leaf_node = leaf_type;
using node_type = std::variant<branch_node, leaf_node>;
using node_list = std::vector<node_type>;
using size_type = typename node_list::size_type;
using idx_type = size_type;
[[nodiscard]] auto size() const -> size_type;
[[nodiscard]] auto empty() const -> bool;
/** Remove all nodes. */
void clear();
/**
* Reserve (allocate) space for @p nodes.
*
* This improves the efficiency of ``add_leaf`` and ``add_branch`` but does not
* modify the tree in any way.
*/
void reserve(size_type size);
/**
* Add a node with no children.
*
* Return an ID that can be used to point to this node as a children in ``add_branch``.
*/
auto add_leaf(const leaf_type& leaf) -> idx_type;
auto add_leaf(leaf_type&& leaf) -> idx_type;
/**
* Add a node with exactly two children.
*
* The children must have been previously added to the tree and their IDs can be used
* to point to them.
*/
auto add_branch(const branch_type& branch, idx_type left_child, idx_type right_child)
-> idx_type;
auto add_branch(branch_type&& branch, idx_type left_child, idx_type right_child) -> idx_type;
[[nodiscard]] auto node(idx_type idx) const -> const node_type&;
[[nodiscard]] auto node(idx_type idx) -> node_type&;
[[nodiscard]] auto is_branch(idx_type idx) const -> bool;
[[nodiscard]] auto is_leaf(idx_type idx) const -> bool;
[[nodiscard]] auto leaf(idx_type idx) const -> const leaf_type&;
[[nodiscard]] auto leaf(idx_type idx) -> leaf_type&;
[[nodiscard]] auto branch(idx_type idx) const -> const branch_type&;
[[nodiscard]] auto branch(idx_type idx) -> branch_type&;
[[nodiscard]] auto left(idx_type idx) const -> idx_type;
[[nodiscard]] auto right(idx_type idx) const -> idx_type;
[[nodiscard]] auto root() const -> idx_type;
// TODO(C++20): replace by the `= default` implementation of `operator==`
[[nodiscard]] auto operator==(const flat_binary_tree& other) const -> bool
{
return m_nodes == other.m_nodes && m_root == other.m_root;
}
[[nodiscard]] auto operator!=(const flat_binary_tree& other) const -> bool
{
return !(*this == other);
}
template <typename Visitor>
void dfs_raw(Visitor&& visitor, idx_type start) const;
private:
node_list m_nodes;
idx_type m_root = 0;
template <typename L>
auto add_leaf_impl(L&& leaf) -> idx_type;
template <typename B>
auto add_branch_impl(B&& branch, idx_type left_child, idx_type right_child) -> idx_type;
};
/****************************************
* Implementation of flat_binary_tree *
****************************************/
template <typename B, typename L>
auto flat_binary_tree<B, L>::size() const -> size_type
{
return m_nodes.size();
}
template <typename B, typename L>
auto flat_binary_tree<B, L>::empty() const -> bool
{
return m_nodes.empty();
}
template <typename B, typename L>
auto flat_binary_tree<B, L>::node(idx_type idx) const -> const node_type&
{
return m_nodes.at(idx);
}
template <typename B, typename L>
auto flat_binary_tree<B, L>::node(idx_type idx) -> node_type&
{
return m_nodes.at(idx);
}
template <typename B, typename L>
auto flat_binary_tree<B, L>::is_branch(idx_type idx) const -> bool
{
return std::holds_alternative<branch_node>(node(idx));
}
template <typename B, typename L>
auto flat_binary_tree<B, L>::is_leaf(idx_type idx) const -> bool
{
return std::holds_alternative<leaf_node>(node(idx));
}
template <typename B, typename L>
auto flat_binary_tree<B, L>::leaf(idx_type idx) const -> const leaf_type&
{
return std::get<leaf_node>(node(idx));
}
template <typename B, typename L>
auto flat_binary_tree<B, L>::leaf(idx_type idx) -> leaf_type&
{
return std::get<leaf_node>(node(idx));
}
template <typename B, typename L>
auto flat_binary_tree<B, L>::branch(idx_type idx) const -> const branch_type&
{
return std::get<branch_node>(node(idx)).data;
}
template <typename B, typename L>
auto flat_binary_tree<B, L>::branch(idx_type idx) -> branch_type&
{
return std::get<branch_node>(node(idx)).data;
}
template <typename B, typename L>
auto flat_binary_tree<B, L>::left(idx_type idx) const -> idx_type
{
return std::get<branch_node>(node(idx)).left_child;
}
template <typename B, typename L>
auto flat_binary_tree<B, L>::right(idx_type idx) const -> idx_type
{
return std::get<branch_node>(node(idx)).right_child;
}
template <typename B, typename L>
auto flat_binary_tree<B, L>::root() const -> idx_type
{
return m_root;
}
template <typename B, typename L>
void flat_binary_tree<B, L>::clear()
{
return m_nodes.clear();
}
template <typename B, typename L>
void flat_binary_tree<B, L>::reserve(size_type size)
{
return m_nodes.reserve(size);
}
template <typename B, typename L>
template <typename Leaf>
auto flat_binary_tree<B, L>::add_leaf_impl(Leaf&& leaf) -> idx_type
{
m_nodes.emplace_back(std::forward<Leaf>(leaf));
return size() - 1;
}
template <typename B, typename L>
auto flat_binary_tree<B, L>::add_leaf(const leaf_type& leaf) -> idx_type
{
return add_leaf_impl(leaf);
}
template <typename B, typename L>
auto flat_binary_tree<B, L>::add_leaf(leaf_type&& leaf) -> idx_type
{
return add_leaf_impl(std::move(leaf));
}
template <typename B, typename L>
template <typename Branch>
auto
flat_binary_tree<B, L>::add_branch_impl(Branch&& branch, idx_type left_child, idx_type right_child)
-> idx_type
{
m_nodes.emplace_back(branch_node{ std::forward<Branch>(branch), left_child, right_child });
const auto idx = size() - 1;
if ((left_child == root()) || right_child == root())
{
m_root = idx;
}
return idx;
}
template <typename B, typename L>
auto
flat_binary_tree<B, L>::add_branch(const branch_type& branch, idx_type left_child, idx_type right_child)
-> idx_type
{
return add_branch_impl(branch, left_child, right_child);
}
template <typename B, typename L>
auto
flat_binary_tree<B, L>::add_branch(branch_type&& branch, idx_type left_child, idx_type right_child)
-> idx_type
{
return add_branch_impl(std::move(branch), left_child, right_child);
}
template <typename B, typename L>
template <typename Visitor>
void flat_binary_tree<B, L>::dfs_raw(Visitor&& visitor, idx_type start_idx) const
{
if (is_leaf(start_idx))
{
visitor.on_leaf(*this, start_idx);
}
else
{
const auto left_idx = left(start_idx);
const auto right_idx = right(start_idx);
visitor.on_branch_left_before(*this, start_idx, left_idx);
dfs_raw(visitor, left_idx);
visitor.on_branch_infix(*this, start_idx, left_idx, right_idx);
dfs_raw(visitor, right_idx);
visitor.on_branch_right_after(*this, start_idx, right_idx);
}
}
}
#endif
|