61#include <initializer_list>
65#ifndef DOXYGEN_SHOULD_SKIP_THIS
67 template <
typename Val,
typename Cmp >
69 template <
typename Val,
typename Cmp >
72 template <
typename Val,
typename Cmp >
74 template <
typename Val,
typename Cmp >
78 template <
typename Val >
81 AVLTreeNode* parent{
nullptr};
82 AVLTreeNode* left_child{
nullptr};
83 AVLTreeNode* right_child{
nullptr};
92 enum class Emplace { EMPLACE };
94 explicit AVLTreeNode(
const Val& val) : value(val) { GUM_CONSTRUCTOR(AVLTreeNode); };
96 explicit AVLTreeNode(Val&& val) noexcept : value(std::move(val)) {
97 GUM_CONSTRUCTOR(AVLTreeNode);
100 template <
typename... Args >
101 explicit AVLTreeNode(
const Emplace& emplace, Args&&... args) :
102 value(std::forward< Args >(args)...) {
103 GUM_CONSTRUCTOR(AVLTreeNode);
106 AVLTreeNode(
const AVLTreeNode< Val >& from) :
107 parent(from.parent), left_child(from.left_child), right_child(from.right_child),
108 height(from.height), value(from.value) {
109 GUM_CONS_CPY(AVLTreeNode);
112 AVLTreeNode(AVLTreeNode< Val >&& from) :
113 parent(from.parent), left_child(from.left_child), right_child(from.right_child),
114 height(from.height), value(std::move(from.value)) {
115 GUM_CONS_MOV(AVLTreeNode);
118 ~AVLTreeNode() { GUM_DESTRUCTOR(AVLTreeNode); }
121 bool operator==(
const AVLTreeNode< Val >& from)
const {
return value == from.value; }
125 template <
typename Val >
126 std::ostream&
operator<<(std::ostream& stream,
const AVLTreeNode< Val >& node) {
127 return stream <<
'<' << node.value <<
'>';
131 template <
typename Val >
139 static Size castToSize(
const AVLTreeNode< Val >& key) {
140 return HashFunc< Val >::castToSize(key.value);
146 using HashFunc< Val >::operator();
149 INLINE
Size operator()(
const AVLTreeNode< Val >& key)
const {
150 return HashFunc< Val >::operator()(key.value);
167 template <
typename Val,
typename Cmp = std::less< Val > >
205 explicit AVLTree(std::initializer_list< Val > list);
297 template < typename... Args >
456 template < typename Val, typename
Cmp =
std::less< Val > >
483#ifndef DOXYGEN_SHOULD_SKIP_THIS
593 template < typename Val, typename
Cmp =
std::less< Val > >
620#ifndef DOXYGEN_SHOULD_SKIP_THIS
701 template < typename Val, typename
Cmp =
std::less< Val > >
727 const bool rbegin =
true) noexcept;
729#ifndef DOXYGEN_SHOULD_SKIP_THIS
812 template < typename Val, typename
Cmp =
std::less< Val > >
839#ifndef DOXYGEN_SHOULD_SKIP_THIS
912 template < typename Val, typename
Cmp >
914 return stream << tree.toString();
918#ifndef DOXYGEN_SHOULD_SKIP_THIS
927 extern const AVLTreeIterator< int, std::less< int > > _static_AVLTree_end_;
928 extern const AVLTreeReverseIterator< int, std::less< int > > _static_AVLTree_rend_;
929 extern const AVLTreeIteratorSafe< int, std::less< int > > _static_AVLTree_end_safe_;
930 extern const AVLTreeReverseIteratorSafe< int, std::less< int > > _static_AVLTree_rend_safe_;
932 inline constexpr void*
const _AVLTree_end_ = (
void*
const)&_static_AVLTree_end_;
933 inline constexpr void*
const _AVLTree_rend_ = (
void*
const)&_static_AVLTree_rend_;
934 inline constexpr void*
const _AVLTree_end_safe_ = (
void*
const)&_static_AVLTree_end_safe_;
935 inline constexpr void*
const _AVLTree_rend_safe_ = (
void*
const)&_static_AVLTree_rend_safe_;
AVL binary search tree safe (w.r.t.
AVLTreeIteratorSafe(AVLTree< Val, Cmp > &tree, const bool begin=true)
constructor for begin safe iterators
AVLTreeIteratorSafe(const AVLTreeIteratorSafe< Val, Cmp > &from)
copy constructor
const value_type & const_reference
friend AVLTree< Val, Cmp >
const value_type * const_pointer
AVLTreeIteratorSafe(AVLTreeIteratorSafe< Val, Cmp > &&from)
move constructor
std::bidirectional_iterator_tag iterator_category
~AVLTreeIteratorSafe() noexcept
destructor
AVL binary search tree iterator.
AVLNode * preceding_node_
AVLTreeNode< Val > AVLNode
friend AVLTree< Val, Cmp >
AVLTree< Val, Cmp > * tree_
AVLNode * precedingNode_(AVLNode *node) const noexcept
computes the node to go to when applying operator--
const value_type & const_reference
AVLTreeIterator(AVLTreeIterator< Val, Cmp > &&from) noexcept
move constructor
void unregisterTree_() noexcept
make the iterator point to nothing
~AVLTreeIterator() noexcept
destructor
AVLTreeIterator(const AVLTree< Val, Cmp > &tree, const bool begin=true) noexcept
constructor for begin iterators
void pointToEndRend_() noexcept
const value_type * const_pointer
AVLTreeIterator(const AVLTreeIterator< Val, Cmp > &from) noexcept
copy constructor
AVLNode * nextNode_(AVLNode *node) const noexcept
computes the node to go to when applying operator++
std::bidirectional_iterator_tag iterator_category
AVL binary search tree safe (w.r.t.
const value_type & const_reference
AVLTreeReverseIteratorSafe(AVLTreeReverseIteratorSafe< Val, Cmp > &&from)
move constructor
AVLTreeReverseIteratorSafe(AVLTree< Val, Cmp > &tree, const bool rbegin=true)
constructor for rbegin safe iterators
~AVLTreeReverseIteratorSafe() noexcept
destructor
friend AVLTree< Val, Cmp >
AVLTreeReverseIteratorSafe(const AVLTreeReverseIteratorSafe< Val, Cmp > &from)
copy constructor
std::bidirectional_iterator_tag iterator_category
const value_type * const_pointer
AVL binary search tree reverse iterator.
const value_type * const_pointer
AVLTreeReverseIterator(AVLTreeReverseIterator< Val, Cmp > &&from) noexcept
move constructor
AVLTreeReverseIterator(const AVLTreeReverseIterator< Val, Cmp > &from) noexcept
copy constructor
std::bidirectional_iterator_tag iterator_category
~AVLTreeReverseIterator() noexcept
destructor
AVLTreeReverseIterator(const AVLTree< Val, Cmp > &tree, const bool rbegin=true) noexcept
constructor for rbegin iterators
const value_type & const_reference
friend AVLTree< Val, Cmp >
AVLNode * leftRotation_(AVLNode *node_p)
rotate the subtree rooted at p to the left
AVLTreeIteratorSafe< Val, Cmp > iterator_safe
Types for STL compliance.
AVLTreeReverseIteratorSafe< Val, Cmp > reverse_iterator_safe
Types for STL compliance.
AVLTree(AVLTree< Val, Cmp > &&from) noexcept
Move constructor.
AVLTreeIterator< Val, Cmp > iterator
Types for STL compliance.
AVLTree(const Cmp &compare=Cmp())
Basic constructor.
const value_type & insert_(AVLNode *node)
insert a node into the tree
std::vector< iterator_safe * > safe_iterators_
The list of safe iterators and reverse iterators used by the AVL tree.
void clear()
remove all the elements in the tree
~AVLTree()
Class destructor.
AVLNode * rightRotation_(AVLNode *node_q)
rotate the subtree rooted at q to the right
AVLTree(std::initializer_list< Val > list)
Initializer list constructor.
void erase_(AVLNode *node)
remove a node from the tree and free memory
void removeFromSafeList_(iterator_safe *iter)
unregister a safe iterator
bool contains(const value_type &val) const
Indicates whether the tree contains a given value.
AVLNode * highest_node_
the node containing the highest element
Val value_type
Types for STL compliance.
AVLNode * removeNodeFromTree_(AVLNode *node)
remove a node from the tree and returns the node that was actually removed
const value_type & highestValue() const
returns the max element (w.r.t. Cmp) in the tree
AVLNode * lowestNode_() const noexcept
returns the node containing the lowest element of the tree
AVLTreeNode< Val > AVLNode
Types for STL compliance.
const Val * const_pointer
Types for STL compliance.
bool empty() const noexcept
Indicates whether the tree is empty.
AVLTree< Val, Cmp > & operator=(AVLTree< Val, Cmp > &&from) noexcept
Move operator.
AVLNode * highestNode_() const noexcept
returns the node containing the highest element of the tree
AVLTreeReverseIterator< Val, Cmp > reverse_iterator
Types for STL compliance.
const value_type & emplace(Args &&... args)
emplace a new element into the tree
iterator begin() const
returns a new iterator pointing to the minimal element of the tree
static AVLNode * copySubtree_(const AVLNode *from_node, AVLNode *new_parent)
copies recursively a subtree of the AVL tree
Val & reference
Types for STL compliance.
AVLTree< Val, Cmp > & operator=(const AVLTree< Val, Cmp > &from)
Copy operator.
void erase(const value_type &val)
remove an element from the tree
bool exists(const value_type &val) const
Alias of contains: indicates whether the tree contains a given value.
bool owns_nodes_
indicates whether the tree owns its nodes. If not, it won't delete them
AVLNode * root_node_
the root of the AVL tree
constexpr const iterator_safe & endSafe() const
returns a safe iterator pointing just after the maximal element
Size size() const noexcept
Returns the number of elements in the tree.
iterator_safe beginSafe()
returns a new safe iterator pointing to the minimal element of the tree
void rebalanceTree_(AVLNode *node)
rebalance the tree moving up recursively from a given node
AVLTree(const AVLTree< Val, Cmp > &from)
Copy constructor.
constexpr const reverse_iterator & rend() const
returns an iterator pointing just before the minimal element
std::string toString() const
returns a string with the content of the tree, order from the lowest to the highest element
const value_type & insert(const value_type &val)
adds (by copy) a new element into the tree
static void deleteSubtree_(AVLNode *subtree_root_node)
deletes recursively a subtree of the AVL tree
void insertIntoSafeList_(iterator_safe *iter)
register a new safe iterator
Val * pointer
Types for STL compliance.
reverse_iterator_safe rbeginSafe()
returns a safe iterator pointing to the maximal element of the tree
const value_type & lowestValue() const
returns the min element (w.r.t. Cmp) in the tree
constexpr const reverse_iterator_safe & rendSafe() const
returns a safe iterator pointing just before the minimal element
reverse_iterator rbegin() const
returns a new iterator pointing to the maximal element of the tree
const Val & const_reference
Types for STL compliance.
AVLNode * lowest_node_
the node containing the lowest element
Cmp cmp_
the comparison function
constexpr const iterator & end() const
returns an iterator pointing just after the maximal element
Size nb_elements_
the number of elements in the tree
This class should be useless as only its specializations should be used.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Classes providing basic hash functions for hash tables.
gum is the global namespace for all aGrUM entities
std::ostream & operator<<(std::ostream &stream, const AVLTree< Val, Cmp > &tree)
display the content of a tree
Data types to enable creating static variables at compile time.
bool operator==(const TiXmlString &a, const TiXmlString &b)