16 this->destroy_subtree(this->p_head);
17 this->p_head =
nullptr;
24 std::queue<const Node<T> *>
q;
37 q.push(
curr->p_right);
48 std::stack<Node<T> *>
s;
57 s.push(
curr->p_right);
58 m_pool.deallocate(
curr);
78 std::stack<StackEntry>
s;
90 child->m_height =
top.src->p_left->m_height;
91 child->m_color =
top.src->p_left->m_color;
97 else if (
top.stage == 1)
100 if (
top.src->p_right)
103 child->m_height =
top.src->p_right->m_height;
104 child->m_color =
top.src->p_right->m_color;
128 destroy_subtree(p_head);
130 m_pool.destroy_all();
131 p_head = clone_subtree(
other.p_head);
138 other.p_head =
nullptr;
145 destroy_subtree(p_head);
146 p_head =
other.p_head;
147 m_pool = std::move(
other.m_pool);
148 other.p_head =
nullptr;
161 std::stack<const Node<T> *>
s;
167 std::cout <<
curr->m_data <<
" ";
169 s.push(
curr->p_right);
171 s.push(
curr->p_left);
177 std::stack<const Node<T> *>
s;
179 while (
curr || !
s.empty())
188 std::cout <<
curr->m_data <<
" ";
197 std::stack<const Node<T> *>
s;
200 while (!
s.empty() ||
curr)
214 std::cout <<
peek->m_data <<
" ";
226 std::queue<const Node<T> *>
q;
232 std::cout <<
cur->m_data <<
" ";
236 q.push(
cur->p_right);
245 std::queue<const Node<T> *>
q;
253 q.push(
curr->p_left);
255 q.push(
curr->p_right);
267 if (p_head &&
root != p_head)
269 destroy_subtree(p_head);
276 return p_head ==
nullptr;
281 return compute_size(p_head);
289 destroy_subtree(
p->p_left);
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));
303 destroy_subtree(
p->p_right);
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));
315 std::cout << std::endl;
320 std::cout << std::endl;
325 std::cout << std::endl;
330 std::cout << std::endl;
335 return m_pool.allocate(
val);
340 return m_pool.allocate(
val,
l,
r);
351 std::cout <<
prefix << (
is_left ?
"\u2514\u2500\u2500 " :
"\u250c\u2500\u2500 ") <<
node->m_data <<
"\n";
360 std::cout <<
"(empty tree)" << std::endl;
364 print_tree_helper(p_head->p_right,
" ",
false);
366 std::cout << p_head->m_data <<
"\n";
368 print_tree_helper(p_head->p_left,
" ",
true);
370 std::cout << std::flush;
380 out <<
" \"" <<
node->m_data <<
"\" -> \"" <<
node->p_left->m_data <<
"\";\n";
392 out <<
" \"" <<
node->m_data <<
"\" -> \"" <<
node->p_right->m_data <<
"\";\n";
408 throw std::runtime_error(
"Could not open file: " +
filename);
411 out <<
"digraph BST {\n";
412 out <<
" node [shape=circle];\n";
429 std::queue<const Node<T> *>
q;
435 if (!
curr->p_left && !
curr->p_right)
440 q.push(
curr->p_left);
442 q.push(
curr->p_right);
int compute_size(const Node< T > *node) const
void print_levelorder() const
virtual int height() 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)