![]() |
aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
|
A generic binary search tree. More...
#include <agrum/base/core/binSearchTree.h>
Public Types | |
| using | iterator = BinSearchTreeIterator< Val, Cmp, Node > |
| Alias for gum::BinSearchTree iterators. | |
| using | const_iterator = BinSearchTreeIterator< Val, Cmp, Node > |
| Alias for gum::BinSearchTree iterators. | |
Public Member Functions | |
Class constructors and destructors | |
| BinSearchTree (bool uniqueness_policy=false) | |
| Basic constructor: returns an empty binary search tree. | |
| BinSearchTree (const BinSearchTree< Val, Cmp, Node > &from) | |
| Copy constructor. | |
| virtual | ~BinSearchTree () |
| Class destructor. | |
Class operators | |
| BinSearchTree< Val, Cmp, Node > & | operator= (const BinSearchTree< Val, Cmp, Node > &from) |
| Copy operator. | |
Class iterators | |
| iterator | begin () |
| Begin iterator. | |
| const_iterator | begin () const |
| Begin iterator. | |
| iterator | rbegin () |
| Reverse iterator. | |
| const_iterator | rbegin () const |
| Reverse iterator. | |
| const iterator & | end () |
| End iterator. | |
| const const_iterator & | end () const |
| End iterator. | |
| const iterator & | rend () |
| Reverse end iterator. | |
| const const_iterator & | rend () const |
| Reverse end iterator. | |
| iterator | root () |
| Returns an iterator at the root of the tree. | |
| const_iterator | root () const |
| Returns an iterator at the root of the tree. | |
Class Accessors and Modifiers | |
| const Val & | rootValue () const |
| Returns the value of the root of the tree. | |
| const Val & | minValue () const |
| Returns the value of the leftmost node with the minimal key in the tree. | |
| const Val & | maxValue () const |
| Returns the value of the rightmost node with the maximal key in the tree. | |
| const Val & | insert (const Val &val) |
| Creates a copy of the value, insert it in the tree and returns the copy value. | |
| void | erase (const Val &val) |
| Erase the leftmost node with the given (key,val) pair. | |
| void | erase (const iterator &iter) |
| Erase the node pointed to by the iterator. | |
| bool | contains (const Val &val) const |
| Returns true if the gum::BinSearchTree contains the value. | |
| void | clear () |
| Removes all the elements from the gum::BinSearchTree. | |
| Size | size () const |
| Returns the number of elements in the binary search tree. | |
| bool | empty () const |
| Indicates whether the gum::BinSearchTree search tree is empty. | |
| virtual std::string | toString () const |
| Displays the content of the tree, in increasing order w.r.t. | |
Fine tuning | |
| bool | uniquenessPolicy () const |
| Returns the current uniqueness policy. | |
| void | setUniquenessPolicy (const bool new_policy) |
| Enables the user to change dynamically the policy for checking whether there can exist several identical elements in the binary tree. | |
Protected Member Functions | |
| Node * | copy_ (Node *root_from, Node *parent=0, BinTreeDir dir=BinTreeDir::LEFT_CHILD) |
| A method for recursively copying the contents of a BinSearchTree. | |
| Node * | minNode_ (Node *node) const |
| Returns the smallest node w.r.t. | |
| Node * | maxNode_ (Node *node) const |
| Returns the greatest node w.r.t. | |
| Node * | succNode_ (Node *node) const |
| Returns the next node according to the weak ordering Cmp. | |
| Node * | prevNode_ (Node *node) const |
| Returns the previous node according to weak ordering Cmp. | |
| Node * | getNode_ (const Val &val) const |
| Returns the node containing a given value. | |
| void | deleteSubTree_ (Node *node) |
| A method for recursively deleting a subtree of the gum::BinSearchTree. | |
| virtual void | erase_ (Node *node) |
| Erase the node passed in argument. | |
| virtual Node * | insert_ (const Val &val) |
| Creates a copy of the value, insert it in the gum::BinSearchTree and returns the copy value. | |
Protected Attributes | |
| Node * | root_ |
| The root node of the tree. | |
| Cmp | cmp_ |
| The comparison function. | |
| iterator * | iterator_list_ |
| The list of iterators pointing to the binary search tree. | |
| bool | uniqueness_policy_ |
| The uniqueness property: whether the same value can appear multiple times. | |
| Size | nb_elements_ |
| The number of elements stored in the tree. | |
| iterator | iter_end_ |
| Pseudo static iterator. | |
Private Member Functions | |
| void | _eraseWithTwoChildren_ (Node *node) |
| Erase a node with two children. | |
| void | _updateEraseIterators_ (Node *node) |
| Update all iterators when a given node is deleted. | |
Friends | |
| class | BinSearchTreeIterator< Val, Cmp, Node > |
| To speed-up accesses. | |
A generic binary search tree.
| Val | The values type to store in the binary search tree. |
| Cmp | The comparator for sorting the binary search tree. |
| Node | The nodes type used to store values in the binary search tree. |
Definition at line 84 of file binSearchTree.h.
| using gum::BinSearchTree< Val, Cmp, Node >::const_iterator = BinSearchTreeIterator< Val, Cmp, Node > |
Alias for gum::BinSearchTree iterators.
Definition at line 89 of file binSearchTree.h.
| using gum::BinSearchTree< Val, Cmp, Node >::iterator = BinSearchTreeIterator< Val, Cmp, Node > |
Alias for gum::BinSearchTree iterators.
Definition at line 88 of file binSearchTree.h.
|
explicit |
Basic constructor: returns an empty binary search tree.
| uniqueness_policy | Allows (false) or disables (true) the binary tree uniqueness. |
It is possible for the binary tree to have multiple instances of the same value within the tree.
| gum::BinSearchTree< Val, Cmp, Node >::BinSearchTree | ( | const BinSearchTree< Val, Cmp, Node > & | from | ) |
Copy constructor.
| from | The gum::BinSearchTree to copy. |
|
virtual |
Class destructor.
|
private |
Erase a node with two children.
This is used by gum::BinSearchTree::erase_(Node*).
| node | The node to erase. |
|
private |
Update all iterators when a given node is deleted.
| node | The node that is erased. |
| iterator gum::BinSearchTree< Val, Cmp, Node >::begin | ( | ) |
Begin iterator.
| const_iterator gum::BinSearchTree< Val, Cmp, Node >::begin | ( | ) | const |
Begin iterator.
| void gum::BinSearchTree< Val, Cmp, Node >::clear | ( | ) |
Removes all the elements from the gum::BinSearchTree.
| bool gum::BinSearchTree< Val, Cmp, Node >::contains | ( | const Val & | val | ) | const |
Returns true if the gum::BinSearchTree contains the value.
| val | The value tested for existence. |
|
protected |
A method for recursively copying the contents of a BinSearchTree.
| root_from | The root of the tree to be copied. |
| parent | The node that should be the parent of the copy. |
| dir | The direction of the edge parent->copy. |
References gum::LEFT_CHILD.
|
protected |
A method for recursively deleting a subtree of the gum::BinSearchTree.
Note that this method does not update the iterators pointing to nodes of the subtree. These should be cleared before deleteSubTree_ is called.
| node | The root of the subtree to delete. |
| bool gum::BinSearchTree< Val, Cmp, Node >::empty | ( | ) | const |
Indicates whether the gum::BinSearchTree search tree is empty.
| const iterator & gum::BinSearchTree< Val, Cmp, Node >::end | ( | ) |
End iterator.
| const const_iterator & gum::BinSearchTree< Val, Cmp, Node >::end | ( | ) | const |
End iterator.
| void gum::BinSearchTree< Val, Cmp, Node >::erase | ( | const iterator & | iter | ) |
Erase the node pointed to by the iterator.
If we could not find the node, then nothing happens. In particular, no exception is raised.
| iter | The iterator pointing toward the value to remove. |
| void gum::BinSearchTree< Val, Cmp, Node >::erase | ( | const Val & | val | ) |
Erase the leftmost node with the given (key,val) pair.
| val | The value to remove. |
| NotFound | Raised if we could not find the node. |
|
protectedvirtual |
Erase the node passed in argument.
| node | The Node to erase. |
|
protected |
Returns the node containing a given value.
| val | The value of the node to return. |
| const Val & gum::BinSearchTree< Val, Cmp, Node >::insert | ( | const Val & | val | ) |
Creates a copy of the value, insert it in the tree and returns the copy value.
When elements are inserted into binary search trees, this is actually copies that are inserted. Thus, the method returns the newly created copy, so that the user may reference it.
| val | The value added by copy. |
| DuplicateElement | Raised if the binary tree already contains the value and the uniqueness property is set to true. |
|
protectedvirtual |
Creates a copy of the value, insert it in the gum::BinSearchTree and returns the copy value.
When elements are inserted into gum::BinSearchTree, this is actually copies that are inserted. Thus, the method returns the node containing the newly created copy, so that the user may reference the new copy.
| DuplicateElement | exception is raised if the binary tree already contains the value and the uniqueness property is set to true. |
| val | The value added by copy. |
|
protected |
Returns the greatest node w.r.t.
order Cmp in the subtree rooted at node.
| node | The root for looking for the the greatest node. |
| const Val & gum::BinSearchTree< Val, Cmp, Node >::maxValue | ( | ) | const |
Returns the value of the rightmost node with the maximal key in the tree.
| NotFound | Raised if the tree is empty/ |
|
protected |
Returns the smallest node w.r.t.
order Cmp in the subtree rooted at node.
| node | The root for looking for the the smallest node. |
| const Val & gum::BinSearchTree< Val, Cmp, Node >::minValue | ( | ) | const |
Returns the value of the leftmost node with the minimal key in the tree.
| NotFound | Raised if the tree is empty. |
| BinSearchTree< Val, Cmp, Node > & gum::BinSearchTree< Val, Cmp, Node >::operator= | ( | const BinSearchTree< Val, Cmp, Node > & | from | ) |
Copy operator.
| from | The gum::BinSearchTree to copy. |
|
protected |
| iterator gum::BinSearchTree< Val, Cmp, Node >::rbegin | ( | ) |
Reverse iterator.
| const_iterator gum::BinSearchTree< Val, Cmp, Node >::rbegin | ( | ) | const |
Reverse iterator.
| const iterator & gum::BinSearchTree< Val, Cmp, Node >::rend | ( | ) |
Reverse end iterator.
| const const_iterator & gum::BinSearchTree< Val, Cmp, Node >::rend | ( | ) | const |
Reverse end iterator.
| iterator gum::BinSearchTree< Val, Cmp, Node >::root | ( | ) |
Returns an iterator at the root of the tree.
| const_iterator gum::BinSearchTree< Val, Cmp, Node >::root | ( | ) | const |
Returns an iterator at the root of the tree.
| const Val & gum::BinSearchTree< Val, Cmp, Node >::rootValue | ( | ) | const |
Returns the value of the root of the tree.
| NotFound | Raised if the tree is empty. |
| void gum::BinSearchTree< Val, Cmp, Node >::setUniquenessPolicy | ( | const bool | new_policy | ) |
Enables the user to change dynamically the policy for checking whether there can exist several identical elements in the binary tree.
By default, trees can store several times the same element. However, for some applications, we should ensure that all elements in the binary search tree are distinct.
| new_policy | Set the uniqueness policy on or off. |
| Size gum::BinSearchTree< Val, Cmp, Node >::size | ( | ) | const |
Returns the number of elements in the binary search tree.
|
protected |
|
virtual |
Displays the content of the tree, in increasing order w.r.t.
Cmp.
| bool gum::BinSearchTree< Val, Cmp, Node >::uniquenessPolicy | ( | ) | const |
Returns the current uniqueness policy.
|
friend |
|
protected |
The comparison function.
Definition at line 314 of file binSearchTree.h.
|
protected |
Pseudo static iterator.
The end and rend iterators are constructed only once per binary search tree so as to optimize for(iter = begin();iter != end(); iter++) loops: this will avoid creating objects end and rend each time we pass in the loop.
Definition at line 334 of file binSearchTree.h.
Referenced by BinSearchTreeIterator< Val, Cmp, Node >.
|
mutableprotected |
The list of iterators pointing to the binary search tree.
Definition at line 317 of file binSearchTree.h.
|
protected |
The number of elements stored in the tree.
Definition at line 324 of file binSearchTree.h.
|
protected |
The root node of the tree.
Definition at line 311 of file binSearchTree.h.
|
mutableprotected |
The uniqueness property: whether the same value can appear multiple times.
Definition at line 321 of file binSearchTree.h.