24template<
typename ReturnType,
typename... Types>
26 std::enable_if_t<tpf::types::is_same_v<tpf::remove_cv_ref_t<Types>...>, ReturnType>;
28template<
typename ElementType>
33 enum class visit_mode:
int{ undefined = 0, pre_order, in_order,
34 ascending_order = in_order, post_order, descending_order };
36 enum class find_mode:
int { undefined = 0, predecessor = 1, exact_match = 2, successor = 3 };
38 enum class child_status:
int { left_child = -1, no_child = 0, right_child = 1};
43 mutable int m_height{1};
52 int height(
bool bRecalculate =
false)
const
56 int left_height = 1, right_height = 1;
59 left_height += this->m_left->height(
false);
62 right_height += this->m_right->height(
false);
65 this->m_height = left_height > right_height ?
66 left_height : right_height;
69 return this->m_height;
75 auto parent_ptr = this->m_parent;
79 auto old_height = parent_ptr->m_height;
81 if(old_height != parent_ptr->height(
true))
82 parent_ptr = parent_ptr->m_parent;
90 if(this->m_left && this->m_left.get() == child)
93 else if(this->m_right && this->m_right.get() == child)
102 os <<
"node_" << this->m_value;
110 <<
" [ shape=oval, label = \"V:"
112 <<
" H:"<< this->m_height <<
"\"] ;";
129 os <<
" [style = dashed, color = red] ; " <<
nl;
131 os <<
" [style = dashed, color = blue] ; " <<
nl;
138 this->m_left->get_node_name(os) <<
" [ color = red ] ;" <<
nl;
141 this->m_left->print_node(os);
148 this->m_right->get_node_name(os) <<
" [color = blue ] ; " <<
nl;
151 this->m_right->print_node(os);
158 os <<
"digraph G { " <<
nl;
167 const ElementType&
get()
const {
return this->m_value; }
174 if(value == this->m_value)
180 if(this->m_left && (ptr = this->m_left->
find(value)))
183 if(this->m_right && (ptr = this->m_right->
find(value)))
190 m_value{value}, m_parent{parent} { }
194 template<
typename Type>
199 if(value == this->m_value)
201 else if(value < this->m_value)
205 return this->m_left->insert(std::forward<Type>(value));
210 this->m_left.reset(
new binary_node{std::forward<Type>(value),
this} );
213 this->m_left->update_height();
222 return this->m_right->insert(std::forward<Type>(value));
227 this->m_right.reset(
new binary_node{std::forward<Type>(value),
this});
230 this->m_right->update_height();
239 template<
typename Type,
typename... Types>
245 bool result = this->
insert(std::forward<Type>(arg));
247 if constexpr(
sizeof...(args) != 0)
250 return result && this->
insert(std::forward<Types>(args)...);
265 os << this->m_value <<
", ";
268 this->m_left->visit_nodes(os, order);
271 this->m_right->visit_nodes(os, order);
278 this->m_left->visit_nodes(os, order);
281 this->m_right->visit_nodes(os, order);
283 os << this->m_value <<
", ";
291 this->m_right->visit_nodes(os, order);
293 os << this->m_value <<
", ";
296 this->m_left->visit_nodes(os, order);
305 this->m_left->visit_nodes(os, order);
307 os << this->m_value <<
", ";
310 this->m_right->visit_nodes(os, order);
326 if(this->m_left && (ptr = this->m_left->
find(value, fmode, vmode)))
329 if( value < this->m_value)
332 if(this->m_right && (ptr = this->m_right->
find(value, fmode, vmode)))
341 if(this->m_right && (ptr = this->m_right->
find(value, fmode, vmode)))
344 if(value > this->m_value)
347 if(this->m_left && (ptr = this->m_left->
find(value, fmode, vmode)))
353 return this->
find(value);
383 root.visit_nodes(os, visit_mode::ascending_order);
389 root.visit_nodes(os, visit_mode::descending_order);
404 root.
insert(8, 15, 7, 9, 12, 17, 6, 16, 18, 25);
406 if(
auto ptr = root.find(6))
408 stream <<
"Value found: " << ptr->get() <<
endl;
415 if(
auto ptr = root.find(20) )
417 stream <<
"Value found: " << ptr->get() <<
endl;
436 root.
insert(8, 15, 7, 9, 12, 17, 6, 16, 18);
446 stream <<
"Usage 1: " << appname <<
" graph node_list " <<
endL;
447 stream <<
"\tExample: " << appname <<
" graph 10 8 15 7 9 12 17 6 16 18 > binary_tree.gv" <<
endL;
448 stream <<
"\tdot -Tpng binary_tree.gv -o binary_tree.png" <<
endL;
450 stream <<
"Usage 2: " << appname <<
" list node_list " <<
endL;
451 stream <<
"\tExample: " << appname <<
" list 10 8 15 7 9 12 17 6 16 18" <<
endL;
453 stream <<
"Usage 3: " << appname <<
" {ascending|desecnding} {successor_of|predecessor_of|exact_match_of} value node_list " <<
endL;
454 stream <<
"\tExample: " << appname <<
" ascending successor_of 19 10 8 15 7 9 12 17 6 16 18" <<
endL;
455 stream <<
"\t\t" <<
" Finds the successor of 19 in ascending order from the list 10 8 15 7 9 12 17 6 16 18" <<
endL;
458int main(
int argc,
const char* argv[])
466 print_usage(argv[0],
"Program commandline syntax");
475 for(
int i = 3; i < argc; ++i)
477 root.
insert( std::atoi(argv[i]) );
482 else if(
string_t(argv[1]) ==
"list")
487 for(
int i = 3; i < argc; ++i)
489 root.
insert( std::atoi(argv[i]) );
492 stream <<
"Ascending order: ";
493 root.visit_nodes(
stream, visit_mode::ascending_order);
496 stream <<
"Descending order: ";
497 root.visit_nodes(
stream, visit_mode::descending_order);
500 else if(
string_t(argv[1]) ==
"ascending"
501 ||
string_t(argv[1]) ==
"descending")
507 find_mode fmode {find_mode::undefined};
509 if(
string_t(argv[1]) ==
"ascending")
510 vmode = visit_mode::ascending_order;
512 if(
string_t(argv[1]) ==
"descending")
513 vmode = visit_mode::descending_order;
515 if(vmode==visit_mode::undefined)
521 if(
string_t(argv[2]) ==
"successor_of")
522 fmode = find_mode::successor;
524 if(
string_t(argv[2]) ==
"predecessor_of")
525 fmode = find_mode::predecessor;
527 if(
string_t(argv[2]) ==
"exact_match_of")
528 fmode = find_mode::exact_match;
530 if(fmode == find_mode::undefined)
536 int value = std::atoi(argv[3]);
541 for(
int i = 5; i < argc; ++i)
543 root.
insert( std::atoi(argv[i]) );
546 stream <<
"Ascending order: ";
547 root.visit_nodes(
stream, visit_mode::ascending_order);
550 stream <<
"Descending order: ";
551 root.visit_nodes(
stream, visit_mode::descending_order);
554 if(fmode == find_mode::undefined || vmode == visit_mode::undefined)
560 auto ptr = root.find(value, fmode, vmode);
564 if(fmode == find_mode::successor)
565 stream <<
"The successor of " << value;
566 else if (fmode == find_mode::predecessor)
567 stream <<
"The predecessor of " << value;
568 else if (fmode == find_mode::exact_match)
569 stream <<
"The exact match of " << value;
571 if(vmode == visit_mode::ascending_order)
572 stream <<
" in ascending order is ";
574 if(vmode == visit_mode::descending_order)
575 stream <<
" in descending order is ";
581 if(fmode == find_mode::successor)
582 stream <<
"The successor of " << value;
583 else if (fmode == find_mode::predecessor)
584 stream <<
"The predecessor of " << value;
585 else if (fmode == find_mode::exact_match)
586 stream <<
"The exact match of " << value;
588 if(vmode == visit_mode::ascending_order)
589 stream <<
" in ascending order is NOT found";
591 if(vmode == visit_mode::descending_order)
592 stream <<
" in descending order NOT found ";
std::enable_if_t< tpf::types::is_same_v< tpf::remove_cv_ref_t< Types >... >, ReturnType > enable_if_all_types_are_the_same_t
void print_usage(const char *appname, const char *msg)
void test_ascending_descending_order()
void test_find_binary_tree()
int main(int argc, const char *argv[])
void test_build_digraph()
typename binary_node< ElementType >::node_ptr_t node_ptr_t
typename graph_t::visit_mode visit_mode
binary_node * find(ElementType value)
enable_if_all_types_are_the_same_t< bool, ElementType, Type, Types... > insert(Type &&arg, Types &&... args)
child_status get_child_status(binary_node *child)
tpf::sstream & get_node_name(tpf::sstream &os)
const ElementType & get() const
void print_node(tpf::sstream &os)
std::unique_ptr< binary_node > node_ptr_t
tpf::sstream & get_node_definition(tpf::sstream &os)
bool insert(ElementType value)
enable_if_all_types_are_the_same_t< bool, ElementType, Type > insert(Type &&value)
binary_node * find(ElementType value, find_mode fmode, visit_mode vmode=visit_mode::ascending_order)
binary_node(ElementType value=ElementType{}, binary_node *parent=nullptr)
int height(bool bRecalculate=false) const
void visit_nodes(tpf::sstream &os, visit_mode order=visit_mode::in_order)
Stream output operators << are implemented.