CppCommon 1.0.5.0
C++ Common Library
Loading...
Searching...
No Matches
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
14namespace CppCommon {
15
17
98template <typename T, typename TCompare = std::less<T>>
100{
101public:
102 // Standard container type definitions
103 typedef T value_type;
104 typedef TCompare value_compare;
108 typedef const value_type* const_pointer;
109 typedef ptrdiff_t difference_type;
110 typedef size_t size_type;
115
117 struct Node
118 {
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
227private:
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.
T * lowest() noexcept
Get the lowest binary tree item.
BinTreeConstReverseIterator< BinTreeAVL< T, TCompare >, T > const_reverse_iterator
BinTreeIterator< BinTreeAVL< T, TCompare >, T > iterator
T * erase(const T &item) noexcept
Erase the given item from the binary tree.
void clear() noexcept
Clear the binary tree.
const_reverse_iterator crbegin() const noexcept
BinTreeConstIterator< BinTreeAVL< T, TCompare >, T > const_iterator
reverse_iterator rend() noexcept
Get the reverse end binary tree iterator.
bool empty() const noexcept
Is the binary tree empty?
T * root() noexcept
Get the root binary tree item.
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
size_t size() const noexcept
Get the binary tree size.
reverse_iterator rbegin() noexcept
Get the reverse begin binary tree iterator.
BinTreeReverseIterator< BinTreeAVL< T, TCompare >, T > reverse_iterator
value_type & reference
const value_type * const_pointer
const T * root() const noexcept
std::pair< iterator, bool > insert(T &item) noexcept
Insert a new item into the binary tree.
friend void swap(BinTreeAVL< U, UCompare > &bintree1, BinTreeAVL< U, UCompare > &bintree2) noexcept
iterator end() noexcept
Get the end binary tree iterator.
T * highest() noexcept
Get the highest binary tree item.
bool compare(const T &item1, const T &item2) const noexcept
Compare two items: if the first item is less than the second one?
const_iterator cbegin() const noexcept
BinTreeAVL(const TCompare &compare=TCompare()) noexcept
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
iterator begin() noexcept
Get the begin binary tree iterator.
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.
AVL binary tree node.
signed char balance
Balance level (-1, 0, 1)
T * left
Pointer to the left child binary tree node.
T * right
Pointer to the right child binary tree node.
T * parent
Pointer to the parent binary tree node.