C++ Library Extensions 2022.12.09
To help learn modern C++ programming
049-binary_tree01.cpp
Go to the documentation of this file.
1#include <tpf_output.hpp>
2
4auto endl = tpf::endl; // single carriage return and flush to console output
5auto endL = tpf::endL; // double carriage returns and flush to console output
6
7auto nl = "\n"; // single carriage return without flush
8auto nL = "\n\n"; // double carriage returns without flush
9
10template<typename ElementType>
12{
13 public:
14 using node_ptr_t = std::unique_ptr<binary_node>;
15 enum class visit_mode: int{ pre_order, in_order, ascending_order = in_order, post_order, descending_order };
16
17 private:
18
19 ElementType m_value; // node's value
20
21 binary_node* m_parent; // pointer to point to the parent node
22
23 node_ptr_t m_left; // left child node
24 node_ptr_t m_right; // right child node
25
26 public:
27 binary_node(ElementType value = ElementType{}, binary_node* parent = nullptr):
28 m_value{value}, m_parent{parent} { }
29
30 // if insertion fails,
31 // returns false
32 bool insert(ElementType value)
33 {
34 if(value == this->m_value)
35 return false;
36 else if(value < this->m_value)
37 {
38 if(this->m_left)
39 // this is recursion
40 return this->m_left->insert(value);
41 else
42 {
43 this->m_left.reset( new binary_node{value, this} );
44 return true;
45 }
46 }
47 else // value > this->m_value
48 {
49 if(this->m_right)
50 // this is also recursion
51 return this->m_right->insert(value);
52 else
53 {
54 this->m_right.reset( new binary_node{value, this});
55 return true;
56 }
57 }
58 }
59
60 // for more information about visiting order
61 // Binary Tree (ALL Interview Questions)
62 // https://www.youtube.com/watch?v=VQTF_pRTZek&list=PLeIMaH7i8JDj7DnmO7lll97P1yZjMCpgY&index=2
63 void visit_nodes(std::stringstream& os, visit_mode order = visit_mode::in_order)
64 {
65 switch(order)
66 {
68 {
69 os << this->m_value << nl;
70
71 if(this->m_left)
72 this->m_left->visit_nodes(os, order);
73
74 if(this->m_right)
75 this->m_right->visit_nodes(os, order);
76
77 return;
78 }
80 {
81 if(this->m_left)
82 this->m_left->visit_nodes(os, order);
83
84 if(this->m_right)
85 this->m_right->visit_nodes(os, order);
86
87 os << this->m_value << nl;
88
89 return;
90 }
91
93 {
94 if(this->m_right)
95 this->m_right->visit_nodes(os, order);
96
97 os << this->m_value << nl;
98
99 if(this->m_left)
100 this->m_left->visit_nodes(os, order);
101
102 return;
103 }
104
106 default: // in_order
107 {
108 if(this->m_left)
109 this->m_left->visit_nodes(os, order);
110
111 os << this->m_value << nl;
112
113 if(this->m_right)
114 this->m_right->visit_nodes(os, order);
115
116 return;
117 }
118 }
119 }
120};
121
123{
124 // In courtesy of Yoav Freund's AVL trees
125 // https://youtu.be/YKt1kquKScY?t=402
126
127 binary_node<int> root{50};
128
130
131 root.insert(17);
132 root.insert(72);
133
134 root.insert(12);
135 root.insert(23);
136
137 root.insert(54);
138 root.insert(76);
139
140 root.insert(9);
141 root.insert(14);
142 root.insert(19);
143 root.insert(67);
144
145 std::stringstream os;
146
147 root.visit_nodes(os, visit_mode::pre_order);
148 stream << "Pre-order: " << endL;
149 stream << os.str() << endL;
150
151 os.str("");
152 root.visit_nodes(os, visit_mode::in_order);
153 stream << "In-order: " << endL;
154 stream << os.str() << endL;
155
156 os.str("");
157 root.visit_nodes(os, visit_mode::post_order);
158 stream << "post-order: " << endL;
159 stream << os.str() << endL;
160}
161
163{
164 // In courtesy of Vivekanand Khyade - Algorithm Every Day
165 // Introduction to AVL tree (Why AVL tree is needed?)
166 // https://youtu.be/5Q-__zhQ2Gs?t=10
167
168 binary_node<int> root{10};
169
171
172 root.insert(8);
173 root.insert(15);
174
175 root.insert(7);
176 root.insert(9);
177 root.insert(12);
178 root.insert(17);
179
180 root.insert(6);
181 root.insert(16);
182 root.insert(18);
183
184 std::stringstream os;
185
186 root.visit_nodes(os, visit_mode::ascending_order);
187 stream << "Ascending-order: " << endL;
188 stream << os.str() << endL;
189
190 os.str("");
191 root.visit_nodes(os, visit_mode::descending_order);
192 stream << "Descending-order: " << endL;
193 stream << os.str() << endL;
194}
195
196int main()
197{
198 // test_binary_tree();
200}
auto endL
void test_binary_tree()
tpf::sstream stream
auto nL
auto endl
void test_ascending_descending_order()
auto nl
int main()
typename binary_node< ElementType >::node_ptr_t node_ptr_t
typename graph_t::visit_mode visit_mode
Definition: 060-graph02.cpp:8
void visit_nodes(std::stringstream &os, visit_mode order=visit_mode::in_order)
std::unique_ptr< binary_node > node_ptr_t
bool insert(ElementType value)
binary_node(ElementType value=ElementType{}, binary_node *parent=nullptr)
std::string str() const
Definition: tpf_output.hpp:951
constexpr auto endL
Definition: tpf_output.hpp:974
constexpr auto endl
Definition: tpf_output.hpp:973
Stream output operators << are implemented.