33 #ifndef DOXYGEN_SHOULD_SKIP_THIS 43 template <
typename Val,
class Cmp,
class Node >
45 _node(0), _next_node(0), _prev_node(0), _parent(0), _left_child(0),
46 _right_child(0), _tree(0), _next_iter(0) {
50 template <
typename Val,
class Cmp,
class Node >
52 const BinSearchTreeIterator< Val, Cmp, Node >& from) :
61 _tree->_iterator_list =
this;
66 template <
typename Val,
class Cmp,
class Node >
69 const Node* current_node,
70 bool add_to_iterator_list) {
75 _node =
const_cast< Node*
>(current_node);
77 if (add_to_iterator_list &&
_tree) {
79 _tree->_iterator_list =
this;
83 template <
typename Val,
class Cmp,
class Node >
86 BinSearchTreeIterator< Val, Cmp, Node >*iter, *prev_iter = 0;
88 for (iter =
_tree->_iterator_list; iter !=
this && iter;
89 prev_iter = iter, iter = iter->_next_iter) {}
100 template <
typename Val,
class Cmp,
class Node >
108 template <
typename Val,
class Cmp,
class Node >
124 template <
typename Val,
class Cmp,
class Node >
125 INLINE BinSearchTreeIterator< Val, Cmp, Node >&
127 operator=(
const BinSearchTreeIterator< Val, Cmp, Node >& from) {
134 if (from._tree !=
_tree) {
140 _tree->_iterator_list =
this;
157 template <
typename Val,
class Cmp,
class Node >
159 if (_node)
return _node->value();
162 "the iterator does not point to a node of the binary tree");
165 template <
typename Val,
class Cmp,
class Node >
169 for (; node; prevNode = node, node = node->leftChild()) {}
174 template <
typename Val,
class Cmp,
class Node >
178 for (; node; prevNode = node, node = node->rightChild()) {}
183 template <
typename Val,
class Cmp,
class Node >
187 if (node->rightChild())
return _minNode(node->rightChild());
189 Node* par = node->parent();
199 template <
typename Val,
class Cmp,
class Node >
203 if (node->leftChild())
return _maxNode(node->leftChild());
205 Node* par = node->parent();
215 template <
typename Val,
class Cmp,
class Node >
216 INLINE BinSearchTreeIterator< Val, Cmp, Node >&
234 template <
typename Val,
class Cmp,
class Node >
235 INLINE BinSearchTreeIterator< Val, Cmp, Node >&
254 template <
typename Val,
class Cmp,
class Node >
256 operator==(
const BinSearchTreeIterator< Val, Cmp, Node >& from)
const {
258 return (_node == from._node);
260 return ((_node == from._node) && (
_tree == from._tree)
266 template <
typename Val,
class Cmp,
class Node >
268 operator!=(
const BinSearchTreeIterator< Val, Cmp, Node >& from)
const {
270 return (_node != from._node);
272 return ((_node != from._node) || (
_tree != from._tree)
278 template <
typename Val,
class Cmp,
class Node >
279 INLINE BinSearchTreeIterator< Val, Cmp, Node >&
284 _node = _node ? _node->parent() :
_parent;
297 template <
typename Val,
class Cmp,
class Node >
298 INLINE BinSearchTreeIterator< Val, Cmp, Node >&
316 template <
typename Val,
class Cmp,
class Node >
317 INLINE BinSearchTreeIterator< Val, Cmp, Node >&
341 template <
typename Val,
class Cmp,
class Node >
343 _root(0), _iterator_list(0), _uniqueness_policy(uniqueness_policy),
345 GUM_CONSTRUCTOR(BinSearchTree);
346 _iter_end._initialize(
this, 0,
false);
349 template <
typename Val,
class Cmp,
class Node >
353 _iterator_list(0), _uniqueness_policy(from._uniqueness_policy) {
355 GUM_CONS_CPY(BinSearchTree);
358 _root = _copy(from._root);
359 _nb_elements = from._nb_elements;
362 _iter_end._initialize(
this, 0,
false);
365 template <
typename Val,
class Cmp,
class Node >
368 for (iterator *iter = _iterator_list, *next_iter = 0; iter; iter = next_iter) {
369 next_iter = iter->_next_iter;
374 _deleteSubTree(_root);
382 template <
typename Val,
class Cmp,
class Node >
388 GUM_OP_CPY(BinSearchTree);
394 _uniqueness_policy = from._uniqueness_policy;
395 _root = _copy(from._root);
397 _nb_elements = from._nb_elements;
407 template <
typename Val,
class Cmp,
class Node >
410 GUM_DESTRUCTOR(BinSearchTree);
416 template <
typename Val,
class Cmp,
class Node >
424 Node* new_node =
new Node(*node);
426 if (parent) parent->insertChild(*new_node, dir);
435 template <
typename Val,
class Cmp,
class Node >
441 _deleteSubTree(node->leftChild());
442 _deleteSubTree(node->rightChild());
448 template <
typename Val,
class Cmp,
class Node >
456 if (_cmp(val, node->value()))
457 if (!node->leftChild()) {
460 return node->insertLeftChild(val);
462 node = node->leftChild();
464 else if (_cmp(node->value(), val) || !_uniqueness_policy)
465 if (!node->rightChild()) {
468 return node->insertRightChild(val);
470 node = node->rightChild();
476 "Val " << val <<
" already in the binary search tree");
482 _root =
new Node(val);
487 template <
typename Val,
class Cmp,
class Node >
489 return _insert(val)->value();
492 template <
typename Val,
class Cmp,
class Node >
495 GUM_ERROR(NotFound,
"no value in an empty Binary Search tree");
498 return _root->value();
501 template <
typename Val,
class Cmp,
class Node >
504 GUM_ERROR(NotFound,
"no minimal value in an empty Binary Search tree");
507 return _minNode(_root)->value();
510 template <
typename Val,
class Cmp,
class Node >
513 GUM_ERROR(NotFound,
"no maximal value in an empty Binary Search tree");
516 return _maxNode(_root)->value();
519 template <
typename Val,
class Cmp,
class Node >
527 if (_cmp(val, node->value())) {
528 if (!node->leftChild())
531 node = node->leftChild();
532 }
else if (_cmp(node->value(), val)) {
533 if (!node->rightChild())
536 node = node->rightChild();
545 template <
typename Val,
class Cmp,
class Node >
547 return (_getNode(val) != 0);
550 template <
typename Val,
class Cmp,
class Node >
555 template <
typename Val,
class Cmp,
class Node >
557 return (_nb_elements == 0);
560 template <
typename Val,
class Cmp,
class Node >
563 std::stringstream stream;
566 for (const_iterator iter = begin(); iter != end(); ++iter, deja =
true) {
567 if (deja) stream <<
" , ";
577 template <
typename Val,
class Cmp,
class Node >
579 return _uniqueness_policy;
582 template <
typename Val,
class Cmp,
class Node >
585 _uniqueness_policy = new_policy;
588 template <
typename Val,
class Cmp,
class Node >
589 INLINE BinSearchTreeIterator< Val, Cmp, Node >
591 BinSearchTreeIterator< Val, Cmp, Node > iter;
592 iter._initialize(
this, _minNode(_root),
true);
596 template <
typename Val,
class Cmp,
class Node >
597 INLINE BinSearchTreeIterator< Val, Cmp, Node >
599 BinSearchTreeIterator< Val, Cmp, Node > iter;
600 iter._initialize(
this, _minNode(_root),
true);
604 template <
typename Val,
class Cmp,
class Node >
605 INLINE BinSearchTreeIterator< Val, Cmp, Node >
607 BinSearchTreeIterator< Val, Cmp, Node > iter;
608 iter._initialize(
this, _maxNode(_root),
true);
612 template <
typename Val,
class Cmp,
class Node >
613 INLINE BinSearchTreeIterator< Val, Cmp, Node >
615 BinSearchTreeIterator< Val, Cmp, Node > iter;
616 iter._initialize(
this, _maxNode(_root),
true);
620 template <
typename Val,
class Cmp,
class Node >
621 INLINE
const BinSearchTreeIterator< Val, Cmp, Node >&
626 template <
typename Val,
class Cmp,
class Node >
627 INLINE
const BinSearchTreeIterator< Val, Cmp, Node >&
632 template <
typename Val,
class Cmp,
class Node >
633 INLINE
const BinSearchTreeIterator< Val, Cmp, Node >&
638 template <
typename Val,
class Cmp,
class Node >
639 INLINE
const BinSearchTreeIterator< Val, Cmp, Node >&
644 template <
typename Val,
class Cmp,
class Node >
645 INLINE BinSearchTreeIterator< Val, Cmp, Node >
647 BinSearchTreeIterator< Val, Cmp, Node > iter;
648 iter._initialize(
this, _root,
true);
652 template <
typename Val,
class Cmp,
class Node >
653 INLINE BinSearchTreeIterator< Val, Cmp, Node >
655 BinSearchTreeIterator< Val, Cmp, Node > iter;
656 iter._initialize(
this, _root,
true);
660 template <
typename Val,
class Cmp,
class Node >
666 __updateEraseIterators(node);
674 if (!node->leftChild() && !node->rightChild()) {
676 if (!node->parent()) _root = 0;
683 else if (!node->leftChild()) {
685 if (!node->parent()) {
689 _root = node->rightChild();
691 Node * parent = node->parent(), *child = node->rightChild();
693 parent->eraseLink(dir);
694 node->eraseRightLink();
695 parent->insertChild(*child, dir);
699 else if (!node->rightChild()) {
701 if (!node->parent()) {
705 _root = node->leftChild();
707 Node * parent = node->parent(), *child = node->leftChild();
709 parent->eraseLink(dir);
710 node->eraseLeftLink();
711 parent->insertChild(*child, dir);
716 __eraseWithTwoChildren(node);
723 template <
typename Val,
class Cmp,
class Node >
739 Node* successor = _succNode(node);
741 if (successor == node->rightChild()) {
742 Node* left_child = node->leftChild();
743 node->eraseLeftLink();
744 node->eraseRightLink();
745 successor->insertLeftChild(*left_child);
747 if (!node->parent()) {
754 Node* parent = node->parent();
755 parent->eraseLink(par_dir);
756 parent->insertChild(*successor, par_dir);
759 Node* parent = successor->parent();
760 parent->eraseLeftLink();
762 if (successor->rightChild()) {
763 Node* succ_child = successor->rightChild();
764 successor->eraseRightLink();
765 parent->insertLeftChild(*succ_child);
768 Node *left = node->leftChild(), *right = node->rightChild();
769 node->eraseLeftLink();
770 node->eraseRightLink();
771 successor->insertLeftChild(*left);
772 successor->insertRightChild(*right);
774 if (!node->parent()) {
779 Node* parent = node->parent();
780 parent->eraseLink(par_dir);
781 parent->insertChild(*successor, par_dir);
786 template <
typename Val,
class Cmp,
class Node >
788 Node* n = _getNode(val);
796 template <
typename Val,
class Cmp,
class Node >
801 template <
typename Val,
class Cmp,
class Node >
803 for (iterator* iter = _iterator_list; iter; iter = iter->_next_iter) {
806 if (iter->_node == node) {
808 iter->_next_node = _succNode(node);
809 iter->_prev_node = _prevNode(node);
810 iter->_parent = node->parent();
811 iter->_left_child = node->leftChild();
812 iter->_right_child = node->rightChild();
813 }
else if (!iter->_node) {
814 if (iter->_next_node == node) iter->_next_node = _succNode(node);
816 if (iter->_prev_node == node) iter->_prev_node = _prevNode(node);
818 if (iter->_parent == node) iter->_parent = node->parent();
820 if (iter->_left_child == node) iter->_left_child = node->leftChild();
822 if (iter->_right_child == node) iter->_right_child = node->rightChild();
829 #endif // DOXYGEN_SHOULD_SKIP_THIS virtual ~BinSearchTree()
Class destructor.
BinSearchTree< Val, Cmp, Node > * _tree
The binary search tree pointed to by the iterator.
BinSearchTreeIterator< Val, Cmp, Node > & operator--()
Point the iterator to the preceding value in the binary search tree.
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.
Basic binary search trees.
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.
BinSearchTreeIterator< Val, Cmp, Node > & downRight()
Makes the iterator move to the right child of the node it points to.
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.
bool operator==(const BinSearchTreeIterator< Val, Cmp, Node > &from) const
Checks whether two iterators are pointing toward identical elements.
void clear()
Detach the iterator from its current tree (if any) and reset it.
gum is the global namespace for all aGrUM entities
BinSearchTreeIterator()
Class Constructors and Destructors.
iterator rbegin()
Reverse iterator.
bool operator!=(const BinSearchTreeIterator< Val, Cmp, Node > &from) const
Checks whether two iterators are pointing toward different elements.
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.
BinTreeDir
The direction of a given edge in a binary tree.
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.
BinSearchTreeIterator< Val, Cmp, Node > & operator++()
Point the iterator to the next value in the binary search 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.
BinSearchTreeIterator< Val, Cmp, Node > & up()
Makes the iterator move to its parent node.
Node * _right_child
The right child to be used when _node=0 and rightdown() is applied.
BinSearchTree(bool uniqueness_policy=false)
Basic constructor: returns an empty binary search tree.
Node * _node
The current node pointed to by the iterator.
bool uniquenessPolicy() const
Returns the current uniqueness policy.
virtual Node * _insert(const Val &val)
Creates a copy of the value, insert it in the gum::BinSearchTree and returns the copy value...
void __updateEraseIterators(Node *node)
Update all iterators when a given node is deleted.
void clear()
Removes all the elements from the gum::BinSearchTree.
BinSearchTreeIterator< Val, Cmp, Node > & downLeft()
Makes the iterator move to the left child of the node it points to.
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.
void setUniquenessPolicy(const bool new_policy)
Enables the user to change dynamically the policy for checking whether there can exist several identi...
Node * _prev_node
The preceding node to be used when _node=0 (if a – operator is applied).
const iterator & end()
End iterator.
const Val & operator*() const
Returns the value pointed to by the 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.
friend class BinSearchTree< Val, Cmp, Node >
To speed-up accesses.
BinSearchTreeIterator< Val, Cmp, Node > & operator=(const BinSearchTreeIterator< Val, Cmp, Node > &from)
Class operators.
~BinSearchTreeIterator()
Class destructor.
bool contains(const Val &val) const
Returns true if the gum::BinSearchTree contains the value.
virtual void _erase(Node *node)
Erase the node passed in argument.
void _initialize(const BinSearchTree< Val, Cmp, Node > *tree, const Node *current_node, bool add_to_iterator_list)
A function called to initialize an iterator.
iterator begin()
Begin iterator.
void _detachFromTree()
a method to detach the current iterator from its tree's iterator's list.
#define GUM_ERROR(type, msg)
Node * _minNode(Node *node) const
Returns the smallest node w.r.t.
void __eraseWithTwoChildren(Node *node)
Erase a node with two children.
Node * _prevNode(Node *node) const
Returns the previous node according to weak ordering Cmp.