aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
binSearchTree.h
Go to the documentation of this file.
1/****************************************************************************
2 * This file is part of the aGrUM/pyAgrum library. *
3 * *
4 * Copyright (c) 2005-2025 by *
5 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
6 * - Christophe GONZALES(_at_AMU) *
7 * *
8 * The aGrUM/pyAgrum library is free software; you can redistribute it *
9 * and/or modify it under the terms of either : *
10 * *
11 * - the GNU Lesser General Public License as published by *
12 * the Free Software Foundation, either version 3 of the License, *
13 * or (at your option) any later version, *
14 * - the MIT license (MIT), *
15 * - or both in dual license, as here. *
16 * *
17 * (see https://agrum.gitlab.io/articles/dual-licenses-lgplv3mit.html) *
18 * *
19 * This aGrUM/pyAgrum library is distributed in the hope that it will be *
20 * useful, but WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
21 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES MERCHANTABILITY or FITNESS *
22 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
25 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
26 * OTHER DEALINGS IN THE SOFTWARE. *
27 * *
28 * See LICENCES for more details. *
29 * *
30 * SPDX-FileCopyrightText: Copyright 2005-2025 *
31 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
32 * - Christophe GONZALES(_at_AMU) *
33 * SPDX-License-Identifier: LGPL-3.0-or-later OR MIT *
34 * *
35 * Contact : info_at_agrum_dot_org *
36 * homepage : http://agrum.gitlab.io *
37 * gitlab : https://gitlab.com/agrumery/agrum *
38 * *
39 ****************************************************************************/
40
41
48
49#ifndef GUM_BIN_SEARCH_TREE_H
50#define GUM_BIN_SEARCH_TREE_H
51
52#include <agrum/agrum.h>
53
55
56namespace gum {
57
58#ifndef DOXYGEN_SHOULD_SKIP_THIS
59 // classes provided/used by this header
60 template < typename Val, class Cmp, class Node >
61 class BinSearchTree;
62
63 template < typename Val, class Cmp, class Node >
65#endif // DOXYGEN_SHOULD_SKIP_THIS
66
67 // ===========================================================================
68 // === GENERIC BINARY SEARCH TREE ===
69 // ===========================================================================
70
83 template < typename Val, class Cmp = std::less< Val >, class Node = BinTreeNode< Val > >
85 public:
91
92 // ============================================================================
94 // ============================================================================
96
105 explicit BinSearchTree(bool uniqueness_policy = false);
106
112
116 virtual ~BinSearchTree();
117
119 // ============================================================================
121 // ============================================================================
123
130
132 // ============================================================================
134 // ============================================================================
136
145
155
161 const iterator& end();
162 const const_iterator& end() const;
164
171 const iterator& rend();
172 const const_iterator& rend() const;
174
183
185 // ============================================================================
187 // ============================================================================
189
195 const Val& rootValue() const;
196
204 const Val& minValue() const;
205
213 const Val& maxValue() const;
214
228 const Val& insert(const Val& val);
229
235 void erase(const Val& val);
236
245 void erase(const iterator& iter);
246
252 bool contains(const Val& val) const;
253
257 void clear();
258
263 Size size() const;
264
269 bool empty() const;
270
276 virtual std::string toString() const;
277
279 // ============================================================================
281 // ============================================================================
283
288 bool uniquenessPolicy() const;
289
305 void setUniquenessPolicy(const bool new_policy);
306
308
309 protected:
311 Node* root_;
312
315
318
321 mutable bool uniqueness_policy_;
322
325
335
338 friend class BinSearchTreeIterator< Val, Cmp, Node >;
340
352 Node* copy_(Node* root_from, Node* parent = 0, BinTreeDir dir = BinTreeDir::LEFT_CHILD);
353
360 Node* minNode_(Node* node) const;
361
368 Node* maxNode_(Node* node) const;
369
375 Node* succNode_(Node* node) const;
376
382 Node* prevNode_(Node* node) const;
383
390 Node* getNode_(const Val& val) const;
391
393
402 void deleteSubTree_(Node* node);
403
408 virtual void erase_(Node* node);
409
410 private:
417 void _eraseWithTwoChildren_(Node* node);
418
419 protected:
436 virtual Node* insert_(const Val& val);
437
438 private:
443 void _updateEraseIterators_(Node* node);
444 };
445
446 // ===========================================================================
447 // === GENERIC BINARY SEARCH TREE ITERATORS ===
448 // ===========================================================================
449
461 template < typename Val, class Cmp, class Node >
463 public:
464 // ============================================================================
466 // ============================================================================
468
473
480
483
485 // ============================================================================
487 // ============================================================================
489
497
504 const Val& operator*() const;
505
527
548
557
566
568 // ############################################################################
570 // ############################################################################
572
578
585
592
596 void clear();
597
599
600 protected:
602 Node* node_;
603
606
610
612 Node* parent_;
613
616
619
622
625
626 private:
629 friend class BinSearchTree< Val, Cmp, Node >;
631
645 const Node* current_node,
646 bool add_to_iterator_list);
647
653 };
654
655} /* namespace gum */
656
657
658#ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
659extern template class gum::BinSearchTree< int >;
660#endif
661
662
663// always include the template implementations
665
666#endif // GUM_BIN_SEARCH_TREE_H
Basic binary tree.
A Generic binary search tree.
bool operator==(const BinSearchTreeIterator< Val, Cmp, Node > &from) const
Checks whether two iterators are pointing toward identical elements.
bool operator!=(const BinSearchTreeIterator< Val, Cmp, Node > &from) const
Checks whether two iterators are pointing toward different elements.
Node * node_
The current node pointed to by the iterator.
BinSearchTreeIterator< Val, Cmp, Node > & up()
Makes the iterator move to its parent node.
BinSearchTree< Val, Cmp, Node > * tree_
The binary search tree pointed to by the iterator.
Node * next_node_
The next node to be used when node_=nullptr (if a ++ operator is applied).
BinSearchTreeIterator()
Class Constructors and Destructors.
Node * prev_node_
The preceding node to be used when node_=nullptr (if a – operator is applied).
void initialize_(const BinSearchTree< Val, Cmp, Node > *tree, const Node *current_node, bool add_to_iterator_list)
A function called to initialize an iterator.
~BinSearchTreeIterator()
Class destructor.
void clear()
Detach the iterator from its current tree (if any) and reset it.
Node * parent_
The parent to be used when node_=nullptr (if operation up is applied).
BinSearchTreeIterator< Val, Cmp, Node > & operator--()
Point the iterator to the preceding value in the binary search tree.
const Val & operator*() const
Returns the value pointed to by the iterator.
BinSearchTreeIterator< Val, Cmp, Node > & downLeft()
Makes the iterator move to the left child of the node it points to.
void detachFromTree_()
a method to detach the current iterator from its tree's iterator's list.
friend class BinSearchTree< Val, Cmp, Node >
To speed-up accesses.
Node * left_child_
The left child to be used when node_=nullptr and leftdown() is applied.
Node * right_child_
The right child to be used when node_=nullptr and rightdown() is applied.
BinSearchTreeIterator(const BinSearchTreeIterator< Val, Cmp, Node > &from)
Copy constructor: creates an iterator pointing toward the same tree.
BinSearchTreeIterator< Val, Cmp, Node > * next_iter_
The next iterator in the list of iterators of the binSearchTree.
BinSearchTreeIterator< Val, Cmp, Node > & operator++()
Point the iterator to the next value in the binary search tree.
BinSearchTreeIterator< Val, Cmp, Node > & downRight()
Makes the iterator move to the right child of the node it points to.
BinSearchTreeIterator< Val, Cmp, Node > & operator=(const BinSearchTreeIterator< Val, Cmp, Node > &from)
Class operators.
A generic binary search tree.
Size size() const
Returns the number of elements in the binary search tree.
void setUniquenessPolicy(const bool new_policy)
Enables the user to change dynamically the policy for checking whether there can exist several identi...
iterator rbegin()
Reverse iterator.
virtual Node * insert_(const Val &val)
Creates a copy of the value, insert it in the gum::BinSearchTree and returns the copy value.
const_iterator rbegin() const
Reverse iterator.
BinSearchTree(const BinSearchTree< Val, Cmp, Node > &from)
Copy constructor.
virtual std::string toString() const
Displays the content of the tree, in increasing order w.r.t.
BinSearchTreeIterator< Val, Cmp, Node > const_iterator
Alias for gum::BinSearchTree iterators.
const_iterator root() const
Returns an iterator at the root of the tree.
void _updateEraseIterators_(Node *node)
Update all iterators when a given node is deleted.
BinSearchTreeIterator< Val, Cmp, Node > iterator
Alias for gum::BinSearchTree iterators.
Node * getNode_(const Val &val) const
Returns the node containing a given value.
virtual void erase_(Node *node)
Erase the node passed in argument.
Node * root_
The root node of the tree.
bool uniqueness_policy_
The uniqueness property: whether the same value can appear multiple times.
const_iterator begin() const
Begin iterator.
const Val & rootValue() const
Returns the value of the root of the tree.
void deleteSubTree_(Node *node)
A method for recursively deleting a subtree of the gum::BinSearchTree.
const const_iterator & rend() const
Reverse end iterator.
void erase(const iterator &iter)
Erase the node pointed to by the iterator.
const Val & maxValue() const
Returns the value of the rightmost node with the maximal key in the tree.
Node * succNode_(Node *node) const
Returns the next node according to the weak ordering Cmp.
iterator begin()
Begin iterator.
const Val & insert(const Val &val)
Creates a copy of the value, insert it in the tree and returns the copy value.
void _eraseWithTwoChildren_(Node *node)
Erase a node with two children.
bool empty() const
Indicates whether the gum::BinSearchTree search tree is empty.
void clear()
Removes all the elements from the gum::BinSearchTree.
const const_iterator & end() const
End iterator.
const iterator & end()
End iterator.
iterator root()
Returns an iterator at the root of the tree.
Node * minNode_(Node *node) const
Returns the smallest node w.r.t.
friend class BinSearchTreeIterator< Val, Cmp, Node >
To speed-up accesses.
Node * copy_(Node *root_from, Node *parent=0, BinTreeDir dir=BinTreeDir::LEFT_CHILD)
A method for recursively copying the contents of a BinSearchTree.
Node * maxNode_(Node *node) const
Returns the greatest node w.r.t.
Cmp cmp_
The comparison function.
bool uniquenessPolicy() const
Returns the current uniqueness policy.
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.
bool contains(const Val &val) const
Returns true if the gum::BinSearchTree contains the value.
const iterator & rend()
Reverse end iterator.
BinSearchTree< Val, Cmp, Node > & operator=(const BinSearchTree< Val, Cmp, Node > &from)
Copy operator.
virtual ~BinSearchTree()
Class destructor.
iterator * iterator_list_
The list of iterators pointing to the binary search tree.
Node * prevNode_(Node *node) const
Returns the previous node according to weak ordering Cmp.
iterator iter_end_
Pseudo static iterator.
const Val & minValue() const
Returns the value of the leftmost node with the minimal key in the tree.
BinSearchTree(bool uniqueness_policy=false)
Basic constructor: returns an empty binary search tree.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition types.h:74
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
BinTreeDir
The direction of a given edge in a binary tree.
Definition binTreeNode.h:56