CPPX 1.1.2
A Modern C++ Utility Library
Loading...
Searching...
No Matches
binary_tree.tpp
Go to the documentation of this file.
1#include <algorithm>
2
3namespace stl_ext
4{
5
6template <typename T> void BinaryTree<T>::preorder(const Node<T> *node) const
7{
8 if (!node)
9 return;
10 std::stack<const Node<T> *> s;
11 s.push(node);
12 while (!s.empty())
13 {
14 const Node<T> *curr = s.top();
15 s.pop();
16 std::cout << curr->get_data() << " ";
17 if (curr->get_right())
18 s.push(curr->get_right());
19 if (curr->get_left())
20 s.push(curr->get_left());
21 }
22}
23
24template <typename T> void BinaryTree<T>::inorder(const Node<T> *node) const
25{
26 std::stack<const Node<T> *> s;
27 const Node<T> *curr = node;
28 while (curr || !s.empty())
29 {
30 while (curr)
31 {
32 s.push(curr);
33 curr = curr->get_left();
34 }
35 curr = s.top();
36 s.pop();
37 std::cout << curr->get_data() << " ";
38 curr = curr->get_right();
39 }
40}
41
42template <typename T> void BinaryTree<T>::postorder(const Node<T> *node) const
43{
44 if (!node)
45 return;
46 std::stack<const Node<T> *> s;
47 const Node<T> *curr = node;
48 const Node<T> *last = nullptr;
49 while (!s.empty() || curr)
50 {
51 if (curr)
52 {
53 s.push(curr);
54 curr = curr->get_left();
55 }
56 else
57 {
58 const Node<T> *peek = s.top();
59 if (peek->get_right() && last != peek->get_right())
60 curr = peek->get_right();
61 else
62 {
63 std::cout << peek->get_data() << " ";
64 last = peek;
65 s.pop();
66 }
67 }
68 }
69}
70
71template <typename T> void BinaryTree<T>::levelorder(const Node<T> *node) const
72{
73 if (!node)
74 return;
75 std::queue<const Node<T> *> q;
76 q.push(node);
77 while (!q.empty())
78 {
79 const Node<T> *cur = q.front();
80 q.pop();
81 std::cout << cur->get_data() << " ";
82 if (cur->get_left())
83 q.push(cur->get_left());
84 if (cur->get_right())
85 q.push(cur->get_right());
86 }
87}
88
89template <typename T> int BinaryTree<T>::compute_size(const Node<T> *node) const
90{
91 if (!node)
92 return 0;
93 int c = 0;
94 std::queue<const Node<T> *> q;
95 q.push(node);
96 while (!q.empty())
97 {
98 const Node<T> *curr = q.front();
99 q.pop();
100 c++;
101 if (curr->get_left())
102 q.push(curr->get_left());
103 if (curr->get_right())
104 q.push(curr->get_right());
105 }
106 return c;
107}
108
109template <typename T> BinaryTree<T>::BinaryTree(const BinaryTree &other)
110{
111 if (other.p_head)
112 p_head = std::make_unique<Node<T>>(*other.p_head);
113}
114
116{
117 if (this == &other)
118 return *this;
119 p_head = other.p_head ? std::make_unique<Node<T>>(*other.p_head) : nullptr;
120 return *this;
121}
122
123template <typename T> Node<T> *BinaryTree<T>::get_root() const
124{
125 return p_head.get();
126}
127
128template <typename T> void BinaryTree<T>::set_root(std::unique_ptr<Node<T>> root)
129{
130 p_head = std::move(root);
131}
132
133template <typename T> bool BinaryTree<T>::is_empty() const
134{
135 return p_head == nullptr;
136}
137
138template <typename T> int BinaryTree<T>::size() const
139{
140 return compute_size(p_head.get());
141}
142
143template <typename T> void BinaryTree<T>::set_left(Node<T> *p, std::unique_ptr<Node<T>> l)
144{
145 if (p)
146 {
147 p->set_left(std::move(l));
148
149 int h_left = p->get_left() ? p->get_left()->get_height_val() : 0;
150 int h_right = p->get_right() ? p->get_right()->get_height_val() : 0;
151 p->set_height_val(1 + std::max(h_left, h_right));
152 }
153}
154
155template <typename T> void BinaryTree<T>::set_right(Node<T> *p, std::unique_ptr<Node<T>> r)
156{
157 if (p)
158 {
159 p->set_right(std::move(r));
160
161 int h_left = p->get_left() ? p->get_left()->get_height_val() : 0;
162 int h_right = p->get_right() ? p->get_right()->get_height_val() : 0;
163 p->set_height_val(1 + std::max(h_left, h_right));
164 }
165}
166
167template <typename T> void BinaryTree<T>::print_preorder() const
168{
169 preorder(p_head.get());
170 std::cout << std::endl;
171}
172template <typename T> void BinaryTree<T>::print_inorder() const
173{
174 inorder(p_head.get());
175 std::cout << std::endl;
176}
177template <typename T> void BinaryTree<T>::print_postorder() const
178{
179 postorder(p_head.get());
180 std::cout << std::endl;
181}
182template <typename T> void BinaryTree<T>::print_levelorder() const
183{
184 levelorder(p_head.get());
185 std::cout << std::endl;
186}
187
188template <typename T> std::unique_ptr<Node<T>> BinaryTree<T>::make_node(const T &val)
189{
190 return std::make_unique<Node<T>>(val);
191}
192
193template <typename T>
194std::unique_ptr<Node<T>> BinaryTree<T>::make_node(const T &val, std::unique_ptr<Node<T>> l, std::unique_ptr<Node<T>> r)
195{
196 return std::make_unique<Node<T>>(val, std::move(l), std::move(r));
197}
198
199} // namespace stl_ext
static std::unique_ptr< Node< T > > make_node(const T &val)
int compute_size(const Node< T > *node) const
void print_levelorder() const
void postorder(const Node< T > *node) const
void set_left(Node< T > *parent, std::unique_ptr< Node< T > > left_child)
void print_preorder() const
void set_root(std::unique_ptr< Node< T > > root)
Node< T > * get_root() const
void inorder(const Node< T > *node) const
void preorder(const Node< T > *node) const
void print_inorder() const
void set_right(Node< T > *parent, std::unique_ptr< Node< T > > right_child)
bool is_empty() const
void levelorder(const Node< T > *node) const
BinaryTree & operator=(const BinaryTree &other)
void print_postorder() const
int get_height_val() const
Definition node.tpp:25
void set_left(std::unique_ptr< Node< T > > node)
Definition node.tpp:60
const T & get_data() const
Definition node.tpp:45
Node< T > * get_right() const
Definition node.tpp:65
void set_right(std::unique_ptr< Node< T > > node)
Definition node.tpp:70
Node< T > * get_left() const
Definition node.tpp:55
void set_height_val(int h)
Definition node.tpp:30
Definition cppx.h:12