60# define ISROOT(p) ((p)->is_root())
61# define ISLEAF(p) ((p)->is_leaf())
62# define ISLEFTMOST(p) ((p)->is_leftmost())
63# define ISRIGHTMOST(p) ((p)->is_rightmost())
65# define SIBLING_LIST(p) ((p)->get_sibling_list())
66# define CHILD_LIST(p) ((p)->get_child_list())
67# define SIBLING_LINK(p) ((p)->get_sibling_list())
68# define LCHILD(p) ((p)->get_left_child())
69# define RSIBLING(p) ((p)->get_right_sibling())
70# define IS_UNIQUE_SIBLING(p) (RSIBLING(p) == (p))
230 for (
size_t j = 0; c !=
nullptr and j < i; ++j)
249 return p->upper_link();
268 p->set_is_root(
false);
269 p->set_is_leftmost(
false);
275 p->set_is_rightmost(
false);
280 p->set_is_rightmost(
true);
299 <<
"Cannot insert sibling of a root";
310 if (old_prev_node !=
nullptr)
358 p->set_is_root(
false);
377 p->set_is_rightmost(
false);
398 p->set_is_root(
false);
409 p->set_is_leftmost(
false);
459 <<
"\"this\" is not root";
463 if (old_next_tree !=
nullptr)
497 <<
"\"this\" is not the leftmost tree in the forest";
507 for (
auto t =
const_cast<Tree_Node*
>(
this); t !=
nullptr;
514 template <
typename Operation>
522 template <
typename Operation>
551 template <
class Operation>
static
571 template <
class Operation>
577 template <
class Operation>
616 :
curr(p.get_left_child()) {}
619 :
curr(p.get_left_child()) {}
622 :
curr(p->get_left_child()) {}
674 std::swap(
root, it.root);
675 std::swap(
curr, it.curr);
676 std::swap(
pos, it.pos);
738 p = p->get_left_sibling())
770 template <
typename T>
776 template <
class Node>
static inline
779 using It =
typename Node::Children_Iterator;
780 for (It it(src); it.has_curr(); it.next_ne())
781 tgt->insert_rightmost_child(
new Node(it.get_curr()->get_key()));
786 auto p =
itor.get_curr();
791 template <
class Node>
801 template <
class Node>
static inline
807 Node * child =
root->get_left_child();
808 for (
int i = 0; child !=
nullptr; ++i, child = child->get_right_sibling())
834 template <
class Node>
inline
839 <<
"root is not root";
866 template <
class Node>
inline
879 template <
class Node>
static inline
884 Node * child = node->get_left_child();
886 for (
int i = 0; child
not_eq nullptr;
887 i++, child = child->get_right_sibling())
914 template <
class Node>
inline
943 template <
class Node>
inline
961 template <
class Node,
class Eq>
965 return t2 ==
nullptr;
970 if (
not eq(
t1->get_key(),
t2->get_key()))
975 return zipEq(
t1->children_nodes(),
t2->children_nodes()).
981 catch (
const std::length_error &)
987 template <
class Node,
988 class Eq = std::equal_to<typename Node::key_type>>
1002 template <
class Node>
inline
1005 if (
root ==
nullptr)
1012 for (
Node * p =
static_cast<Node *
>(
root->get_right_child()); p !=
nullptr; )
1014 Node * to_delete = p;
1015 p =
static_cast<Node *
>(p->get_left_sibling());
1019 if (
root->is_leftmost())
1036 template <
class Node>
inline
1039 if (
root ==
nullptr)
1046 while (
root !=
nullptr)
1061 template <
class Node>
1064 if (
root ==
nullptr)
1068 for (
Node * aux =
root->get_left_child(); aux !=
nullptr;
1069 aux = aux->get_right_sibling())
1076 template <
class Node>
static inline
1078 const int & idx,
const size_t &
size)
1080 if (node ==
nullptr)
1084 <<
"index out of maximum range";
1089 Node * child = node->get_left_child();
1090 for (
int i = 0; i < path[idx]
and child !=
nullptr; ++i)
1091 child = child->get_right_sibling();
1109 template <
class Node>
inline
1112 for (
int i = 0;
root !=
nullptr; i++,
root =
root->get_right_sibling())
1119 template <
class Node,
class Equal>
inline static
1121 const size_t & current_level,
int deway [],
1122 const size_t &
size,
size_t & n);
1144 template <
class Node,
1147 int deway [],
const size_t &
size,
size_t & n)
1153 for (
int i = 0;
root !=
nullptr; i++,
root =
root->get_right_sibling())
1158 if (result !=
nullptr)
1165 template <
class Node,
class Equal>
inline static
1167 const typename Node::key_type & key,
1168 const size_t & current_level,
int deway [],
1169 const size_t &
size,
size_t & n)
1174 if (
root ==
nullptr)
1177 if (Equal()(
root->get_key(), key))
1179 n = current_level + 1;
1183 Node * child =
root->get_left_child();
1184 for (
int i = 0; child !=
nullptr;
1185 i++, child = child->get_right_sibling())
1188 <<
"there is no enough space for deway array";
1189 deway[current_level + 1] = i;
1191 (child, key, current_level + 1,
deway,
size, n);
1193 if (result!=
nullptr)
1218 template <
class TNode,
class BNode>
1221 if (
root ==
nullptr)
1222 return BNode::NullPtr;
1224 auto * result =
new BNode (
root->get_key());
1231 template <
class TNode,
class BNode>
inline static
1234 if (
lnode == BNode::NullPtr)
1238 tree_node->insert_leftmost_child(child);
1241 template <
class TNode,
class BNode>
inline static
1244 if (
rnode == BNode::NullPtr)
1248 tree_node->insert_right_sibling(sibling);
1251 template <
class TNode,
class BNode>
inline static
1254 if (
broot == BNode::NullPtr)
1285 template <
class TNode,
class BNode>
inline
1288 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
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.
Dynamic singly linked list with functional programming support.
CRTP Mixin providing functional programming operations.
Container< __Type > maps(Operation &operation) const
Transform elements using a mapping function.
Iterator that zips two other iterators.
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
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
DynListStack< Tree_Node * > s
size_t get_pos() const
Return the current position of iterator. Only valid if.
Iterator(Tree_Node *r=nullptr) noexcept
Tree_Node * get_curr() const
Iterator(Tree_Node &root)
Tree_Node * get_curr_ne() const noexcept
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
Dlink * get_sibling_list() noexcept
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 forrest.
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
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
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 as the right sibling of this.
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
void for_each_child(Operation &&op=Operation()) const
void set_is_rightmost(bool value) noexcept
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.
TNode * bin_to_forest(BNode *broot)
Converts a binary tree to its equivalent forest.
constexpr Node *& RLINK(Node *p) noexcept
Return the right tree of p.
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.
constexpr Node *& LLINK(Node *p) noexcept
Return a pointer to left subtree.
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 all(Container &container, Operation &operation)
Return true if all elements satisfy a predicate.
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
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))
DynList< T > maps(const C &c, Op op)
Classic map operation.
Children_Set(const Tree_Node &)
Children_Set(const Tree_Node &&)
unsigned int is_rightmost
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)