CPPX 3.2.1
A Modern C++ Utility Library
Loading...
Searching...
No Matches
binary_tree.tpp
Go to the documentation of this file.
1#pragma once
2#include "cppx/cppx.h"
3
4#include <algorithm>
5#include <fstream>
6#include <queue>
7#include <sstream>
8#include <stack>
9#include <stdexcept>
10
11namespace stl_ext
12{
13
14template <typename T> void BinaryTree<T>::clear()
15{
16 this->destroy_subtree(this->p_head);
17 this->p_head = nullptr;
18}
19
20template <typename T> int BinaryTree<T>::height() const
21{
22 if (!p_head)
23 return 0;
24 std::queue<const Node<T> *> q;
25 q.push(p_head);
26 int h = 0;
27 while (!q.empty())
28 {
29 int level_size = static_cast<int>(q.size());
30 for (int i = 0; i < level_size; ++i)
31 {
32 const Node<T> *curr = q.front();
33 q.pop();
34 if (curr->p_left)
35 q.push(curr->p_left);
36 if (curr->p_right)
37 q.push(curr->p_right);
38 }
39 ++h;
40 }
41 return h;
42}
43
44template <typename T> void BinaryTree<T>::destroy_subtree(Node<T> *node)
45{
46 if (!node)
47 return;
48 std::stack<Node<T> *> s;
49 s.push(node);
50 while (!s.empty())
51 {
52 Node<T> *curr = s.top();
53 s.pop();
54 if (curr->p_left)
55 s.push(curr->p_left);
56 if (curr->p_right)
57 s.push(curr->p_right);
58 m_pool.deallocate(curr);
59 }
60}
61
62template <typename T> Node<T> *BinaryTree<T>::clone_subtree(const Node<T> *node)
63{
64 if (!node)
65 return nullptr;
66
67 struct StackEntry
68 {
69 const Node<T> *src;
71 int stage;
72 };
73
74 Node<T> *new_root = m_pool.allocate(node->m_data);
75 new_root->m_height = node->m_height;
76 new_root->m_color = node->m_color;
77
78 std::stack<StackEntry> s;
79 s.push({node, new_root, 0});
80
81 while (!s.empty())
82 {
83 auto &top = s.top();
84 if (top.stage == 0)
85 {
86 top.stage = 1;
87 if (top.src->p_left)
88 {
89 Node<T> *child = m_pool.allocate(top.src->p_left->m_data);
90 child->m_height = top.src->p_left->m_height;
91 child->m_color = top.src->p_left->m_color;
92 child->p_parent = top.dest;
93 top.dest->p_left = child;
94 s.push({top.src->p_left, child, 0});
95 }
96 }
97 else if (top.stage == 1)
98 {
99 top.stage = 2;
100 if (top.src->p_right)
101 {
102 Node<T> *child = m_pool.allocate(top.src->p_right->m_data);
103 child->m_height = top.src->p_right->m_height;
104 child->m_color = top.src->p_right->m_color;
105 child->p_parent = top.dest;
106 top.dest->p_right = child;
107 s.push({top.src->p_right, child, 0});
108 }
109 }
110 else
111 {
112 s.pop();
113 }
114 }
115
116 return new_root;
117}
118
119template <typename T> BinaryTree<T>::BinaryTree(const BinaryTree &other) : p_head(nullptr)
120{
121 p_head = clone_subtree(other.p_head);
122}
123
125{
126 if (this == &other)
127 return *this;
128 destroy_subtree(p_head);
129 p_head = nullptr;
130 m_pool.destroy_all();
131 p_head = clone_subtree(other.p_head);
132 return *this;
133}
134
135template <typename T>
136BinaryTree<T>::BinaryTree(BinaryTree &&other) noexcept : p_head(other.p_head), m_pool(std::move(other.m_pool))
137{
138 other.p_head = nullptr;
139}
140
141template <typename T> BinaryTree<T> &BinaryTree<T>::operator=(BinaryTree &&other) noexcept
142{
143 if (this == &other)
144 return *this;
145 destroy_subtree(p_head);
146 p_head = other.p_head;
147 m_pool = std::move(other.m_pool);
148 other.p_head = nullptr;
149 return *this;
150}
151
152template <typename T> BinaryTree<T>::~BinaryTree()
153{
154 p_head = nullptr;
155}
156
157template <typename T> void BinaryTree<T>::preorder(const Node<T> *node) const
158{
159 if (!node)
160 return;
161 std::stack<const Node<T> *> s;
162 s.push(node);
163 while (!s.empty())
164 {
165 const Node<T> *curr = s.top();
166 s.pop();
167 std::cout << curr->m_data << " ";
168 if (curr->p_right)
169 s.push(curr->p_right);
170 if (curr->p_left)
171 s.push(curr->p_left);
172 }
173}
174
175template <typename T> void BinaryTree<T>::inorder(const Node<T> *node) const
176{
177 std::stack<const Node<T> *> s;
178 const Node<T> *curr = node;
179 while (curr || !s.empty())
180 {
181 while (curr)
182 {
183 s.push(curr);
184 curr = curr->p_left;
185 }
186 curr = s.top();
187 s.pop();
188 std::cout << curr->m_data << " ";
189 curr = curr->p_right;
190 }
191}
192
193template <typename T> void BinaryTree<T>::postorder(const Node<T> *node) const
194{
195 if (!node)
196 return;
197 std::stack<const Node<T> *> s;
198 const Node<T> *curr = node;
199 const Node<T> *last = nullptr;
200 while (!s.empty() || curr)
201 {
202 if (curr)
203 {
204 s.push(curr);
205 curr = curr->p_left;
206 }
207 else
208 {
209 const Node<T> *peek = s.top();
210 if (peek->p_right && last != peek->p_right)
211 curr = peek->p_right;
212 else
213 {
214 std::cout << peek->m_data << " ";
215 last = peek;
216 s.pop();
217 }
218 }
219 }
220}
221
222template <typename T> void BinaryTree<T>::levelorder(const Node<T> *node) const
223{
224 if (!node)
225 return;
226 std::queue<const Node<T> *> q;
227 q.push(node);
228 while (!q.empty())
229 {
230 const Node<T> *cur = q.front();
231 q.pop();
232 std::cout << cur->m_data << " ";
233 if (cur->p_left)
234 q.push(cur->p_left);
235 if (cur->p_right)
236 q.push(cur->p_right);
237 }
238}
239
240template <typename T> int BinaryTree<T>::compute_size(const Node<T> *node) const
241{
242 if (!node)
243 return 0;
244 int c = 0;
245 std::queue<const Node<T> *> q;
246 q.push(node);
247 while (!q.empty())
248 {
249 const Node<T> *curr = q.front();
250 q.pop();
251 c++;
252 if (curr->p_left)
253 q.push(curr->p_left);
254 if (curr->p_right)
255 q.push(curr->p_right);
256 }
257 return c;
258}
259
260template <typename T> Node<T> *BinaryTree<T>::get_root() const
261{
262 return p_head;
263}
264
265template <typename T> void BinaryTree<T>::set_root(Node<T> *root)
266{
267 if (p_head && root != p_head)
268 {
269 destroy_subtree(p_head);
270 }
271 p_head = root;
272}
273
274template <typename T> bool BinaryTree<T>::is_empty() const
275{
276 return p_head == nullptr;
277}
278
279template <typename T> int BinaryTree<T>::size() const
280{
281 return compute_size(p_head);
282}
283
284template <typename T> void BinaryTree<T>::set_left(Node<T> *p, Node<T> *l)
285{
286 if (p)
287 {
288 if (p->p_left)
289 destroy_subtree(p->p_left);
290 p->p_left = l;
291
292 int h_left = p->p_left ? p->p_left->m_height : 0;
293 int h_right = p->p_right ? p->p_right->m_height : 0;
294 p->m_height = static_cast<std::int8_t>(1 + std::max(h_left, h_right));
295 }
296}
297
298template <typename T> void BinaryTree<T>::set_right(Node<T> *p, Node<T> *r)
299{
300 if (p)
301 {
302 if (p->p_right)
303 destroy_subtree(p->p_right);
304 p->p_right = r;
305
306 int h_left = p->p_left ? p->p_left->m_height : 0;
307 int h_right = p->p_right ? p->p_right->m_height : 0;
308 p->m_height = static_cast<std::int8_t>(1 + std::max(h_left, h_right));
309 }
310}
311
312template <typename T> void BinaryTree<T>::print_preorder() const
313{
314 preorder(p_head);
315 std::cout << std::endl;
316}
317template <typename T> void BinaryTree<T>::print_inorder() const
318{
319 inorder(p_head);
320 std::cout << std::endl;
321}
322template <typename T> void BinaryTree<T>::print_postorder() const
323{
324 postorder(p_head);
325 std::cout << std::endl;
326}
327template <typename T> void BinaryTree<T>::print_levelorder() const
328{
329 levelorder(p_head);
330 std::cout << std::endl;
331}
332
333template <typename T> Node<T> *BinaryTree<T>::make_node(const T &val)
334{
335 return m_pool.allocate(val);
336}
337
338template <typename T> Node<T> *BinaryTree<T>::make_node(const T &val, Node<T> *l, Node<T> *r)
339{
340 return m_pool.allocate(val, l, r);
341}
342
343template <typename T>
344void BinaryTree<T>::print_tree_helper(const Node<T> *node, const std::string &prefix, bool is_left) const
345{
346 if (!node)
347 return;
348
349 print_tree_helper(node->p_right, prefix + (is_left ? "\u2502 " : " "), false);
350
351 std::cout << prefix << (is_left ? "\u2514\u2500\u2500 " : "\u250c\u2500\u2500 ") << node->m_data << "\n";
352
353 print_tree_helper(node->p_left, prefix + (is_left ? " " : "\u2502 "), true);
354}
355
356template <typename T> void BinaryTree<T>::print_tree() const
357{
358 if (!p_head)
359 {
360 std::cout << "(empty tree)" << std::endl;
361 return;
362 }
363
364 print_tree_helper(p_head->p_right, " ", false);
365
366 std::cout << p_head->m_data << "\n";
367
368 print_tree_helper(p_head->p_left, " ", true);
369
370 std::cout << std::flush;
371}
372
373template <typename T> void BinaryTree<T>::dot_helper(const Node<T> *node, std::ostream &out, int &null_count) const
374{
375 if (!node)
376 return;
377
378 if (node->p_left)
379 {
380 out << " \"" << node->m_data << "\" -> \"" << node->p_left->m_data << "\";\n";
381 dot_helper(node->p_left, out, null_count);
382 }
383 else
384 {
385 out << " null" << null_count << " [shape=point];\n";
386 out << " \"" << node->m_data << "\" -> null" << null_count << ";\n";
387 ++null_count;
388 }
389
390 if (node->p_right)
391 {
392 out << " \"" << node->m_data << "\" -> \"" << node->p_right->m_data << "\";\n";
393 dot_helper(node->p_right, out, null_count);
394 }
395 else
396 {
397 out << " null" << null_count << " [shape=point];\n";
398 out << " \"" << node->m_data << "\" -> null" << null_count << ";\n";
399 ++null_count;
400 }
401}
402
403template <typename T> void BinaryTree<T>::dump_to_dot(const std::string &filename) const
404{
405 std::ofstream out(filename);
406 if (!out.is_open())
407 {
408 throw std::runtime_error("Could not open file: " + filename);
409 }
410
411 out << "digraph BST {\n";
412 out << " node [shape=circle];\n";
413
414 if (p_head)
415 {
416 int null_count = 0;
417 dot_helper(p_head, out, null_count);
418 }
419
420 out << "}\n";
421 out.close();
422}
423
424template <typename T> int BinaryTree<T>::count_leaves() const
425{
426 if (!p_head)
427 return 0;
428 int count = 0;
429 std::queue<const Node<T> *> q;
430 q.push(p_head);
431 while (!q.empty())
432 {
433 const Node<T> *curr = q.front();
434 q.pop();
435 if (!curr->p_left && !curr->p_right)
436 ++count;
437 else
438 {
439 if (curr->p_left)
440 q.push(curr->p_left);
441 if (curr->p_right)
442 q.push(curr->p_right);
443 }
444 }
445 return count;
446}
447
448} // namespace stl_ext
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
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)
Definition cppx.h:11