aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
gum::BinSearchTree< Val, Cmp, Node > Class Template Reference

A generic binary search tree. More...

#include <agrum/tools/core/binSearchTree.h>

+ Collaboration diagram for gum::BinSearchTree< Val, Cmp, Node >:

Public Member Functions

Class constructors and destructors
 BinSearchTree (bool uniqueness_policy=false)
 Basic constructor: returns an empty binary search tree. More...
 
 BinSearchTree (const BinSearchTree< Val, Cmp, Node > &from)
 Copy constructor. More...
 
virtual ~BinSearchTree ()
 Class destructor. More...
 
Class operators
BinSearchTree< Val, Cmp, Node > & operator= (const BinSearchTree< Val, Cmp, Node > &from)
 Copy operator. More...
 
Class iterators
iterator begin ()
 Begin iterator. More...
 
const_iterator begin () const
 Begin iterator. More...
 
iterator rbegin ()
 Reverse iterator. More...
 
const_iterator rbegin () const
 Reverse iterator. More...
 
const iteratorend ()
 End iterator. More...
 
const const_iteratorend () const
 End iterator. More...
 
const iteratorrend ()
 Reverse end iterator. More...
 
const const_iteratorrend () const
 Reverse end iterator. More...
 
iterator root ()
 Returns an iterator at the root of the tree. More...
 
const_iterator root () const
 Returns an iterator at the root of the tree. More...
 
Class Accessors and Modifiers
const Val & rootValue () const
 Returns the value of the root of the tree. More...
 
const Val & minValue () const
 Returns the value of the leftmost node with the minimal key in the tree. More...
 
const Val & maxValue () const
 Returns the value of the rightmost node with the maximal key in the tree. More...
 
const Val & insert (const Val &val)
 Creates a copy of the value, insert it in the tree and returns the copy value. More...
 
void erase (const Val &val)
 Erase the leftmost node with the given (key,val) pair. More...
 
void erase (const iterator &iter)
 Erase the node pointed to by the iterator. More...
 
bool contains (const Val &val) const
 Returns true if the gum::BinSearchTree contains the value. More...
 
void clear ()
 Removes all the elements from the gum::BinSearchTree. More...
 
Size size () const
 Returns the number of elements in the binary search tree. More...
 
bool empty () const
 Indicates whether the gum::BinSearchTree search tree is empty. More...
 
virtual std::string toString () const
 Displays the content of the tree, in increasing order w.r.t. More...
 
Fine tuning
bool uniquenessPolicy () const
 Returns the current uniqueness policy. More...
 
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. More...
 

Public Types

typedef BinSearchTreeIterator< Val, Cmp, Node > iterator
 Alias for gum::BinSearchTree iterators. More...
 
typedef BinSearchTreeIterator< Val, Cmp, Node > const_iterator
 Alias for gum::BinSearchTree iterators. More...
 

Protected Attributes

Node * root_
 The root node of the tree. More...
 
Cmp cmp_
 The comparison function. More...
 
iteratoriterator_list_
 The list of iterators pointing to the binary search tree. More...
 
bool uniqueness_policy_
 The uniqueness property: whether the same value can appear multiple times. More...
 
Size nb_elements_
 The number of elements stored in the tree. More...
 
iterator iter_end_
 Pseudo static iterator. More...
 

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. More...
 
Node * minNode_ (Node *node) const
 Returns the smallest node w.r.t. More...
 
Node * maxNode_ (Node *node) const
 Returns the greatest node w.r.t. More...
 
Node * succNode_ (Node *node) const
 Returns the next node according to the weak ordering Cmp. More...
 
Node * prevNode_ (Node *node) const
 Returns the previous node according to weak ordering Cmp. More...
 
Node * getNode_ (const Val &val) const
 Returns the node containing a given value. More...
 
void deleteSubTree_ (Node *node)
 A method for recursively deleting a subtree of the gum::BinSearchTree. More...
 
virtual void erase_ (Node *node)
 Erase the node passed in argument. More...
 
virtual Node * insert_ (const Val &val)
 Creates a copy of the value, insert it in the gum::BinSearchTree and returns the copy value. More...
 

Friends

class BinSearchTreeIterator< Val, Cmp, Node >
 To speed-up accesses. More...
 

Detailed Description

template<typename Val, class Cmp = std::less< Val >, class Node = BinTreeNode< Val >>
class gum::BinSearchTree< Val, Cmp, Node >

A generic binary search tree.

Template Parameters
ValThe values type to store in the binary search tree.
CmpThe comparator for sorting the binary search tree.
NodeThe nodes type used to store values in the binary search tree.

Definition at line 64 of file binSearchTree.h.

Member Typedef Documentation

◆ const_iterator

template<typename Val , class Cmp = std::less< Val >, class Node = BinTreeNode< Val >>
typedef BinSearchTreeIterator< Val, Cmp, Node > gum::BinSearchTree< Val, Cmp, Node >::const_iterator

Alias for gum::BinSearchTree iterators.

Definition at line 69 of file binSearchTree.h.

◆ iterator

template<typename Val , class Cmp = std::less< Val >, class Node = BinTreeNode< Val >>
typedef BinSearchTreeIterator< Val, Cmp, Node > gum::BinSearchTree< Val, Cmp, Node >::iterator

Alias for gum::BinSearchTree iterators.

Definition at line 68 of file binSearchTree.h.

Constructor & Destructor Documentation

◆ BinSearchTree() [1/2]

template<typename Val , class Cmp = std::less< Val >, class Node = BinTreeNode< Val >>
gum::BinSearchTree< Val, Cmp, Node >::BinSearchTree ( bool  uniqueness_policy = false)
explicit

Basic constructor: returns an empty binary search tree.

Parameters
uniqueness_policyAllows (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.

◆ BinSearchTree() [2/2]

template<typename Val , class Cmp = std::less< Val >, class Node = BinTreeNode< Val >>
gum::BinSearchTree< Val, Cmp, Node >::BinSearchTree ( const BinSearchTree< Val, Cmp, Node > &  from)

Copy constructor.

Parameters
fromThe gum::BinSearchTree to copy.

◆ ~BinSearchTree()

template<typename Val , class Cmp = std::less< Val >, class Node = BinTreeNode< Val >>
virtual gum::BinSearchTree< Val, Cmp, Node >::~BinSearchTree ( )
virtual

Class destructor.

Member Function Documentation

◆ _eraseWithTwoChildren_()

template<typename Val , class Cmp = std::less< Val >, class Node = BinTreeNode< Val >>
void gum::BinSearchTree< Val, Cmp, Node >::_eraseWithTwoChildren_ ( Node *  node)
private

Erase a node with two children.

This is used by gum::BinSearchTree::erase_(Node*).

Parameters
nodeThe node to erase.

◆ _updateEraseIterators_()

template<typename Val , class Cmp = std::less< Val >, class Node = BinTreeNode< Val >>
void gum::BinSearchTree< Val, Cmp, Node >::_updateEraseIterators_ ( Node *  node)
private

Update all iterators when a given node is deleted.

Parameters
nodeThe node that is erased.

◆ begin() [1/2]

template<typename Val , class Cmp = std::less< Val >, class Node = BinTreeNode< Val >>
iterator gum::BinSearchTree< Val, Cmp, Node >::begin ( )

Begin iterator.

Returns
Returns an iterator at the beginning of the gum::BinSearchTree.

◆ begin() [2/2]

template<typename Val , class Cmp = std::less< Val >, class Node = BinTreeNode< Val >>
const_iterator gum::BinSearchTree< Val, Cmp, Node >::begin ( ) const

Begin iterator.

Returns
Returns an iterator at the beginning of the gum::BinSearchTree.

◆ clear()

template<typename Val , class Cmp = std::less< Val >, class Node = BinTreeNode< Val >>
void gum::BinSearchTree< Val, Cmp, Node >::clear ( )

Removes all the elements from the gum::BinSearchTree.

◆ contains()

template<typename Val , class Cmp = std::less< Val >, class Node = BinTreeNode< Val >>
bool gum::BinSearchTree< Val, Cmp, Node >::contains ( const Val &  val) const

Returns true if the gum::BinSearchTree contains the value.

Parameters
valThe value tested for existence.
Returns
Returns true if the gum::BinSearchTree contains the value.

◆ copy_()

template<typename Val , class Cmp = std::less< Val >, class Node = BinTreeNode< Val >>
Node* gum::BinSearchTree< Val, Cmp, Node >::copy_ ( Node *  root_from,
Node *  parent = 0,
BinTreeDir  dir = BinTreeDir::LEFT_CHILD 
)
protected

A method for recursively copying the contents of a BinSearchTree.

Warning
This function produces a tree with precisely the same structure as that passed in argument (node). Hence, this should be used only when copying binary search trees storing data w.r.t. the same weak order Cmp.
Parameters
root_fromThe root of the tree to be copied.
parentThe node that should be the parent of the copy.
dirThe direction of the edge parent->copy.

◆ deleteSubTree_()

template<typename Val , class Cmp = std::less< Val >, class Node = BinTreeNode< Val >>
void gum::BinSearchTree< Val, Cmp, Node >::deleteSubTree_ ( Node *  node)
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.

Parameters
nodeThe root of the subtree to delete.

◆ empty()

template<typename Val , class Cmp = std::less< Val >, class Node = BinTreeNode< Val >>
bool gum::BinSearchTree< Val, Cmp, Node >::empty ( ) const

Indicates whether the gum::BinSearchTree search tree is empty.

Returns
Returns true if the gum::BinSearchTree is empty.

◆ end() [1/2]

template<typename Val , class Cmp = std::less< Val >, class Node = BinTreeNode< Val >>
const iterator& gum::BinSearchTree< Val, Cmp, Node >::end ( )

End iterator.

Returns
Returns an iterator at the end of the gum::BinSearchTree.

◆ end() [2/2]

template<typename Val , class Cmp = std::less< Val >, class Node = BinTreeNode< Val >>
const const_iterator& gum::BinSearchTree< Val, Cmp, Node >::end ( ) const

End iterator.

Returns
Returns an iterator at the end of the gum::BinSearchTree.

◆ erase() [1/2]

template<typename Val , class Cmp = std::less< Val >, class Node = BinTreeNode< Val >>
void gum::BinSearchTree< Val, Cmp, Node >::erase ( const Val &  val)

Erase the leftmost node with the given (key,val) pair.

Parameters
valThe value to remove.
Exceptions
NotFoundRaised if we could not find the node.

◆ erase() [2/2]

template<typename Val , class Cmp = std::less< Val >, class Node = BinTreeNode< Val >>
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.

Parameters
iterThe iterator pointing toward the value to remove.

◆ erase_()

template<typename Val , class Cmp = std::less< Val >, class Node = BinTreeNode< Val >>
virtual void gum::BinSearchTree< Val, Cmp, Node >::erase_ ( Node *  node)
protectedvirtual

Erase the node passed in argument.

Parameters
nodeThe Node to erase.

◆ getNode_()

template<typename Val , class Cmp = std::less< Val >, class Node = BinTreeNode< Val >>
Node* gum::BinSearchTree< Val, Cmp, Node >::getNode_ ( const Val &  val) const
protected

Returns the node containing a given value.

Parameters
valThe value of the node to return.
Returns
Returns the node containing a given value (0 if the value cannot be found).

◆ insert()

template<typename Val , class Cmp = std::less< Val >, class Node = BinTreeNode< Val >>
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.

Parameters
valThe value added by copy.
Returns
Returns a reference over copy added in the gum::BinSearchTree.
Exceptions
DuplicateElementRaised if the binary tree already contains the value and the uniqueness property is set to true.

◆ insert_()

template<typename Val , class Cmp = std::less< Val >, class Node = BinTreeNode< Val >>
virtual Node* gum::BinSearchTree< Val, Cmp, Node >::insert_ ( const Val &  val)
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.

Warning
this method is actually the implementation of method insert. It is used to speed-up insertions in terminal classes such as AVL trees.
Exceptions
DuplicateElementexception is raised if the binary tree already contains the value and the uniqueness property is set to true.
Parameters
valThe value added by copy.
Returns
Returns the node containing the newly created copy.

◆ maxNode_()

template<typename Val , class Cmp = std::less< Val >, class Node = BinTreeNode< Val >>
Node* gum::BinSearchTree< Val, Cmp, Node >::maxNode_ ( Node *  node) const
protected

Returns the greatest node w.r.t.

order Cmp in the subtree rooted at node.

Parameters
nodeThe root for looking for the the greatest node.
Returns
Returns the greatest node.

◆ maxValue()

template<typename Val , class Cmp = std::less< Val >, class Node = BinTreeNode< Val >>
const Val& gum::BinSearchTree< Val, Cmp, Node >::maxValue ( ) const

Returns the value of the rightmost node with the maximal key in the tree.

Returns
Returns the value of the rightmost node with the maximal key in the tree.
Exceptions
NotFoundRaised if the tree is empty/

◆ minNode_()

template<typename Val , class Cmp = std::less< Val >, class Node = BinTreeNode< Val >>
Node* gum::BinSearchTree< Val, Cmp, Node >::minNode_ ( Node *  node) const
protected

Returns the smallest node w.r.t.

order Cmp in the subtree rooted at node.

Parameters
nodeThe root for looking for the the smallest node.
Returns
Returns the smallest node.

◆ minValue()

template<typename Val , class Cmp = std::less< Val >, class Node = BinTreeNode< Val >>
const Val& gum::BinSearchTree< Val, Cmp, Node >::minValue ( ) const

Returns the value of the leftmost node with the minimal key in the tree.

Returns
Returns the value of the leftmost node with the minimal key in the tree.
Exceptions
NotFoundRaised if the tree is empty.

◆ operator=()

template<typename Val , class Cmp = std::less< Val >, class Node = BinTreeNode< Val >>
BinSearchTree< Val, Cmp, Node >& gum::BinSearchTree< Val, Cmp, Node >::operator= ( const BinSearchTree< Val, Cmp, Node > &  from)

Copy operator.

Parameters
fromThe gum::BinSearchTree to copy.
Returns
Returns this gum::BinSearchTree.

◆ prevNode_()

template<typename Val , class Cmp = std::less< Val >, class Node = BinTreeNode< Val >>
Node* gum::BinSearchTree< Val, Cmp, Node >::prevNode_ ( Node *  node) const
protected

Returns the previous node according to weak ordering Cmp.

Parameters
nodeThe node for which the previous node is returned.
Returns
Returns the previous node according to the weak ordering Cmp.

◆ rbegin() [1/2]

template<typename Val , class Cmp = std::less< Val >, class Node = BinTreeNode< Val >>
iterator gum::BinSearchTree< Val, Cmp, Node >::rbegin ( )

Reverse iterator.

Returns
Returns an iterator at the beginning of the gum::BinSearchTree for reverse iteration.

◆ rbegin() [2/2]

template<typename Val , class Cmp = std::less< Val >, class Node = BinTreeNode< Val >>
const_iterator gum::BinSearchTree< Val, Cmp, Node >::rbegin ( ) const

Reverse iterator.

Returns
Returns an iterator at the beginning of the gum::BinSearchTree for reverse iteration.

◆ rend() [1/2]

template<typename Val , class Cmp = std::less< Val >, class Node = BinTreeNode< Val >>
const iterator& gum::BinSearchTree< Val, Cmp, Node >::rend ( )

Reverse end iterator.

Returns
Returns an iterator at the end of the gum::BinSearchTree for reverse iteration.

◆ rend() [2/2]

template<typename Val , class Cmp = std::less< Val >, class Node = BinTreeNode< Val >>
const const_iterator& gum::BinSearchTree< Val, Cmp, Node >::rend ( ) const

Reverse end iterator.

Returns
Returns an iterator at the end of the gum::BinSearchTree for reverse iteration.

◆ root() [1/2]

template<typename Val , class Cmp = std::less< Val >, class Node = BinTreeNode< Val >>
iterator gum::BinSearchTree< Val, Cmp, Node >::root ( )

Returns an iterator at the root of the tree.

Returns
Returns an iterator at the root of the tree.

◆ root() [2/2]

template<typename Val , class Cmp = std::less< Val >, class Node = BinTreeNode< Val >>
const_iterator gum::BinSearchTree< Val, Cmp, Node >::root ( ) const

Returns an iterator at the root of the tree.

Returns
Returns an iterator at the root of the tree.

◆ rootValue()

template<typename Val , class Cmp = std::less< Val >, class Node = BinTreeNode< Val >>
const Val& gum::BinSearchTree< Val, Cmp, Node >::rootValue ( ) const

Returns the value of the root of the tree.

Returns
Returns the value of the root of the tree.
Exceptions
NotFoundRaised if the tree is empty.

◆ setUniquenessPolicy()

template<typename Val , class Cmp = std::less< Val >, class Node = BinTreeNode< Val >>
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.

Warning
When setting the policy to "uniqueness", the function does not check whether the tree already contains identical elements. It thus only ensures that elements inserted from now on do not already belong to the tree.
Parameters
new_policySet the uniqueness policy on or off.

◆ size()

template<typename Val , class Cmp = std::less< Val >, class Node = BinTreeNode< Val >>
Size gum::BinSearchTree< Val, Cmp, Node >::size ( ) const

Returns the number of elements in the binary search tree.

Returns
Returns the number of elements in the binary search tree.

◆ succNode_()

template<typename Val , class Cmp = std::less< Val >, class Node = BinTreeNode< Val >>
Node* gum::BinSearchTree< Val, Cmp, Node >::succNode_ ( Node *  node) const
protected

Returns the next node according to the weak ordering Cmp.

Parameters
nodeThe node for which the sucessor is returned.
Returns
Returns the next node according to the weak ordering Cmp.

◆ toString()

template<typename Val , class Cmp = std::less< Val >, class Node = BinTreeNode< Val >>
virtual std::string gum::BinSearchTree< Val, Cmp, Node >::toString ( ) const
virtual

Displays the content of the tree, in increasing order w.r.t.

Cmp.

Returns
Returns the content of the tree in a string.

◆ uniquenessPolicy()

template<typename Val , class Cmp = std::less< Val >, class Node = BinTreeNode< Val >>
bool gum::BinSearchTree< Val, Cmp, Node >::uniquenessPolicy ( ) const

Returns the current uniqueness policy.

Returns
Returns the current uniqueness policy.

Friends And Related Function Documentation

◆ BinSearchTreeIterator< Val, Cmp, Node >

template<typename Val , class Cmp = std::less< Val >, class Node = BinTreeNode< Val >>
friend class BinSearchTreeIterator< Val, Cmp, Node >
friend

To speed-up accesses.

Definition at line 318 of file binSearchTree.h.

Member Data Documentation

◆ cmp_

template<typename Val , class Cmp = std::less< Val >, class Node = BinTreeNode< Val >>
Cmp gum::BinSearchTree< Val, Cmp, Node >::cmp_
protected

The comparison function.

Definition at line 294 of file binSearchTree.h.

◆ iter_end_

template<typename Val , class Cmp = std::less< Val >, class Node = BinTreeNode< Val >>
iterator gum::BinSearchTree< Val, Cmp, Node >::iter_end_
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 314 of file binSearchTree.h.

◆ iterator_list_

template<typename Val , class Cmp = std::less< Val >, class Node = BinTreeNode< Val >>
iterator* gum::BinSearchTree< Val, Cmp, Node >::iterator_list_
mutableprotected

The list of iterators pointing to the binary search tree.

Definition at line 297 of file binSearchTree.h.

◆ nb_elements_

template<typename Val , class Cmp = std::less< Val >, class Node = BinTreeNode< Val >>
Size gum::BinSearchTree< Val, Cmp, Node >::nb_elements_
protected

The number of elements stored in the tree.

Definition at line 304 of file binSearchTree.h.

◆ root_

template<typename Val , class Cmp = std::less< Val >, class Node = BinTreeNode< Val >>
Node* gum::BinSearchTree< Val, Cmp, Node >::root_
protected

The root node of the tree.

Definition at line 291 of file binSearchTree.h.

◆ uniqueness_policy_

template<typename Val , class Cmp = std::less< Val >, class Node = BinTreeNode< Val >>
bool gum::BinSearchTree< Val, Cmp, Node >::uniqueness_policy_
mutableprotected

The uniqueness property: whether the same value can appear multiple times.

Definition at line 301 of file binSearchTree.h.


The documentation for this class was generated from the following file: