aGrUM  0.16.0
binSearchTree.h
Go to the documentation of this file.
1 
30 #ifndef GUM_BIN_SEARCH_TREE_H
31 #define GUM_BIN_SEARCH_TREE_H
32 
33 #include <agrum/agrum.h>
34 
35 #include <agrum/core/binTreeNode.h>
36 
37 namespace gum {
38 
39 #ifndef DOXYGEN_SHOULD_SKIP_THIS
40  // classes provided/used by this header
41  template < typename Val, class Cmp, class Node >
42  class BinSearchTree;
43 
44  template < typename Val, class Cmp, class Node >
45  class BinSearchTreeIterator;
46 #endif // DOXYGEN_SHOULD_SKIP_THIS
47 
48  // ===========================================================================
49  // === GENERIC BINARY SEARCH TREE ===
50  // ===========================================================================
51 
64  template < typename Val,
65  class Cmp = std::less< Val >,
66  class Node = BinTreeNode< Val > >
67  class BinSearchTree {
68  public:
74 
75  // ============================================================================
77  // ============================================================================
79 
88  explicit BinSearchTree(bool uniqueness_policy = false);
89 
95 
99  virtual ~BinSearchTree();
100 
102  // ============================================================================
104  // ============================================================================
106 
114 
116  // ============================================================================
118  // ============================================================================
120 
125  iterator begin();
127  const_iterator begin() const;
129 
135  iterator rbegin();
137  const_iterator rbegin() const;
139 
144  const iterator& end();
146  const const_iterator& end() const;
148 
154  const iterator& rend();
156  const const_iterator& rend() const;
158 
163  iterator root();
165  const_iterator root() const;
167 
169  // ============================================================================
171  // ============================================================================
173 
179  const Val& rootValue() const;
180 
188  const Val& minValue() const;
189 
197  const Val& maxValue() const;
198 
212  const Val& insert(const Val& val);
213 
219  void erase(const Val& val);
220 
229  void erase(const iterator& iter);
230 
236  bool contains(const Val& val) const;
237 
241  void clear();
242 
247  Size size() const;
248 
253  bool empty() const;
254 
260  virtual const std::string toString() const;
261 
263  // ============================================================================
265  // ============================================================================
267 
272  bool uniquenessPolicy() const;
273 
289  void setUniquenessPolicy(const bool new_policy);
290 
292 
293  protected:
295  Node* _root;
296 
298  Cmp _cmp;
299 
301  mutable iterator* _iterator_list;
302 
305  mutable bool _uniqueness_policy;
306 
309 
318  iterator _iter_end;
319 
322  friend class BinSearchTreeIterator< Val, Cmp, Node >;
324 
336  Node* _copy(Node* root_from,
337  Node* parent = 0,
339 
346  Node* _minNode(Node* node) const;
347 
354  Node* _maxNode(Node* node) const;
355 
361  Node* _succNode(Node* node) const;
362 
368  Node* _prevNode(Node* node) const;
369 
376  Node* _getNode(const Val& val) const;
377 
379 
388  void _deleteSubTree(Node* node);
389 
394  virtual void _erase(Node* node);
395 
396  private:
403  void __eraseWithTwoChildren(Node* node);
404 
405  protected:
422  virtual Node* _insert(const Val& val);
423 
424  private:
429  void __updateEraseIterators(Node* node);
430  };
431 
432  // ===========================================================================
433  // === GENERIC BINARY SEARCH TREE ITERATORS ===
434  // ===========================================================================
435 
447  template < typename Val, class Cmp, class Node >
449  public:
450  // ============================================================================
452  // ============================================================================
454 
459 
466 
469 
471  // ============================================================================
473  // ============================================================================
475 
483 
490  const Val& operator*() const;
491 
513 
534 
542  bool operator!=(const BinSearchTreeIterator< Val, Cmp, Node >& from) const;
543 
551  bool operator==(const BinSearchTreeIterator< Val, Cmp, Node >& from) const;
552 
554  // ############################################################################
556  // ############################################################################
558 
564 
571 
578 
582  void clear();
583 
585 
586  protected:
588  Node* _node;
589 
591  Node* _next_node;
592 
595  Node* _prev_node;
596 
598  Node* _parent;
599 
601  Node* _left_child;
602 
605 
608 
611 
612  private:
615  friend class BinSearchTree< Val, Cmp, Node >;
617 
630  void _initialize(const BinSearchTree< Val, Cmp, Node >* tree,
631  const Node* current_node,
632  bool add_to_iterator_list);
633 
638  void _detachFromTree();
639  };
640 
641 } /* namespace gum */
642 
643 
644 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
645 extern template class gum::BinSearchTree< int >;
646 #endif
647 
648 
649 // always include the template implementations
651 
652 #endif // GUM_BIN_SEARCH_TREE_H
iterator _iter_end
Pseudo static iterator.
virtual ~BinSearchTree()
Class destructor.
BinSearchTree< Val, Cmp, Node > * _tree
The binary search tree pointed to by the iterator.
A generic binary search tree.
Definition: binSearchTree.h:67
Node * _getNode(const Val &val) const
Returns the node containing a given value.
Node * _maxNode(Node *node) const
Returns the greatest node w.r.t.
Cmp _cmp
The comparison function.
Size size() const
Returns the number of elements in the binary search tree.
Node * _left_child
The left child to be used when _node=0 and leftdown() is applied.
Node * _next_node
The next node to be used when _node=0 (if a ++ operator is applied).
const Val & insert(const Val &val)
Creates a copy of the value, insert it in the tree and returns the copy value.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Definition: agrum.h:25
iterator rbegin()
Reverse iterator.
Node * _copy(Node *root_from, Node *parent=0, BinTreeDir dir=BinTreeDir::LEFT_CHILD)
A method for recursively copying the contents of a BinSearchTree.
void _deleteSubTree(Node *node)
A method for recursively deleting a subtree of the gum::BinSearchTree.
bool _uniqueness_policy
The uniqueness property: whether the same value can appear multiple times.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
BinSearchTreeIterator< Val, Cmp, Node > const_iterator
Alias for gum::BinSearchTree iterators.
Definition: binSearchTree.h:72
BinTreeDir
The direction of a given edge in a binary tree.
Definition: binTreeNode.h:37
bool empty() const
Indicates whether the gum::BinSearchTree search tree is empty.
iterator root()
Returns an iterator at 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 & rootValue() const
Returns the value of the root of the tree.
Node * _parent
The parent to be used when _node=0 (if operation up is applied).
const Val & maxValue() const
Returns the value of the rightmost node with the maximal key in the tree.
Node * _right_child
The right child to be used when _node=0 and rightdown() is applied.
Formula operator*(const Formula &a, const Formula &b)
Definition: formula_inl.h:489
BinSearchTree(bool uniqueness_policy=false)
Basic constructor: returns an empty binary search tree.
bool operator==(const TiXmlString &a, const TiXmlString &b)
Definition: tinystr.h:243
Node * _node
The current node pointed to by the iterator.
bool uniquenessPolicy() const
Returns the current uniqueness policy.
A Generic binary search tree.
iterator * _iterator_list
The list of iterators pointing to the binary search tree.
virtual Node * _insert(const Val &val)
Creates a copy of the value, insert it in the gum::BinSearchTree and returns the copy value...
BinSearchTreeIterator< Val, Cmp, Node > iterator
Alias for gum::BinSearchTree iterators.
Definition: binSearchTree.h:71
void __updateEraseIterators(Node *node)
Update all iterators when a given node is deleted.
void clear()
Removes all the elements from the gum::BinSearchTree.
virtual const std::string toString() const
Displays the content of the tree, in increasing order w.r.t.
const iterator & rend()
Reverse end iterator.
void erase(const Val &val)
Erase the leftmost node with the given (key,val) pair.
Size _nb_elements
The number of elements stored in the tree.
void setUniquenessPolicy(const bool new_policy)
Enables the user to change dynamically the policy for checking whether there can exist several identi...
bool operator!=(const TiXmlString &a, const TiXmlString &b)
Definition: tinystr.h:251
Node * _prev_node
The preceding node to be used when _node=0 (if a – operator is applied).
const iterator & end()
End iterator.
BinSearchTree< Val, Cmp, Node > & operator=(const BinSearchTree< Val, Cmp, Node > &from)
Copy operator.
BinSearchTreeIterator< Val, Cmp, Node > * _next_iter
The next iterator in the list of iterators of the binSearchTree.
Node * _succNode(Node *node) const
Returns the next node according to the weak ordering Cmp.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:48
bool contains(const Val &val) const
Returns true if the gum::BinSearchTree contains the value.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
virtual void _erase(Node *node)
Erase the node passed in argument.
iterator begin()
Begin iterator.
Node * _minNode(Node *node) const
Returns the smallest node w.r.t.
Node * _root
The root node of the tree.
void __eraseWithTwoChildren(Node *node)
Erase a node with two children.
Node * _prevNode(Node *node) const
Returns the previous node according to weak ordering Cmp.