|
Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
|
Simple (unbalanced) binary search tree. More...
#include <tpl_binTree.H>
Classes | |
| struct | Iterator |
| Iterator on nodes of the tree. More... | |
Public Types | |
| using | Node = NodeType< Key > |
Public Member Functions | |
| GenBinTree (const GenBinTree &)=delete | |
| GenBinTree & | operator= (const GenBinTree &)=delete |
| void | swap (GenBinTree &tree) noexcept(std::is_nothrow_swappable_v< Compare >) |
Swaps in constant time the nodes and the comparison criteria of this and tree. | |
| Compare & | key_comp () noexcept |
| return the comparison criteria | |
| Compare & | get_compare () noexcept |
See key_comp(). | |
| GenBinTree () noexcept(std::is_nothrow_default_constructible_v< Node > and std::is_nothrow_default_constructible_v< Compare > and std::is_nothrow_constructible_v< Compare, Compare & >) | |
| Builds an empty tree with a comparison criteria built by default. | |
| GenBinTree (Compare _cmp) noexcept(std::is_nothrow_default_constructible_v< Node > and std::is_nothrow_constructible_v< Compare, Compare & >) | |
Builds an empty tree with the comparison criteria _cmp. | |
| GenBinTree (GenBinTree &&tree) noexcept(bin_node_detail::nothrow_header_from_v< Node > and bin_node_detail::nothrow_compare_taken_v< Compare >) | |
Takes the nodes and the comparison criteria of tree, which is left empty and can take nodes again. | |
| Node * | search (const Key &key) const noexcept(nothrow_compare_copy_v< Compare, Key >) |
| Search a key. | |
| virtual | ~GenBinTree ()=default |
| bool | verify () const |
Return true if the tree is a consistent (correct) binary search tree. | |
| Node *& | getRoot () noexcept |
| Return the root of tree. | |
| Node * | getRoot () const noexcept |
| Return the root of tree. | |
| bool | verifyBin () const |
See verify(). | |
| Node * | insert (Node *p) noexcept(nothrow_compare_copy_v< Compare, Key >) |
| Insert a node in the tree. | |
| Node * | insert_dup (Node *p) noexcept(nothrow_compare_copy_v< Compare, Key >) |
| Insert a node in the tree. | |
| Node * | search_or_insert (Node *p) noexcept(nothrow_compare_copy_v< Compare, Key >) |
| Search or insert a key. | |
| bool | split (const Key &key, GenBinTree &l, GenBinTree &r) noexcept(nothrow_compare_copy_v< Compare, Key >) |
| Split the tree according to a key. | |
| void | split_dup (const Key &key, GenBinTree &l, GenBinTree &r) noexcept(nothrow_compare_v< Compare, Key >) |
| Split the tree according to a key that could be in the tree. | |
| Node * | remove (const Key &key) noexcept(nothrow_compare_copy_v< Compare, Key >) |
| Remove a key from the tree. | |
| void | join (GenBinTree &tree, GenBinTree &dup) noexcept |
Join tree with this. | |
| void | join_dup (GenBinTree &t) noexcept |
Join this with t independently of the presence of duplicated keys. | |
| void | join_exclusive (GenBinTree &t) noexcept |
Join exclusive of this with t | |
Static Private Member Functions | |
| template<class Inserter > | |
| static void | move_all (Node *&src, Inserter inserter) noexcept |
Private Attributes | |
| Node | headNode |
| The node. | |
| Node * | head |
| Node *& | root |
| Compare | cmp |
Simple (unbalanced) binary search tree.
GenBinTree implements a basic binary search tree without any balancing operations. The tree's performance depends entirely on the insertion order of keys.
WARNING**: If you cannot ensure random insertion order, use a balanced tree (Avl_Tree, Rb_Tree, Treap, etc.) instead.
| NodeType | Node template (BinNode or BinNodeVtl). |
| Key | The type of keys stored in the tree. |
| Compare | Comparison functor for ordering keys. |
Definition at line 137 of file tpl_binTree.H.
| using Aleph::GenBinTree< NodeType, Key, Compare >::Node = NodeType<Key> |
Definition at line 141 of file tpl_binTree.H.
|
delete |
|
inlinenoexcept |
Builds an empty tree with a comparison criteria built by default.
It is a constructor apart from the one that takes the comparison, and not a default argument of that one: GCC and Clang do not count a default argument in the noexcept of the implicit default constructor of a tree that inherits these, and what the construction of the comparison threw would end the program there.
| what | the default construction of the key of a header node or of the comparison, or the copy of the comparison, throws; nothing when they are noexcept. |
Definition at line 211 of file tpl_binTree.H.
|
inlinenoexcept |
Builds an empty tree with the comparison criteria _cmp.
| [in] | _cmp | the comparison criteria, which the tree copies. |
| what | the default construction of the key of a header node or the copy of _cmp throws; nothing when both are noexcept. The copy is from a Compare that is not const, which a comparison may have apart from the one from a const one. |
Definition at line 228 of file tpl_binTree.H.
|
inlinenoexcept |
Takes the nodes and the comparison criteria of tree, which is left empty and can take nodes again.
It builds no key, if it can: the key of the header node is built by default when that cannot throw; else it is that of the header node of tree, moved, when the move of a key cannot throw (see bin_node_detail::header_from()).
| [in,out] | tree | the tree to move from. It keeps its comparison criteria, which are copied: from a const one, or from one that is not const, if they have only that copy. Criteria that cannot be copied are moved, and tree keeps criteria that were moved from. |
| what | the copy of the comparison criteria throws, or their move if they cannot be copied; and what the default construction of the key of the header node throws, with a key whose move may throw too. Then tree keeps its nodes. Nothing when they are noexcept. |
Definition at line 253 of file tpl_binTree.H.
References Aleph::GenBinTree< NodeType, Key, Compare >::root.
|
virtualdefault |
|
inlinenoexcept |
See key_comp().
Definition at line 196 of file tpl_binTree.H.
References Aleph::GenBinTree< NodeType, Key, Compare >::key_comp().
|
inlinenoexcept |
Return the root of tree.
Definition at line 289 of file tpl_binTree.H.
References Aleph::GenBinTree< NodeType, Key, Compare >::root.
|
inlinenoexcept |
Return the root of tree.
Definition at line 286 of file tpl_binTree.H.
References Aleph::GenBinTree< NodeType, Key, Compare >::root.
Referenced by main(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), and write_bin().
|
inlinenoexcept |
Insert a node in the tree.
| [in] | p | pointer to the node to insert |
p->get_key() is not in the tree, then the pointer p is returned (it was inserted); otherwise, nullptr is returned | anything | that the comparison or its copy throws; p is linked only after the last comparison, so the tree is left as it was. |
noexcept only if the comparison and its copy cannot throw, so that what it throws reaches the caller. Definition at line 306 of file tpl_binTree.H.
References Aleph::GenBinTree< NodeType, Key, Compare >::cmp, Aleph::BinTree_Operation< Node, Cmp >::insert(), and Aleph::GenBinTree< NodeType, Key, Compare >::root.
Referenced by main(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), and write_bin().
|
inlinenoexcept |
Insert a node in the tree.
This method does not fail. It always inserts.
@param[in] p pointer to the node to insert @return the `p` pointer @throws anything that the comparison or its copy throws; `p` is linked only after the last comparison, so the tree is left as it was. @note It is `noexcept` only if the comparison and its copy cannot throw, so that what it throws reaches the caller.
Definition at line 323 of file tpl_binTree.H.
References Aleph::GenBinTree< NodeType, Key, Compare >::cmp, Aleph::BinTree_Operation< Node, Cmp >::insert_dup(), and Aleph::GenBinTree< NodeType, Key, Compare >::root.
Referenced by TEST().
|
inlinenoexcept |
Join tree with this.
Duplicated keys of tree are put in dup parameter.
@param[in,out] tree to join with `this` @param[out] dup tree where the duplicated key belonging to `tree` are put. @pre The comparison does not throw; if it does, the program ends (`std::terminate()`). @note It stays `noexcept` because it moves the nodes one by one: a comparison that threw in the middle would leave nodes out of every tree.
Definition at line 424 of file tpl_binTree.H.
References Aleph::GenBinTree< NodeType, Key, Compare >::cmp, and Aleph::GenBinTree< NodeType, Key, Compare >::move_all().
Referenced by TEST().
|
inlinenoexcept |
Join this with t independently of the presence of duplicated keys.
join(t) produces a random tree result of join of this and t. The resulting tree is stored in this.
| [in] | t | tree to join with this keys are inserted |
std::terminate()). noexcept because it moves the nodes one by one: a comparison that threw in the middle would leave nodes out of every tree. Definition at line 448 of file tpl_binTree.H.
References Aleph::GenBinTree< NodeType, Key, Compare >::cmp, and Aleph::GenBinTree< NodeType, Key, Compare >::move_all().
Referenced by TEST().
|
inlinenoexcept |
Join exclusive of this with t
Exclusive means that all the keys of this are lesser than all the keys of t. This knowledge allows a more effcient way for joining that when the keys ranks are overlapped. However, use very carefully because the algorithm does not perform any check and the result would be incorrect.
| [in] | t | tree to exclusively join with this keys are inserted |
Definition at line 465 of file tpl_binTree.H.
References Aleph::GenBinTree< NodeType, Key, Compare >::root.
Referenced by TEST().
|
inlinenoexcept |
return the comparison criteria
Definition at line 193 of file tpl_binTree.H.
References Aleph::GenBinTree< NodeType, Key, Compare >::cmp.
Referenced by Aleph::GenBinTree< NodeType, Key, Compare >::get_compare().
|
inlinestaticprivatenoexcept |
Definition at line 151 of file tpl_binTree.H.
References l, LLINK, r, and RLINK.
Referenced by Aleph::GenBinTree< NodeType, Key, Compare >::join(), and Aleph::GenBinTree< NodeType, Key, Compare >::join_dup().
|
delete |
|
inlinenoexcept |
Remove a key from the tree.
| [in] | key | to remove |
key was found in the tree, nullptr otherwise | anything | that the comparison or its copy throws; the node is unlinked only after the last comparison, so the tree is left as it was. |
noexcept only if the comparison and its copy cannot throw, so that what it throws reaches the caller. Definition at line 407 of file tpl_binTree.H.
References Aleph::GenBinTree< NodeType, Key, Compare >::cmp, Aleph::BinTree_Operation< Node, Cmp >::remove(), and Aleph::GenBinTree< NodeType, Key, Compare >::root.
|
inlinenoexcept |
Search a key.
| [in] | key | to be searched |
nullptr | anything | that the comparison or its copy throws; the search does not modify the tree, so it is left as it was. |
noexcept only if the comparison and its copy cannot throw, so that what it throws reaches the caller. Definition at line 274 of file tpl_binTree.H.
References Aleph::GenBinTree< NodeType, Key, Compare >::cmp, Aleph::GenBinTree< NodeType, Key, Compare >::root, and Aleph::BinTree_Operation< Node, Cmp >::search().
Referenced by main(), TEST(), and write_bin().
|
inlinenoexcept |
Search or insert a key.
search_or_insert(p) searches in the tree the key KEY(p). If this key is found, then a pointer to the node containing it is returned. Otherwise, p is inserted.
| [in] | p | node containing a key to be searched and eventually inserted |
p is found, then a pointer to the containing key in the tree is returned. Otherwise, p is inserted and returned | anything | that the comparison or its copy throws; p is linked only after the last comparison, so the tree is left as it was. |
noexcept only if the comparison and its copy cannot throw, so that what it throws reaches the caller. Definition at line 345 of file tpl_binTree.H.
References Aleph::GenBinTree< NodeType, Key, Compare >::cmp, Aleph::GenBinTree< NodeType, Key, Compare >::root, and Aleph::BinTree_Operation< Node, Cmp >::search_or_insert().
Referenced by TEST().
|
inlinenoexcept |
Split the tree according to a key.
split(key, l, r) splits the tree according to key. That is, if key is not present in the tree, then the tree is split in two trees l which contains the key lesser than key and r which contains the keys greater than key. If key is found in the tree, then the split is not done
| [in] | key | for splitting |
| [out] | l | resulting tree with the keys lesser than key |
| [out] | r | resulting tree with the keys greater than key |
false otherwise | anything | that the comparison or its copy throws; every comparison comes before the first change, so the three trees are left as they were. |
noexcept only if the comparison and its copy cannot throw, so that what it throws reaches the caller. Definition at line 369 of file tpl_binTree.H.
References Aleph::GenBinTree< NodeType, Key, Compare >::cmp, l, r, Aleph::GenBinTree< NodeType, Key, Compare >::root, and Aleph::split_key_rec().
|
inlinenoexcept |
Split the tree according to a key that could be in the tree.
split_dup(key, l, r) splits the tree according to key in two trees l which contains the key lesser than key and r which contains the keys greater or equal than key.
| [in] | key | for splitting |
| [out] | l | resulting tree with the keys lesser than key |
| [out] | r | resulting tree with the keys greater or equal than key |
| anything | that the comparison throws; every comparison comes before the first change, so the three trees are left as they were. |
noexcept only if the comparison cannot throw, so that what it throws reaches the caller. Definition at line 390 of file tpl_binTree.H.
References Aleph::GenBinTree< NodeType, Key, Compare >::cmp, l, r, and Aleph::GenBinTree< NodeType, Key, Compare >::root.
Referenced by TEST().
|
inlinenoexcept |
Swaps in constant time the nodes and the comparison criteria of this and tree.
| [in,out] | tree | the tree to swap with. |
| what | the swap of the comparison criteria throws, which goes first: then the nodes stay where they were. |
Definition at line 184 of file tpl_binTree.H.
References Aleph::GenBinTree< NodeType, Key, Compare >::cmp, and Aleph::GenBinTree< NodeType, Key, Compare >::root.
Referenced by TEST().
|
inline |
Return true if the tree is a consistent (correct) binary search tree.
Definition at line 283 of file tpl_binTree.H.
References Aleph::GenBinTree< NodeType, Key, Compare >::cmp, and Aleph::GenBinTree< NodeType, Key, Compare >::root.
Referenced by TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), and TEST().
|
inline |
See verify().
Definition at line 292 of file tpl_binTree.H.
References Aleph::GenBinTree< NodeType, Key, Compare >::cmp, and Aleph::GenBinTree< NodeType, Key, Compare >::root.
Referenced by main().
|
private |
Definition at line 148 of file tpl_binTree.H.
Referenced by Aleph::GenBinTree< NodeType, Key, Compare >::insert(), Aleph::GenBinTree< NodeType, Key, Compare >::insert_dup(), Aleph::GenBinTree< NodeType, Key, Compare >::join(), Aleph::GenBinTree< NodeType, Key, Compare >::join_dup(), Aleph::GenBinTree< NodeType, Key, Compare >::key_comp(), Aleph::GenBinTree< NodeType, Key, Compare >::remove(), Aleph::GenBinTree< NodeType, Key, Compare >::search(), Aleph::GenBinTree< NodeType, Key, Compare >::search_or_insert(), Aleph::GenBinTree< NodeType, Key, Compare >::split(), Aleph::GenBinTree< NodeType, Key, Compare >::split_dup(), Aleph::GenBinTree< NodeType, Key, Compare >::swap(), Aleph::GenBinTree< NodeType, Key, Compare >::verify(), and Aleph::GenBinTree< NodeType, Key, Compare >::verifyBin().
|
private |
Definition at line 146 of file tpl_binTree.H.
|
private |
The node.
Definition at line 145 of file tpl_binTree.H.
|
private |
Definition at line 147 of file tpl_binTree.H.
Referenced by Aleph::GenBinTree< NodeType, Key, Compare >::GenBinTree(), Aleph::GenBinTree< NodeType, Key, Compare >::getRoot(), Aleph::GenBinTree< NodeType, Key, Compare >::getRoot(), Aleph::GenBinTree< NodeType, Key, Compare >::insert(), Aleph::GenBinTree< NodeType, Key, Compare >::insert_dup(), Aleph::GenBinTree< NodeType, Key, Compare >::join_exclusive(), Aleph::GenBinTree< NodeType, Key, Compare >::remove(), Aleph::GenBinTree< NodeType, Key, Compare >::search(), Aleph::GenBinTree< NodeType, Key, Compare >::search_or_insert(), Aleph::GenBinTree< NodeType, Key, Compare >::split(), Aleph::GenBinTree< NodeType, Key, Compare >::split_dup(), Aleph::GenBinTree< NodeType, Key, Compare >::swap(), Aleph::GenBinTree< NodeType, Key, Compare >::verify(), and Aleph::GenBinTree< NodeType, Key, Compare >::verifyBin().