2#include "../include/cppx.h"
11 std::stack<Node<T> *>
s;
20 s.push(
curr->p_right);
21 m_pool.deallocate(
curr);
41 std::stack<StackEntry>
s;
53 child->m_height =
top.src->p_left->m_height;
54 child->m_color =
top.src->p_left->m_color;
60 else if (
top.stage == 1)
66 child->m_height =
top.src->p_right->m_height;
67 child->m_color =
top.src->p_right->m_color;
91 destroy_subtree(p_head);
94 p_head = clone_subtree(
other.p_head);
101 other.p_head =
nullptr;
108 destroy_subtree(p_head);
109 p_head =
other.p_head;
110 m_pool = std::move(
other.m_pool);
111 other.p_head =
nullptr;
124 std::stack<const Node<T> *>
s;
130 std::cout <<
curr->m_data <<
" ";
132 s.push(
curr->p_right);
134 s.push(
curr->p_left);
140 std::stack<const Node<T> *>
s;
142 while (
curr || !
s.empty())
151 std::cout <<
curr->m_data <<
" ";
160 std::stack<const Node<T> *>
s;
163 while (!
s.empty() ||
curr)
177 std::cout <<
peek->m_data <<
" ";
189 std::queue<const Node<T> *>
q;
195 std::cout <<
cur->m_data <<
" ";
199 q.push(
cur->p_right);
208 std::queue<const Node<T> *>
q;
216 q.push(
curr->p_left);
218 q.push(
curr->p_right);
230 if (p_head &&
root != p_head)
232 destroy_subtree(p_head);
239 return p_head ==
nullptr;
244 return compute_size(p_head);
252 destroy_subtree(
p->p_left);
255 int h_left =
p->p_left ?
p->p_left->m_height : 0;
256 int h_right =
p->p_right ?
p->p_right->m_height : 0;
257 p->m_height =
static_cast<std::int8_t
>(1 + std::max(
h_left,
h_right));
266 destroy_subtree(
p->p_right);
269 int h_left =
p->p_left ?
p->p_left->m_height : 0;
270 int h_right =
p->p_right ?
p->p_right->m_height : 0;
271 p->m_height =
static_cast<std::int8_t
>(1 + std::max(
h_left,
h_right));
278 std::cout << std::endl;
283 std::cout << std::endl;
288 std::cout << std::endl;
293 std::cout << std::endl;
298 return m_pool.allocate(
val);
303 return m_pool.allocate(
val,
l,
r);
314 std::cout <<
prefix << (
is_left ?
"\u2514\u2500\u2500 " :
"\u250c\u2500\u2500 ") <<
node->m_data <<
"\n";
323 std::cout <<
"(empty tree)" << std::endl;
327 print_tree_helper(p_head->p_right,
" ",
false);
329 std::cout << p_head->m_data <<
"\n";
331 print_tree_helper(p_head->p_left,
" ",
true);
333 std::cout << std::flush;
343 out <<
" \"" <<
node->m_data <<
"\" -> \"" <<
node->p_left->m_data <<
"\";\n";
355 out <<
" \"" <<
node->m_data <<
"\" -> \"" <<
node->p_right->m_data <<
"\";\n";
371 throw std::runtime_error(
"Could not open file: " +
filename);
374 out <<
"digraph BST {\n";
375 out <<
" node [shape=circle];\n";
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
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
void preorder(const Node< T > *node) const
void print_inorder() 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)