CppCommon  1.0.4.1
C++ Common Library
bintree_avl.h
Go to the documentation of this file.
1 
9 #ifndef CPPCOMMON_CONTAINERS_BINTREE_AVL_H
10 #define CPPCOMMON_CONTAINERS_BINTREE_AVL_H
11 
12 #include "bintree.h"
13 
14 namespace CppCommon {
15 
17 
98 template <typename T, typename TCompare = std::less<T>>
100 {
101 public:
102  // Standard container type definitions
103  typedef T value_type;
104  typedef TCompare value_compare;
106  typedef const value_type& const_reference;
107  typedef value_type* pointer;
108  typedef const value_type* const_pointer;
109  typedef ptrdiff_t difference_type;
110  typedef size_t size_type;
115 
117  struct Node
118  {
119  T* parent;
120  T* left;
121  T* right;
122  signed char balance;
123 
124  Node() : parent(nullptr), left(nullptr), right(nullptr), balance(0) {}
125  };
126 
127  explicit BinTreeAVL(const TCompare& compare = TCompare()) noexcept
128  : _compare(compare),
129  _size(0),
130  _root(nullptr)
131  {}
132  template <class InputIterator>
133  BinTreeAVL(InputIterator first, InputIterator last, const TCompare& compare = TCompare()) noexcept;
134  BinTreeAVL(const BinTreeAVL&) noexcept = default;
135  BinTreeAVL(BinTreeAVL&&) noexcept = default;
136  ~BinTreeAVL() noexcept = default;
137 
138  BinTreeAVL& operator=(const BinTreeAVL&) noexcept = default;
139  BinTreeAVL& operator=(BinTreeAVL&&) noexcept = default;
140 
142  explicit operator bool() const noexcept { return !empty(); }
143 
145  bool empty() const noexcept { return _root == nullptr; }
146 
148  size_t size() const noexcept { return _size; }
149 
151  T* root() noexcept { return _root; }
152  const T* root() const noexcept { return _root; }
154  T* lowest() noexcept;
155  const T* lowest() const noexcept;
157  T* highest() noexcept;
158  const T* highest() const noexcept;
159 
161  bool compare(const T& item1, const T& item2) const noexcept { return _compare(item1, item2); }
162 
164  iterator begin() noexcept;
165  const_iterator begin() const noexcept;
166  const_iterator cbegin() const noexcept;
168  iterator end() noexcept;
169  const_iterator end() const noexcept;
170  const_iterator cend() const noexcept;
171 
173  reverse_iterator rbegin() noexcept;
174  const_reverse_iterator rbegin() const noexcept;
175  const_reverse_iterator crbegin() const noexcept;
177  reverse_iterator rend() noexcept;
178  const_reverse_iterator rend() const noexcept;
179  const_reverse_iterator crend() const noexcept;
180 
182  iterator find(const T& item) noexcept;
183  const_iterator find(const T& item) const noexcept;
184 
186  iterator lower_bound(const T& item) noexcept;
187  const_iterator lower_bound(const T& item) const noexcept;
189  iterator upper_bound(const T& item) noexcept;
190  const_iterator upper_bound(const T& item) const noexcept;
191 
193 
197  std::pair<iterator, bool> insert(T& item) noexcept;
199 
204  std::pair<iterator, bool> insert(const const_iterator& position, T& item) noexcept;
205 
207 
211  T* erase(const T& item) noexcept;
213 
217  iterator erase(const iterator& it) noexcept;
218 
220  void clear() noexcept;
221 
223  void swap(BinTreeAVL& bintree) noexcept;
224  template <typename U, typename UCompare>
225  friend void swap(BinTreeAVL<U, UCompare>& bintree1, BinTreeAVL<U, UCompare>& bintree2) noexcept;
226 
227 private:
228  TCompare _compare; // Binary tree compare
229  size_t _size; // Binary tree size
230  T* _root; // Binary tree root node
231 
232  const T* InternalLowest() const noexcept;
233  const T* InternalHighest() const noexcept;
234  const T* InternalFind(const T& item) const noexcept;
235  const T* InternalLowerBound(const T& item) const noexcept;
236  const T* InternalUpperBound(const T& item) const noexcept;
237 
238  static void RotateLeft(T* node);
239  static void RotateRight(T* node);
240  static void RotateLeftLeft(T* node);
241  static void RotateRightRight(T* node);
242  static void Unlink(T* node);
243  static void Swap(T*& node1, T*& node2);
244 };
245 
246 } // namespace CppCommon
247 
248 #include "bintree_avl.inl"
249 
250 #endif // CPPCOMMON_CONTAINERS_BINTREE_AVL_H
Intrusive non balanced binary tree container definition.
Intrusive balanced AVL binary tree container.
Definition: bintree_avl.h:100
ptrdiff_t difference_type
Definition: bintree_avl.h:109
T * lowest() noexcept
Get the lowest binary tree item.
Definition: bintree_avl.inl:21
BinTreeConstReverseIterator< BinTreeAVL< T, TCompare >, T > const_reverse_iterator
Definition: bintree_avl.h:114
T * root() noexcept
Get the root binary tree item.
Definition: bintree_avl.h:151
BinTreeIterator< BinTreeAVL< T, TCompare >, T > iterator
Definition: bintree_avl.h:111
T * erase(const T &item) noexcept
Erase the given item from the binary tree.
const T * root() const noexcept
Definition: bintree_avl.h:152
void clear() noexcept
Clear the binary tree.
const_reverse_iterator crbegin() const noexcept
BinTreeConstIterator< BinTreeAVL< T, TCompare >, T > const_iterator
Definition: bintree_avl.h:112
reverse_iterator rend() noexcept
Get the reverse end binary tree iterator.
bool empty() const noexcept
Is the binary tree empty?
Definition: bintree_avl.h:145
iterator lower_bound(const T &item) noexcept
Find the iterator which points to the first item that not less than the given item in the binary tree...
const_reverse_iterator crend() const noexcept
const_iterator cend() const noexcept
Definition: bintree_avl.inl:95
size_t size() const noexcept
Get the binary tree size.
Definition: bintree_avl.h:148
void swap(BinTreeAVL &bintree) noexcept
Swap two instances.
value_type * pointer
Definition: bintree_avl.h:107
reverse_iterator rbegin() noexcept
Get the reverse begin binary tree iterator.
BinTreeReverseIterator< BinTreeAVL< T, TCompare >, T > reverse_iterator
Definition: bintree_avl.h:113
value_type & reference
Definition: bintree_avl.h:105
const value_type * const_pointer
Definition: bintree_avl.h:108
std::pair< iterator, bool > insert(T &item) noexcept
Insert a new item into the binary tree.
iterator end() noexcept
Get the end binary tree iterator.
Definition: bintree_avl.inl:83
T * highest() noexcept
Get the highest binary tree item.
Definition: bintree_avl.inl:43
bool compare(const T &item1, const T &item2) const noexcept
Compare two items: if the first item is less than the second one?
Definition: bintree_avl.h:161
const_iterator cbegin() const noexcept
Definition: bintree_avl.inl:77
BinTreeAVL(const TCompare &compare=TCompare()) noexcept
Definition: bintree_avl.h:127
iterator find(const T &item) noexcept
Find the iterator which points to the first equal item in the binary tree or return end iterator.
const value_type & const_reference
Definition: bintree_avl.h:106
iterator begin() noexcept
Get the begin binary tree iterator.
Definition: bintree_avl.inl:65
iterator upper_bound(const T &item) noexcept
Find the iterator which points to the first item that greater than the given item in the binary tree ...
Intrusive binary tree constant iterator.
Definition: bintree.h:335
Intrusive binary tree constant reverse iterator.
Definition: bintree.h:448
Intrusive binary tree iterator.
Definition: bintree.h:279
Intrusive binary tree reverse iterator.
Definition: bintree.h:392
C++ Common project definitions.
Definition: token_bucket.h:15
AVL binary tree node.
Definition: bintree_avl.h:118
signed char balance
Balance level (-1, 0, 1)
Definition: bintree_avl.h:122
T * left
Pointer to the left child binary tree node.
Definition: bintree_avl.h:120
T * right
Pointer to the right child binary tree node.
Definition: bintree_avl.h:121
T * parent
Pointer to the parent binary tree node.
Definition: bintree_avl.h:119