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