|
Aleph-w 3.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 |
Swap this with tree in constant time. | |
| Compare & | key_comp () noexcept |
| return the comparison criteria | |
| Compare & | get_compare () noexcept |
| GenBinTree (Compare _cmp=Compare()) noexcept | |
Initialize an empty tree with comparison criteria __cmp | |
| Node * | search (const Key &key) const noexcept |
| 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 |
| Node * | insert (Node *p) noexcept |
| Insert a node in the tree. | |
| Node * | insert_dup (Node *p) noexcept |
| Insert a node in the tree. | |
| Node * | search_or_insert (Node *p) noexcept |
| Search or insert a key. | |
| bool | split (const Key &key, GenBinTree &l, GenBinTree &r) noexcept |
| Split the tree according to a key. | |
| void | split_dup (const Key &key, GenBinTree &l, GenBinTree &r) noexcept |
| Split the tree according to a key that could be in the tree. | |
| Node * | remove (const Key &key) noexcept |
| 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 135 of file tpl_binTree.H.
| using Aleph::GenBinTree< NodeType, Key, Compare >::Node = NodeType<Key> |
Definition at line 139 of file tpl_binTree.H.
|
delete |
|
inlinenoexcept |
Initialize an empty tree with comparison criteria __cmp
Definition at line 189 of file tpl_binTree.H.
|
virtualdefault |
|
inlinenoexcept |
Definition at line 186 of file tpl_binTree.H.
References Aleph::GenBinTree< NodeType, Key, Compare >::key_comp().
|
inlinenoexcept |
Return the root of tree.
Definition at line 215 of file tpl_binTree.H.
References Aleph::GenBinTree< NodeType, Key, Compare >::root.
|
inlinenoexcept |
Return the root of tree.
Definition at line 212 of file tpl_binTree.H.
References Aleph::GenBinTree< NodeType, Key, Compare >::root.
Referenced by main(), 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 Definition at line 227 of file tpl_binTree.H.
References Aleph::GenBinTree< NodeType, Key, Compare >::cmp, Aleph::DynList< T >::insert(), Aleph::maps(), 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
Definition at line 239 of file tpl_binTree.H.
References Aleph::GenBinTree< NodeType, Key, Compare >::cmp, Aleph::maps(), and Aleph::GenBinTree< NodeType, Key, Compare >::root.
|
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.
Definition at line 313 of file tpl_binTree.H.
References Aleph::GenBinTree< NodeType, Key, Compare >::cmp, Aleph::maps(), 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 |
Definition at line 331 of file tpl_binTree.H.
References Aleph::GenBinTree< NodeType, Key, Compare >::cmp, Aleph::maps(), 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 348 of file tpl_binTree.H.
References Aleph::maps(), and Aleph::GenBinTree< NodeType, Key, Compare >::root.
Referenced by TEST().
|
inlinenoexcept |
return the comparison criteria
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Definition at line 183 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 149 of file tpl_binTree.H.
References l, Aleph::LLINK(), Aleph::maps(), and Aleph::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 Definition at line 301 of file tpl_binTree.H.
References Aleph::GenBinTree< NodeType, Key, Compare >::cmp, Aleph::maps(), Aleph::DynList< T >::remove(), and Aleph::GenBinTree< NodeType, Key, Compare >::root.
|
inlinenoexcept |
Search a key.
| [in] | key | to be searched |
nullptr Definition at line 201 of file tpl_binTree.H.
References Aleph::GenBinTree< NodeType, Key, Compare >::cmp, Aleph::maps(), and Aleph::GenBinTree< NodeType, Key, Compare >::root.
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 Definition at line 256 of file tpl_binTree.H.
References Aleph::GenBinTree< NodeType, Key, Compare >::cmp, Aleph::maps(), and Aleph::GenBinTree< NodeType, Key, Compare >::root.
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 Definition at line 274 of file tpl_binTree.H.
References Aleph::GenBinTree< NodeType, Key, Compare >::cmp, l, Aleph::maps(), 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 |
Definition at line 290 of file tpl_binTree.H.
References Aleph::GenBinTree< NodeType, Key, Compare >::cmp, l, Aleph::maps(), and Aleph::GenBinTree< NodeType, Key, Compare >::root.
Referenced by TEST().
|
inlinenoexcept |
Swap this with tree in constant time.
Definition at line 176 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.
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Definition at line 209 of file tpl_binTree.H.
References Aleph::GenBinTree< NodeType, Key, Compare >::cmp, Aleph::maps(), and Aleph::GenBinTree< NodeType, Key, Compare >::root.
Referenced by TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), and TEST().
|
inline |
Definition at line 218 of file tpl_binTree.H.
References Aleph::GenBinTree< NodeType, Key, Compare >::cmp, Aleph::maps(), and Aleph::GenBinTree< NodeType, Key, Compare >::root.
Referenced by main().
|
private |
Definition at line 146 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 144 of file tpl_binTree.H.
|
private |
The node.
Definition at line 143 of file tpl_binTree.H.
|
private |
Definition at line 145 of file tpl_binTree.H.
Referenced by 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().