61#define ISROOT(p) ((p)->is_root())
62#define ISLEAF(p) ((p)->is_leaf())
63#define ISLEFTMOST(p) ((p)->is_leftmost())
64#define ISRIGHTMOST(p) ((p)->is_rightmost())
66#define SIBLING_LIST(p) ((p)->get_sibling_list())
67#define CHILD_LIST(p) ((p)->get_child_list())
68#define SIBLING_LINK(p) ((p)->get_sibling_list())
69#define LCHILD(p) ((p)->get_left_child())
70#define RSIBLING(p) ((p)->get_right_sibling())
71#define IS_UNIQUE_SIBLING(p) (SIBLING_LIST(p)->is_empty())
347 for (
size_t j = 0; c !=
nullptr and j < i; ++j)
366 return p->upper_link();
385 assert(p->is_rightmost()
and p->is_leftmost()
and p->is_root()
and p->is_leaf());
393 p->set_is_root(
false);
394 p->set_is_leftmost(
false);
400 p->set_is_rightmost(
false);
405 p->set_is_rightmost(
true);
433 if (old_prev_node !=
nullptr)
478 assert(p->is_rightmost()
and p->is_leftmost()
and p->is_root()
and p->is_leaf());
480 p->set_is_root(
false);
499 p->set_is_rightmost(
false);
517 assert(p->is_rightmost()
and p->is_leftmost()
and p->is_root()
and p->is_leaf());
519 p->set_is_root(
false);
530 p->set_is_leftmost(
false);
582 if (old_next_tree !=
nullptr)
631 template <
typename Operation>
638 template <
typename Operation>
669 template <
class Operation>
688 template <
class Operation>
694 template <
class Operation>
748 return curr !=
nullptr;
814 std::swap(
root, it.root);
815 std::swap(
curr, it.curr);
816 std::swap(
pos, it.pos);
852 return curr !=
nullptr;
933 using It =
typename Node::Children_Iterator;
934 for (
It it(src); it.has_curr(); it.
next_ne())
935 tgt->insert_rightmost_child(
new Node(it.get_curr()->get_key()));
959 (*visitFct)(
root, level, child_index);
960 Node *child =
root->get_left_child();
961 for (
int i = 0; child !=
nullptr; ++i, child = child->get_right_sibling())
1017template <
class Node>
1029template <
class Node>
1033 Node *child = node->get_left_child();
1035 for (
int i = 0; child
not_eq nullptr; i++, child = child->get_right_sibling())
1038 (*visitFct)(node, level, child_index);
1062template <
class Node>
1091template <
class Node>
1109template <
class Node,
class Eq>
1113 return t2 ==
nullptr;
1118 if (
not eq(
t1->get_key(),
t2->get_key()))
1123 return zipEq(
t1->children_nodes(),
t2->children_nodes())
1129 catch (
const std::length_error &)
1135template <
class Node,
class Eq = std::equal_to<
typename Node::key_type>>
1149template <
class Node>
1152 if (
root ==
nullptr)
1175 if (
root->is_rightmost())
1179 if (
root->is_leftmost())
1205 for (
Node *p =
static_cast<Node *
>(
root->get_right_child()); p !=
nullptr; )
1207 Node *to_delete = p;
1208 p =
static_cast<Node *
>(p->get_left_sibling());
1219 else if (
root->is_leftmost())
1236template <
class Node>
1239 if (
root ==
nullptr)
1246 while (
root !=
nullptr)
1261template <
class Node>
1264 if (
root ==
nullptr)
1268 for (
Node *aux =
root->get_left_child(); aux !=
nullptr; aux = aux->get_right_sibling())
1275template <
class Node>
1278 if (node ==
nullptr)
1286 Node *child = node->get_left_child();
1287 for (
int i = 0; i < path[idx]
and child !=
nullptr; ++i)
1288 child = child->get_right_sibling();
1306template <
class Node>
1309 for (
int i = 0;
root !=
nullptr; i++,
root =
root->get_right_sibling())
1316template <
class Node,
class Equal>
1318 const size_t ¤t_level,
int deway[],
const size_t &
size,
1341template <
class Node,
class Equal = Aleph::equal_to<
typename Node::key_type>>
1343 const size_t &
size,
size_t &n)
1349 for (
int i = 0;
root !=
nullptr; i++,
root =
root->get_right_sibling())
1353 if (result !=
nullptr)
1360template <
class Node,
class Equal>
1362 const size_t ¤t_level,
int deway[],
const size_t &
size,
1367 if (
root ==
nullptr)
1370 if (Equal()(
root->get_key(), key))
1372 n = current_level + 1;
1376 Node *child =
root->get_left_child();
1377 for (
int i = 0; child !=
nullptr; i++, child = child->get_right_sibling())
1380 deway[current_level + 1] = i;
1383 if (result !=
nullptr)
1408template <
class TNode,
class BNode>
1411 if (
root ==
nullptr)
1412 return BNode::NullPtr;
1414 auto *result =
new BNode(
root->get_key());
1421template <
class TNode,
class BNode>
1424 if (
lnode == BNode::NullPtr)
1428 tree_node->insert_leftmost_child(child);
1431template <
class TNode,
class BNode>
1434 if (
rnode == BNode::NullPtr)
1438 tree_node->insert_right_sibling(sibling);
1441template <
class TNode,
class BNode>
1444 if (
broot == BNode::NullPtr)
1475template <
class TNode,
class BNode>
1478 if (
broot == BNode::NullPtr)
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
Doubly linked circular list node.
Dlink *& get_next() const noexcept
Return the link that is after this
void append(Dlink *node) noexcept
Insert node before this.
Dlink * del() noexcept
Remove this from the list. this must not be a header node.
Dlink *& get_prev() const noexcept
Return the link that is before this
Dlink cut_list(Dlink *link) noexcept
Cut this from link.
void insert(Dlink *node) noexcept
Insert node after 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).
Generic filter iterator wrapper.
void next_ne() noexcept
Advances the iterator to the next filtered element (noexcept version).
CRTP Mixin providing functional programming operations.
Iterator that zips two other iterators.
auto get_curr() const
Get current pair (bounds-checked).
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
Empty constructor (undefined key).
constexpr const T & get_key() const noexcept
bool level_traverse(Op op) const
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
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)
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
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.
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.
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)
DynList< std::pair< typename Container1::Item_Type, typename Container2::Item_Type > > zipEq(const Container1 &a, const Container2 &b)
Zip two containers; throw if lengths differ.
size_t size(Node *root) noexcept
and
Check uniqueness with explicit hash + equality functors.
std::decay_t< typename HeadC::Item_Type > T
static void clone_tree(Node *src, Node *tgt)
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))
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
Basic binary tree node definitions.
Dynamic queue implementation based on linked lists.
Dynamic stack implementation based on linked lists.
#define IS_UNIQUE_SIBLING(p)