CPPX 3.2.1
A Modern C++ Utility Library
Loading...
Searching...
No Matches
cppx.h
Go to the documentation of this file.
1#ifndef CPPX_H
2#define CPPX_H
3
4#include <cstddef>
5#include <cstdint>
6#include <iostream>
7#include <string>
8#include <vector>
9
10namespace stl_ext
11{
12
13enum class Color : std::uint8_t
14{
15 RED,
16 BLACK
17};
18
19template <typename T> class BST;
20template <typename T> class AVLTree;
21template <typename T> class RBTree;
22template <typename T> class BinaryTree;
23
24template <typename T> class Node
25{
26 friend class BST<T>;
27 friend class AVLTree<T>;
28 friend class RBTree<T>;
29 friend class BinaryTree<T>;
30
31 private:
32 T m_data;
33 Node<T> *p_left = nullptr;
34 Node<T> *p_right = nullptr;
35 Node<T> *p_parent = nullptr;
36 std::int8_t m_height = 1;
37 Color m_color = Color::RED;
38
39 public:
40 explicit Node(T val);
41 Node(T val, Node<T> *left, Node<T> *right);
42
43 int get_height_val() const;
44 void set_height_val(int h);
45
46 const T &get_data() const;
47 void set_data(const T &val);
48
49 Node<T> *get_left() const;
50 void set_left(Node<T> *node);
51
52 Node<T> *get_right() const;
53 void set_right(Node<T> *node);
54
55 Color get_color() const;
56 void set_color(Color c);
57
58 Node<T> *get_parent() const;
59 void set_parent(Node<T> *parent);
60};
61
62template <typename T> class NodePool
63{
64 struct Block
65 {
66 static constexpr std::size_t CAPACITY = 4096;
67 alignas(alignof(T) > alignof(void *) ? alignof(T)
68 : sizeof(void *)) unsigned char data[CAPACITY * sizeof(Node<T>)];
69 std::size_t used = 0;
70 };
71
72 std::vector<Block *> m_blocks;
73 std::vector<void *> m_free_list;
74
75 public:
76 NodePool() = default;
77 ~NodePool();
78
79 NodePool(const NodePool &) = delete;
80 NodePool &operator=(const NodePool &) = delete;
81
82 NodePool(NodePool &&other) noexcept;
83 NodePool &operator=(NodePool &&other) noexcept;
84
85 template <typename... Args> Node<T> *allocate(Args &&...args);
86 void deallocate(Node<T> *node);
87 void destroy_all();
88
89 private:
90 void *get_slot();
91};
92
93template <typename T> class BinaryTree
94{
95 protected:
96 Node<T> *p_head = nullptr;
98
99 void preorder(const Node<T> *node) const;
100 void inorder(const Node<T> *node) const;
101 void postorder(const Node<T> *node) const;
102 void levelorder(const Node<T> *node) const;
103 int compute_size(const Node<T> *node) const;
104 void print_tree_helper(const Node<T> *node, const std::string &prefix, bool is_left) const;
105 void dot_helper(const Node<T> *node, std::ostream &out, int &null_count) const;
106 void destroy_subtree(Node<T> *node);
107 Node<T> *clone_subtree(const Node<T> *node);
108
109 public:
110 BinaryTree() = default;
111
112 BinaryTree(const BinaryTree &other);
113 BinaryTree &operator=(const BinaryTree &other);
114
115 BinaryTree(BinaryTree &&other) noexcept;
116 BinaryTree &operator=(BinaryTree &&other) noexcept;
117
118 virtual ~BinaryTree();
119
120 Node<T> *get_root() const;
121 void set_root(Node<T> *root);
122 bool is_empty() const;
123 int size() const;
124 void print_preorder() const;
125 void print_inorder() const;
126 void print_postorder() const;
127 void print_levelorder() const;
128 void print_tree() const;
129 void dump_to_dot(const std::string &filename) const;
130
131 Node<T> *make_node(const T &val);
132 Node<T> *make_node(const T &val, Node<T> *left, Node<T> *right);
133 void set_left(Node<T> *parent, Node<T> *left_child);
134 void set_right(Node<T> *parent, Node<T> *right_child);
135 void clear();
136 virtual int height() const;
137 int count_leaves() const;
138};
139
140template <typename T> class BST : public BinaryTree<T>
141{
142 private:
143 void insert_iterative(Node<T> *node, const T &val);
144 void remove_impl(const T &val);
145
146 public:
147 using BinaryTree<T>::p_head;
148 using BinaryTree<T>::m_pool;
149 virtual void insert(const T &val);
150 bool contains(const T &val) const;
151 virtual void remove(const T &val);
152 T get_min() const;
153 T get_max() const;
154 T get_successor(const T &val) const;
155 T get_predecessor(const T &val) const;
156};
157
158template <typename T> class AVLTree : public BST<T>
159{
160
161 private:
162 int get_height(const Node<T> *node) const;
163 int get_balance_factor(const Node<T> *node) const;
164 void update_height(Node<T> *node);
165
166 Node<T> *rotate_left(Node<T> *node);
167 Node<T> *rotate_right(Node<T> *node);
168 Node<T> *rebalance(Node<T> *node);
169
170 public:
171 void insert(const T &val) override;
172 void remove(const T &val) override;
173 int height() const override;
174};
175
176template <typename T> class RBTree : public BST<T>
177{
178 private:
179 void rotate_left(Node<T> *node);
180 void rotate_right(Node<T> *node);
181 void fix_insert_violation(Node<T> *node);
182 void fix_delete_violation(Node<T> *node, Node<T> *parent);
183 Node<T> *tree_minimum(Node<T> *node) const;
184 Color node_color(const Node<T> *node) const;
185 int compute_black_height(const Node<T> *node) const;
186 bool validate_rb_helper(const Node<T> *node) const;
187
188 public:
189 using BST<T>::p_head;
190 using BST<T>::m_pool;
191
192 void insert(const T &val) override;
193 void remove(const T &val) override;
194
195 static bool is_red(const Node<T> *node);
196 static bool is_black(const Node<T> *node);
197 static Color get_color(const Node<T> *node);
198 int get_black_height() const;
199 bool validate_rb_properties() const;
200 std::vector<T> to_sorted_vector() const;
201};
202
203} // namespace stl_ext
204
205#include "cppx/detail/avl.tpp"
206#include "cppx/detail/binary_tree.tpp"
207#include "cppx/detail/bst.tpp"
208#include "cppx/detail/node.tpp"
209#include "cppx/detail/node_pool.tpp"
210#include "cppx/detail/rbt.tpp"
211
212#endif
int height() const override
Definition avl.tpp:167
void remove(const T &val) override
Definition avl.tpp:101
void insert(const T &val) override
Definition avl.tpp:71
T get_predecessor(const T &val) const
Definition bst.tpp:172
virtual void insert(const T &val)
Definition bst.tpp:8
T get_max() const
Definition bst.tpp:126
T get_successor(const T &val) const
Definition bst.tpp:136
virtual void remove(const T &val)
Definition bst.tpp:111
T get_min() const
Definition bst.tpp:116
bool contains(const T &val) const
Definition bst.tpp:99
int compute_size(const Node< T > *node) const
void print_levelorder() const
virtual int height() const
int count_leaves() const
void destroy_subtree(Node< T > *node)
Node< T > * clone_subtree(const Node< T > *node)
void set_right(Node< T > *parent, Node< T > *right_child)
void postorder(const Node< T > *node) const
void set_left(Node< T > *parent, Node< T > *left_child)
void print_preorder() const
void print_tree() const
Node< T > * get_root() const
void dot_helper(const Node< T > *node, std::ostream &out, int &null_count) const
void inorder(const Node< T > *node) const
void print_tree_helper(const Node< T > *node, const std::string &prefix, bool is_left) const
Node< T > * p_head
Definition cppx.h:96
void preorder(const Node< T > *node) const
void print_inorder() const
NodePool< T > m_pool
Definition cppx.h:97
bool is_empty() const
void levelorder(const Node< T > *node) const
BinaryTree & operator=(const BinaryTree &other)
void dump_to_dot(const std::string &filename) const
void print_postorder() const
void set_root(Node< T > *root)
Node< T > * make_node(const T &val)
NodePool()=default
NodePool(const NodePool &)=delete
void deallocate(Node< T > *node)
Definition node_pool.tpp:42
NodePool & operator=(const NodePool &)=delete
Node< T > * allocate(Args &&...args)
Definition node_pool.tpp:36
void set_color(Color c)
Definition node.tpp:61
int get_height_val() const
Definition node.tpp:16
const T & get_data() const
Definition node.tpp:26
Node< T > * get_right() const
Definition node.tpp:46
void set_right(Node< T > *node)
Definition node.tpp:51
void set_left(Node< T > *node)
Definition node.tpp:41
Node< T > * get_left() const
Definition node.tpp:36
void set_parent(Node< T > *parent)
Definition node.tpp:71
Node< T > * get_parent() const
Definition node.tpp:66
void set_data(const T &val)
Definition node.tpp:31
Color get_color() const
Definition node.tpp:56
void set_height_val(int h)
Definition node.tpp:21
static bool is_black(const Node< T > *node)
Definition rbt.tpp:15
static bool is_red(const Node< T > *node)
Definition rbt.tpp:10
bool validate_rb_properties() const
Definition rbt.tpp:445
static Color get_color(const Node< T > *node)
Definition rbt.tpp:20
std::vector< T > to_sorted_vector() const
Definition rbt.tpp:465
void remove(const T &val) override
Definition rbt.tpp:187
int get_black_height() const
Definition rbt.tpp:419
void insert(const T &val) override
Definition rbt.tpp:79
Definition cppx.h:11
Color
Definition cppx.h:14