datastructures 0.1.0
Loading...
Searching...
No Matches
_avl.hpp
Go to the documentation of this file.
1
7
8#pragma once
9
10#include "_avl_common.hpp"
11
12#include <utility>
13
14namespace datastructures {
15namespace detail {
16
26template <class TItem, class TAlloc>
48
75template <class TKey, class TItem, class TKeyComp, class TAlloc>
76class avl_tree : public avl_tree_base<TKey, TItem, TKeyComp, TAlloc,
77 avl_node<TItem, TAlloc>> {
78 private:
79 using base =
81
82 public:
83 using node_ptr = typename base::node_ptr;
84 using key_type = typename base::key_type;
85
86 private:
87 node_ptr root = nullptr;
88
92 typename base::size_type node_count = 0;
93
94 // ==== Helper Functions ====
95
102 void clone_tree(const avl_tree &other);
103
114 void rotate(node_ptr x) noexcept;
115
120 inline bool is_leaf(node_ptr node) const noexcept {
121 return node->right == nullptr && node->left == nullptr;
122 }
123
128 inline bool is_right_child(node_ptr node) const noexcept {
129 return node->parent->right == node;
130 }
131
132 public:
133 // ==== Constructors & Assigments ====
134
143 avl_tree(const typename base::comparator_type &comparator,
144 const typename base::allocator_type &allocator);
145
147 avl_tree(const avl_tree &other);
148
150 avl_tree &operator=(const avl_tree &other);
151
153 avl_tree(avl_tree &&other) noexcept;
154
156 avl_tree &operator=(avl_tree &&other) noexcept;
157
163 ~avl_tree() noexcept;
164
165 // ==== Access Operations ====
166
168 [[nodiscard]] node_ptr get_root() const noexcept {
169 return root;
170 }
171
180 [[nodiscard]] node_ptr find(const key_type &key) const noexcept;
181
188 [[nodiscard]] node_ptr successor(node_ptr node) const noexcept;
189
196 [[nodiscard]] node_ptr predecessor(node_ptr node) const noexcept;
197
208 template <bool lower>
209 [[nodiscard]] node_ptr bound(const key_type &key) const noexcept;
210
211 // ==== Update Operations ====
212
220 void clear() noexcept;
221
246 template <class TInsertCallback, class TAssignCallback>
247 std::pair<node_ptr, bool> insert(const key_type &key,
248 TInsertCallback &&insert_callback,
249 TAssignCallback &&assign_callback);
250
269 template <class TInsertCallback>
270 std::pair<node_ptr, bool> insert(const key_type &key,
271 TInsertCallback &&insert_callback) {
272 return insert(key, std::forward<TInsertCallback>(insert_callback),
273 [](typename base::item_type &) {});
274 }
275
283 void erase(node_ptr node) noexcept;
284
285 // ==== Size ====
286
290 [[nodiscard]] typename base::size_type size() const noexcept {
291 return node_count;
292 };
293
297 [[nodiscard]] int get_height() const;
298
299 // ==== Debug Functions ====
300
318 std::string to_dot(bool print_invis) const;
319};
320
321} // namespace detail
322} // namespace datastructures
323
324// ==== Implementation ====
325
326#include "_avl.ipp"
Implementation of the ethereal AVL tree.
Header for functions and classes common to all avl trees.
Base class for avl trees.
Definition _avl_common.hpp:76
avl_node_ptr< node_type, allocator_type > node_ptr
The node pointer type.
Definition _avl_common.hpp:90
typename node_alloc_traits::size_type size_type
The size type.
Definition _avl_common.hpp:100
AVL tree implementation.
Definition _avl.hpp:77
base::size_type size() const noexcept
Returns the current number of nodes.
Definition _avl.hpp:290
typename base::node_ptr node_ptr
Definition _avl.hpp:83
~avl_tree() noexcept
Destroys the tree.
Definition _avl.ipp:72
void clear() noexcept
Erases the whole tree.
Definition _avl.ipp:258
node_ptr successor(node_ptr node) const noexcept
Returns the successor to the node.
Definition _avl.ipp:180
std::pair< node_ptr, bool > insert(const key_type &key, TInsertCallback &&insert_callback, TAssignCallback &&assign_callback)
Inserts an item to the tree.
int get_height() const
Returns the current height of the tree.
Definition _avl.ipp:558
node_ptr get_root() const noexcept
Returns the trees root.
Definition _avl.hpp:168
std::string to_dot(bool print_invis) const
Returns the dot representation of the tree.
Definition _avl.ipp:589
node_ptr find(const key_type &key) const noexcept
Finds a node with the given key, if there isn't any returns a nullptr.
Definition _avl.ipp:158
void erase(node_ptr node) noexcept
Erases the node.
Definition _avl.ipp:387
node_ptr predecessor(node_ptr node) const noexcept
Returns the predecessor to the node.
Definition _avl.ipp:201
avl_tree & operator=(const avl_tree &other)
Copies another tree.
Definition _avl.ipp:37
node_ptr bound(const key_type &key) const noexcept
A 2-in-1 function for both lower and upper bounds accessing.
Definition _avl.ipp:223
typename base::key_type key_type
Definition _avl.hpp:84
int8_t avl_sign_type
The type to use for the trinary sign value for each node.
Definition _avl_common.hpp:50
std::allocator_traits< avl_node_allocator_type< TNode, TAlloc > >::pointer avl_node_ptr
A node pointer type.
Definition _avl_common.hpp:72
Declarations of the persistent map.
Definition _avl.hpp:14
AVL Tree Node.
Definition _avl.hpp:27
avl_sign_type sign
Determines whether the node is leaning to one side or balanced.
Definition _avl.hpp:34
avl_node_ptr< avl_node, TAlloc > left
The left sub-tree.
Definition _avl.hpp:43
avl_node_ptr< avl_node, TAlloc > parent
The parent node.
Definition _avl.hpp:40
TItem item
The key / key, value pair.
Definition _avl.hpp:29
avl_node_ptr< avl_node, TAlloc > right
The right sub-tree.
Definition _avl.hpp:46