62#define ISROOT(p) ((p)->is_root())
63#define ISLEAF(p) ((p)->is_leaf())
64#define ISLEFTMOST(p) ((p)->is_leftmost())
65#define ISRIGHTMOST(p) ((p)->is_rightmost())
67#define SIBLING_LIST(p) ((p)->get_sibling_list())
68#define CHILD_LIST(p) ((p)->get_child_list())
69#define SIBLING_LINK(p) ((p)->get_sibling_list())
70#define LCHILD(p) ((p)->get_left_child())
71#define RSIBLING(p) ((p)->get_right_sibling())
72#define IS_UNIQUE_SIBLING(p) (SIBLING_LIST(p)->is_empty())
76namespace tree_node_detail
143 template <
class Node>
175 [[nodiscard]]
constexpr const T &
get_key() const noexcept
186 [[nodiscard]]
constexpr const T &
get_data() const noexcept
226 [[nodiscard]]
constexpr bool is_root() const noexcept
232 [[nodiscard]]
constexpr bool is_leaf() const noexcept
367 for (
size_t j = 0; c !=
nullptr and j < i; ++j)
394 return p->upper_link();
413 assert(p->is_rightmost()
and p->is_leftmost()
and p->is_root()
and p->is_leaf());
421 p->set_is_root(
false);
422 p->set_is_leftmost(
false);
425 if (old_next_node !=
nullptr)
428 p->set_is_rightmost(
false);
433 p->set_is_rightmost(
true);
461 if (old_prev_node !=
nullptr)
477 assert(leaf !=
nullptr);
481 assert(
root !=
nullptr);
506 assert(p->is_rightmost()
and p->is_leftmost()
and p->is_root()
and p->is_leaf());
508 p->set_is_root(
false);
527 p->set_is_rightmost(
false);
528 assert(p->get_right_sibling() == old_left_child);
531 assert(p->is_leftmost());
545 assert(p->is_rightmost()
and p->is_leftmost()
and p->is_root()
and p->is_leaf());
547 p->set_is_root(
false);
558 p->set_is_leftmost(
false);
567 assert(tree !=
nullptr);
610 if (old_next_tree !=
nullptr)
659 template <
typename Operation>
666 template <
typename Operation>
669 for_each_child<Operation>(op);
701 template <
class Operation>
716 while (not p->is_leaf())
726 while (p->is_rightmost())
739 template <
class Operation>
748 template <
class Operation>
805 return curr !=
nullptr;
871 std::swap(
root, it.root);
872 std::swap(
curr, it.curr);
873 std::swap(
pos, it.pos);
909 return curr !=
nullptr;
927 if (lchild ==
nullptr)
1001template <
class Node>
1004 Node *s = src, *t = tgt;
1007 for (
Node *c = s->get_left_child(); c !=
nullptr; c = c->get_right_sibling())
1008 t->insert_rightmost_child(
new Node(c->get_key()));
1010 if (
Node *child = s->get_left_child())
1013 t = t->get_left_child();
1019 while (s != src
and s->is_rightmost())
1021 s = s->get_parent();
1022 t = t->get_parent();
1028 s = s->get_right_sibling();
1029 t = t->get_right_sibling();
1041template <
class Node>
1044 if (
root ==
nullptr)
1061namespace tree_node_detail
1065 template <
class Node>
1077template <
class Node>
1079 void (*visitFct)(
Node *,
int,
int))
1083 int depth = level, pos = child_index;
1086 (*visitFct)(p, depth, pos);
1087 if (
Node *child = p->get_left_child())
1098 while (p !=
root and p->is_rightmost())
1100 p = path.
top().node;
1101 pos = path.
top().index;
1109 p = p->get_right_sibling();
1139template <
class Node>
1172template <
class Node>
1179 assert(
root->is_root());
1188template <
class Node>
1190 void (*visitFct)(
Node *,
int,
int))
1194 int depth = level, pos = child_index;
1198 for (
Node *child = p->get_left_child(); child !=
nullptr; child = p->get_left_child())
1208 (*visitFct)(p, depth, pos);
1212 if (not p->is_rightmost())
1216 p = path.
top().node;
1217 pos = path.
top().index;
1222 p = p->get_right_sibling();
1251template <
class Node>
1283template <
class Node>
1290 for (;
root not_eq
nullptr;
root =
root->get_right_sibling())
1292 assert(
root->is_root());
1313template <
class Node,
class Eq>
1317 return t2 ==
nullptr;
1322 Node *p = t1, *q = t2;
1325 if (not
eq(p->get_key(), q->get_key()))
1328 Node *a = p->get_left_child(), *b = q->get_left_child();
1329 while (a !=
nullptr and b !=
nullptr)
1331 a = a->get_right_sibling();
1332 b = b->get_right_sibling();
1338 if (
Node *child = p->get_left_child())
1341 q = q->get_left_child();
1347 while (p != t1
and p->is_rightmost())
1349 p = p->get_parent();
1350 q = q->get_parent();
1356 p = p->get_right_sibling();
1357 q = q->get_right_sibling();
1361template <
class Node,
class Eq = std::equal_to<
typename Node::key_type>>
1364 return are_tree_equal<Node, Eq>(t1, t2,
eq);
1367namespace tree_node_detail
1380#if defined(__GNUC__) && !defined(__clang__) && __GNUC__ >= 12
1381# pragma GCC diagnostic push
1382# pragma GCC diagnostic ignored "-Wdangling-pointer"
1384 template <
class Node>
1393 auto *p =
static_cast<Node *
>(Base::sibling_to_Tree_Node(pending.
remove_next()));
1394 if (not p->is_leaf())
1403#if defined(__GNUC__) && !defined(__clang__) && __GNUC__ >= 12
1404# pragma GCC diagnostic pop
1413 template <
class Node>
1420 auto *parent =
root->get_parent();
1422 while (not leaf->is_leaf())
1423 leaf =
static_cast<Node *
>(leaf->get_left_child());
1430 parent->set_is_leaf(
true);
1433 auto *
next =
root->get_right_sibling();
1434 next->set_is_leftmost(
true);
1442 if (
root->is_rightmost())
1443 root->get_left_sibling()->set_is_rightmost(
true);
1444 if (
root->is_leftmost())
1445 root->get_right_sibling()->set_is_leftmost(
true);
1464template <
class Node>
1467 if (
root ==
nullptr)
1485template <
class Node>
1488 if (
root ==
nullptr)
1495 while (
root !=
nullptr)
1510template <
class Node>
1513 if (
root ==
nullptr)
1519 size_t depth = 1, height = 1;
1523 if (
auto *child =
static_cast<Node *
>(p->get_left_child()))
1526 if (++depth > height)
1533 while (p !=
root and p->is_rightmost())
1535 p =
static_cast<Node *
>(p->get_parent());
1542 p =
static_cast<Node *
>(p->get_right_sibling());
1547template <
class Node>
1550 for (
int level = idx; node !=
nullptr; ++level)
1554 if (path[level] < 0)
1558 Node *child = node->get_left_child();
1559 for (
int i = 0; i < path[level]
and child !=
nullptr; ++i)
1560 child = child->get_right_sibling();
1581template <
class Node>
1584 for (
int i = 0;
root !=
nullptr; i++,
root =
root->get_right_sibling())
1591template <
class Node,
class Equal>
1593 const size_t ¤t_level,
int deway[],
const size_t &
size,
1616template <
class Node,
class Equal = Aleph::equal_to<
typename Node::key_type>>
1618 const size_t &
size,
size_t &n)
1624 for (
int i = 0;
root !=
nullptr; i++,
root =
root->get_right_sibling())
1628 if (result !=
nullptr)
1639template <
class Node,
class Equal>
1641 const size_t ¤t_level,
int deway[],
const size_t &
size,
1646 if (
root ==
nullptr)
1650 size_t level = current_level;
1653 if (Equal()(p->get_key(), key))
1659 if (
Node *child = p->get_left_child())
1662 deway[level + 1] = 0;
1670 while (p !=
root and p->is_rightmost())
1672 p = p->get_parent();
1679 p = p->get_right_sibling();
1684namespace tree_node_detail
1687 template <
class TNode,
class BNode>
1696 template <
class BNode>
1699 while (
root != BNode::NullPtr)
1739template <
class TNode,
class BNode>
1742 if (
root ==
nullptr)
1743 return BNode::NullPtr;
1745 auto *result =
new BNode(
root->get_key());
1755 if (TNode *child = p->get_left_child())
1757 auto *bchild =
new BNode(child->get_key());
1768 p = path.
top().tnode;
1769 b = path.
top().bnode;
1773 if (p->is_rightmost())
1776 TNode *sibling = p->get_right_sibling();
1777 auto *bsibling =
new BNode(sibling->get_key());
1778 RLINK(b) = bsibling;
1790template <
class TNode,
class BNode>
1793 if (lnode == BNode::NullPtr)
1796 auto *child =
new TNode(
KEY(lnode));
1797 tree_node->insert_leftmost_child(child);
1800template <
class TNode,
class BNode>
1803 if (rnode == BNode::NullPtr)
1806 auto *sibling =
new TNode(
KEY(rnode));
1807 tree_node->insert_right_sibling(sibling);
1815template <
class TNode,
class BNode>
1818 if (broot == BNode::NullPtr)
1826 if (
LLINK(b) != BNode::NullPtr)
1830 t = t->get_left_child();
1836 while (
RLINK(b) == BNode::NullPtr)
1841 t = pending.
top().tnode;
1842 b = pending.
top().bnode;
1847 t = t->get_right_sibling();
1873template <
class TNode,
class BNode>
1876 if (broot == BNode::NullPtr)
1879 auto *troot =
new TNode(
KEY(broot));
CRTP Mixins for container functionality (DRY principle).
Exception handling system with formatted messages for Aleph-w.
#define ah_out_of_range_error_if(C)
Throws std::out_of_range if condition holds.
#define ah_overflow_error_if(C)
Throws std::overflow_error if condition holds.
#define ah_domain_error_if(C)
Throws std::domain_error if condition holds.
#define ah_range_error_if(C)
Throws std::range_error if condition holds.
DRY (Don't Repeat Yourself) utilities and macros.
Standard functor implementations and comparison objects.
Functional programming utilities for Aleph-w containers.
Iterator traits and STL-compatible iterator wrappers.
#define STL_ALEPH_ITERATOR(Set_Name)
WeightedDigraph::Node Node
size_t size_t int32_t value
Stack of the frames of a depth-first search that does not recurse.
T & push(Args &&...args)
Builds a frame on top of the stack.
bool is_empty() const noexcept
Whether the stack has no frames.
T & top() noexcept
The top frame; the stack must not be empty.
void pop() noexcept
Destroys the top frame; the stack must not be empty.
Doubly linked circular list node.
void wrap_header(Dlink *l) noexcept
Wrap a header to a list (without header).
constexpr bool is_empty() const noexcept
Return true if this (as header node) is empty.
void append(Dlink *node) noexcept
Insert node before this.
Dlink * del() noexcept
Remove this from the list. this must not be a header node.
void insert(Dlink *node) noexcept
Insert node after this.
Dlink *& get_prev() const noexcept
Return the link that is before this
Dlink cut_list(Dlink *link) noexcept
Cut this from link.
Dlink *& get_next() const noexcept
Return the link that is after this
void concat_list(Dlink *head) noexcept
Concatenate list head to list this
Dlink * remove_next() noexcept
Remove the item that is after this
void splice(Dlink *l) noexcept
Insert a list l without header node after the node this.
Dynamic queue of elements of generic type T based on single linked list.
T & put(const T &data)
The type of element.
T get()
Remove the oldest item of the queue.
bool is_empty() const noexcept
Return true if this is empty.
Dynamic stack of elements of generic type T based on a singly linked list.
Doubly-linked list (defined in tpl_dynList.H).
CRTP Mixin providing functional programming operations.
and
Transform elements using a mapping function.
Iterator over the children of this.
Tree_Node * get_curr() const
Children_Iterator(const Children_Iterator &it) noexcept
Children_Iterator(Tree_Node &p) noexcept
Children_Iterator(const Tree_Node &p) noexcept
Children_Iterator(Tree_Node *p) noexcept
Tree_Node * get_curr_ne() const noexcept
bool has_curr() const noexcept
Preorder iterator over a tree rooted at a Tree_Node.
void reset_first() noexcept
Iterator(const Iterator &it)
bool has_curr() const noexcept
Iterator(Iterator &&it) noexcept
Iterator & operator=(Iterator it)
void swap(Iterator &it) noexcept
size_t get_pos() const
Return the current position of the iterator. Only valid if.
Iterator(Tree_Node *r=nullptr) noexcept
Tree_Node * get_curr() const
DynListStack< Tree_Node * > s
Iterator(Tree_Node &root)
Tree_Node * get_curr_ne() const noexcept
Forward declaration used by CRTP helpers before the full node definition.
Tree_Node(const T &d)
Constructor with data value __data.
constexpr bool is_leftmost() const noexcept
Returns true if this is the leftmost node among its siblings.
bool traverse(Operation op)
Preorder traversal over all nodes executing op.
void set_is_root(bool value) noexcept
Sets the root flag.
Dlink * get_sibling_list() noexcept
Returns the embedded sibling-list link.
Tree_Node * left_link() const noexcept
Tree_Node * upper_link() const noexcept
static Tree_Node * sibling_to_Tree_Node(Dlink *link) noexcept
Tree_Node * join(Tree_Node *tree)
join tree as subtree of root this
Tree_Node * get_last_tree() const
Returns the rightmost tree of the forest containing this.
void insert_left_sibling(Tree_Node *p)
Inserts p as the left sibling of this.
Tree_Node * get_left_sibling() const noexcept
Returns the left sibling of this.
Tree_Node()=default
Constructor of a node whose key is T(): zero for an arithmetic type or a pointer.
constexpr const T & get_key() const noexcept
bool level_traverse(Op op) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
constexpr bool is_root() const noexcept
Returns true if this is the root of the general tree.
bool level_traverse(Op op)
T key_type
Generic data type stored in the node.
Tree_Node * get_left_child() const noexcept
Returns the leftmost child of this.
constexpr bool is_rightmost() const noexcept
Returns true if this is the rightmost node among its siblings.
Tree_Node * right_link() const noexcept
static bool preorder(const Tree_Node *root, Operation &op)
static Tree_Node * child_to_Tree_Node(Dlink *link) noexcept
Tree_Node * get_child(const size_t i) const noexcept
Returns the i-th child of this.
Container< Tree_Node * > trees() const
Return a list with all trees belonging to the forest.
void insert_leftmost_child(Tree_Node *p) noexcept
Inserts p as the leftmost child of this.
constexpr bool is_leaf() const noexcept
Returns true if this is a leaf node.
T & get_data() noexcept
Returns a modifiable reference to the node contents.
Dlink * get_child_list() noexcept
Returns the embedded child-list link.
Tree_Node * get_right_sibling() const noexcept
Returns the right sibling of this.
Tree_Node * get_left_tree() const noexcept
Returns the tree to the left of this.
void insert_rightmost_child(Tree_Node *p) noexcept
Inserts p as the rightmost child of this.
Tree_Node * lower_link() const noexcept
Children_Iterator children_it() const
Tree_Node * get_right_tree() const noexcept
Returns the tree to the right of this.
void insert_tree_to_right(Tree_Node *tree)
Insert tree to the right of this
constexpr const T & get_data() const noexcept
Tree_Node * get_parent() const noexcept
Returns the parent of this.
void set_is_leaf(bool value) noexcept
Sets the leaf flag.
Container< Tree_Node * > children_nodes() const
Returns a list with the child nodes of this.
void for_each_child(Operation &op) const
Visits each child of this and executes the operation on the child node.
bool traverse(Operation op) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Tree_Node * get_right_child() const noexcept
Returns the rightmost child of this.
void insert_right_sibling(Tree_Node *p) noexcept
Inserts p to the right of this node.
T & get_key() noexcept
Returns a modifiable reference to the node contents.
Container< T > children() const
Returns a list with the contents of the children of this.
void set_is_leftmost(bool value) noexcept
Sets the leftmost-sibling flag.
void for_each_child(Operation &&op=Operation()) const
void set_is_rightmost(bool value) noexcept
Sets the rightmost-sibling flag.
void deway(Tree_Node< int > *p, int prefix[], const int &len, const size_t &dim)
Recursively compute and print Deway numbering for a tree node.
Doubly linked circular list implementation.
#define LINKNAME_TO_TYPE(type_name, link_name)
Generate a conversion function from a Dlink field pointer to a pointer to the class containing it.
__gmp_expr< T, __gmp_binary_expr< __gmp_expr< T, U >, unsigned long int, __gmp_root_function > > root(const __gmp_expr< T, U > &expr, unsigned long int l)
void forest_postorder_traversal(Node *root, void(*visitFct)(Node *, int, int))
Postorder traversal of a forest.
void destroy_tree(Node *root)
Destroys (frees memory) the tree whose root is root.
size_t compute_height(Node *root)
Computes the height of the tree root.
void tree_postorder_traversal(Node *root, void(*visitFct)(Node *, int, int))
Postorder traversal of a tree.
static void clone_tree(Node *src, Node *tgt)
Copies the subtrees of the children of src as children of tgt.
TNode * bin_to_forest(BNode *broot)
Converts a binary tree to its equivalent forest.
bool are_tree_equal(Node *t1, Node *t2, Eq &eq)
Returns true if t1 is equal to t2.
void destroy_forest(Node *root)
Destroys (frees memory) the forest whose first tree is root.
void forest_preorder_traversal(Node *root, void(*visitFct)(Node *, int, int))
Preorder traversal of a forest.
void tree_preorder_traversal(Node *root, void(*visitFct)(Node *, int, int))
Preorder traversal of a tree.
Node * search_deway(Node *root, const typename Node::key_type &key, int deway[], const size_t &size, size_t &n)
Searches key in a forest and computes the Dewey number of the node containing the key.
Node * deway_search(Node *root, int path[], const size_t &size)
Returns a node of a forest given its Dewey number.
BNode * forest_to_bin(TNode *root)
Converts a forest to its equivalent binary tree.
Singly linked list implementations with head-tail access.
void free_detached_tree(Node *root) noexcept
Frees a tree that has no parent nor siblings, without recursion; it finds the nodes by their sibling ...
void detach_tree(Node *root) noexcept
void free_bin_tree(BNode *root) noexcept
Frees the binary tree root without recursion nor memory: a node with a left child turns into the righ...
Main namespace for Aleph-w library functions.
static Node * __deway_search(Node *node, int path[], const int &idx, const size_t &size)
static void insert_sibling(BNode *rnode, TNode *tree_node)
bool eq(const C1 &c1, const C2 &c2, Eq e=Eq())
Check equality of two containers using a predicate.
static void insert_child(BNode *lnode, TNode *tree_node)
static Node * __search_deway(Node *root, const typename Node::key_type &key, const size_t ¤t_level, int deway[], const size_t &size, size_t &n)
size_t size(Node *root) noexcept
See compute_cardinality_rec(Node * root).
std::decay_t< typename HeadC::Item_Type > T
and
Check uniqueness with explicit hash + equality functors.
static void bin_to_tree(BNode *broot, TNode *troot)
static void __tree_postorder_traversal(Node *node, const int &level, const int &child_index, void(*visitFct)(Node *, int, int))
static void __tree_preorder_traversal(Node *root, const int &level, const int &child_index, void(*visitFct)(Node *, int, int))
void next()
Advance all underlying iterators (bounds-checked).
Child iterator adapter used by generic iterator utilities.
Adapter that exposes a node's children through an Iterator type.
Children_Set(const Tree_Node &)
Children_Set(const Tree_Node &&)
unsigned int is_rightmost
Tree_Node variant with a virtual destructor.
virtual ~Tree_Node_Vtl()=default
A node of a forest and its node in the binary tree.
BNode * bnode
Its node in the binary tree.
TNode * tnode
The node of the forest.
A node of a path that a traversal follows, and its position among its siblings.
int index
Its position among its siblings.
Basic binary tree node definitions.
Stack of the frames of a depth-first search that does not recurse.
Dynamic queue implementation based on linked lists.
Dynamic stack implementation based on linked lists.
#define IS_UNIQUE_SIBLING(p)