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; }
173 if(value < this->m_value)
175 if(this->m_left) this->m_left->find_raw(value);
177 else if (value > this->m_value)
179 if(this->m_right) this->m_right->find_raw(value);
203 return this->m_left->
minimum();
211 return this->m_right->
maximum();
217 m_value{value}, m_parent{parent} { }
221 template<
typename Type>
226 if(value == this->m_value)
228 else if(value < this->m_value)
232 return this->m_left->insert(std::forward<Type>(value));
237 this->m_left.reset(
new binary_node{std::forward<Type>(value),
this} );
240 this->m_left->update_height();
249 return this->m_right->insert(std::forward<Type>(value));
254 this->m_right.reset(
new binary_node{std::forward<Type>(value),
this});
257 this->m_right->update_height();
267 if(node_ptr->m_value < this->m_value)
271 return this->m_left->graft(node_ptr);
274 node_ptr->m_parent =
this;
275 this->m_left = std::move(node_ptr);
276 this->m_left->update_height();
285 return this->m_right->graft(node_ptr);
288 node_ptr->m_parent =
this;
289 this->m_right = std::move(node_ptr);
290 this->m_right->update_height();
299 template<
typename Type,
typename... Types>
305 bool result = this->
insert(std::forward<Type>(arg));
307 if constexpr(
sizeof...(args) != 0)
310 return result && this->
insert(std::forward<Types>(args)...);
325 os << this->m_value <<
", ";
328 this->m_left->visit_nodes(os, order);
331 this->m_right->visit_nodes(os, order);
338 this->m_left->visit_nodes(os, order);
341 this->m_right->visit_nodes(os, order);
343 os << this->m_value <<
", ";
351 this->m_right->visit_nodes(os, order);
353 os << this->m_value <<
", ";
356 this->m_left->visit_nodes(os, order);
365 this->m_left->visit_nodes(os, order);
367 os << this->m_value <<
", ";
370 this->m_right->visit_nodes(os, order);
386 if(this->m_left && (ptr = this->m_left->
find(value, fmode, vmode)))
389 if( value < this->m_value)
392 if(this->m_right && (ptr = this->m_right->
find(value, fmode, vmode)))
401 if(this->m_right && (ptr = this->m_right->
find(value, fmode, vmode)))
404 if(value > this->m_value)
407 if(this->m_left && (ptr = this->m_left->
find(value, fmode, vmode)))
413 return this->
find(value);
421 return (!this->m_left && !this->m_right);
426 if(this->m_left && (ptr == this->m_left.
get()))
427 return std::move(this->m_left);
428 else if(this->m_right && (ptr == this->m_right.
get()))
429 return std::move(this->m_right);
439 if(!ptr)
return false;
451 = std::move(root_ptr->m_left);
455 = std::move(root_ptr->m_right);
460 right_child->m_parent =
nullptr;
466 root_ptr = std::move(right_child);
470 root_ptr->graft(left_child);
478 left_child->m_parent =
nullptr;
482 root_ptr = std::move(left_child);
513 ptr->m_parent->m_height = 1;
528 ptr->m_parent->
height(
true);
532 std::move(orphan->m_left);
535 std::move(orphan->m_right);
538 root_ptr->graft(right_child);
541 root_ptr->graft(left_child);
550template<
typename ElementType>
553template<
typename ElementType>
557 using node_ptr_t = std::unique_ptr<binary_node_t>;
559 binary_node_t::remove_node(node_ptr, value);
592 root.visit_nodes(os, visit_mode::ascending_order);
598 root.visit_nodes(os, visit_mode::descending_order);
613 root.
insert(8, 15, 7, 9, 12, 17, 6, 16, 18, 25);
615 if(
auto ptr = root.find(6))
617 stream <<
"Value found: " << ptr->get() <<
endl;
624 if(
auto ptr = root.find(20) )
626 stream <<
"Value found: " << ptr->get() <<
endl;
645 root.
insert(8, 15, 7, 9, 12, 17, 6, 16, 18);
655 stream <<
"Usage 1: " << appname <<
" graph node_list " <<
endL;
656 stream <<
"\tExample: " << appname <<
" graph 10 8 15 7 9 12 17 6 16 18 > binary_tree.gv" <<
endL;
657 stream <<
"\tdot -Tpng binary_tree.gv -o binary_tree.png" <<
endL;
659 stream <<
"Usage 2: " << appname <<
" list node_list " <<
endL;
660 stream <<
"\tExample: " << appname <<
" list 10 8 15 7 9 12 17 6 16 18" <<
endL;
662 stream <<
"Usage 3: " << appname <<
" {ascending|desecnding} {successor_of|predecessor_of|exact_match_of} value node_list " <<
endL;
663 stream <<
"\tExample: " << appname <<
" ascending successor_of 19 10 8 15 7 9 12 17 6 16 18" <<
endL;
664 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;
666 stream <<
"Usage 4: " << appname <<
" remove value node_list " <<
endL;
667 stream <<
"\tExample: " << appname <<
" remove 19 10 8 15 7 9 12 17 6 16 18" <<
endL;
668 stream <<
"\t\t" <<
" Find and remove node with value 19 from the list 10 8 15 7 9 12 17 6 16 18" <<
endL;
670 stream <<
"Usage 5: " << appname <<
" find value node_list " <<
endL;
671 stream <<
"\tExample: " << appname <<
" find 19 10 8 15 7 9 12 17 6 16 18" <<
endL;
672 stream <<
"\t\t" <<
" Find 19 from the list 10 8 15 7 9 12 17 6 16 18" <<
endL;
674 stream <<
"Usage 6: " << appname <<
" min_max node_list " <<
endL;
675 stream <<
"\tExample: " << appname <<
" min_max 10 8 15 7 9 12 17 6 16 18" <<
endL;
676 stream <<
"\t\t" <<
" Find minimum and maximum from the list 10 8 15 7 9 12 17 6 16 18" <<
endL;
679int main(
int argc,
const char* argv[])
689 print_usage(appname,
"Program commandline syntax");
696 if(command ==
"graph")
700 for(
int i = 3; i < argc; ++i)
702 root.
insert( std::atoi(argv[i]) );
707 else if(command ==
"list")
712 for(
int i = 3; i < argc; ++i)
714 root.
insert( std::atoi(argv[i]) );
717 stream <<
"Ascending order: ";
718 root.visit_nodes(
stream, visit_mode::ascending_order);
721 stream <<
"Descending order: ";
722 root.visit_nodes(
stream, visit_mode::descending_order);
725 else if(command ==
"ascending"
726 || command ==
"descending")
732 find_mode fmode {find_mode::undefined};
734 if(command ==
"ascending")
735 vmode = visit_mode::ascending_order;
737 if(command ==
"descending")
738 vmode = visit_mode::descending_order;
740 if(vmode==visit_mode::undefined)
754 if(operation ==
"successor_of")
755 fmode = find_mode::successor;
757 if(operation ==
"predecessor_of")
758 fmode = find_mode::predecessor;
760 if(operation ==
"exact_match_of")
761 fmode = find_mode::exact_match;
763 if(fmode == find_mode::undefined)
769 int value = std::atoi(argv[3]);
774 for(
int i = 5; i < argc; ++i)
776 root.
insert( std::atoi(argv[i]) );
779 stream <<
"Ascending order: ";
780 root.visit_nodes(
stream, visit_mode::ascending_order);
783 stream <<
"Descending order: ";
784 root.visit_nodes(
stream, visit_mode::descending_order);
787 auto ptr = root.find(value, fmode, vmode);
791 if(fmode == find_mode::successor)
792 stream <<
"The successor of " << value;
793 else if (fmode == find_mode::predecessor)
794 stream <<
"The predecessor of " << value;
795 else if (fmode == find_mode::exact_match)
796 stream <<
"The exact match of " << value;
798 if(vmode == visit_mode::ascending_order)
799 stream <<
" in ascending order is ";
801 if(vmode == visit_mode::descending_order)
802 stream <<
" in descending order is ";
808 if(fmode == find_mode::successor)
809 stream <<
"The successor of " << value;
810 else if (fmode == find_mode::predecessor)
811 stream <<
"The predecessor of " << value;
812 else if (fmode == find_mode::exact_match)
813 stream <<
"The exact match of " << value;
815 if(vmode == visit_mode::ascending_order)
816 stream <<
" in ascending order is NOT found";
818 if(vmode == visit_mode::descending_order)
819 stream <<
" in descending order NOT found ";
824 else if(command ==
"remove")
827 using node_ptr_t = std::unique_ptr<binary_node_t>;
836 int value = std::atoi(argv[2]);
838 node_ptr_t root = std::make_unique<binary_node_t>(std::atoi(argv[3]));
840 for(
int i = 4; i < argc; ++i)
842 root->insert( std::atoi(argv[i]) );
847 else if(command ==
"find")
850 using node_ptr_t = std::unique_ptr<binary_node_t>;
859 int value = std::atoi(argv[2]);
861 node_ptr_t root = std::make_unique<binary_node_t>(std::atoi(argv[3]));
863 for(
int i = 4; i < argc; ++i)
865 root->insert( std::atoi(argv[i]) );
868 binary_node_t* ptr = root->find(value);
872 stream <<
"We found value: " << value <<
endl;
876 stream <<
"We failed to find the value: " << value <<
endl;
880 else if(command ==
"min_max")
883 using node_ptr_t = std::unique_ptr<binary_node_t>;
892 node_ptr_t root = std::make_unique<binary_node_t>(std::atoi(argv[2]));
894 for(
int i = 3; i < argc; ++i)
896 root->insert( std::atoi(argv[i]) );
899 binary_node_t* ptr_min = root->minimum();
903 stream <<
"We found minim value: " << ptr_min->get() <<
endl;
907 stream <<
"We failed to find minimum value " <<
endl;
911 binary_node_t* ptr_max = root->maximum();
915 stream <<
"We found maximum value: " << ptr_max->get() <<
endl;
919 stream <<
"We failed to find maximum value " <<
endl;
std::enable_if_t< tpf::types::is_same_v< tpf::remove_cv_ref_t< Types >... >, ReturnType > enable_if_all_types_are_the_same_t
typename binary_node< ElementType >::node_ptr_t node_ptr_t
void test_remove_node(node_ptr_t< ElementType > &node_ptr, ElementType value)
void print_usage(string_t 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 graph_t::visit_mode visit_mode
static bool remove_node(node_ptr_t &root_ptr, ElementType value)
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)
void find_raw(ElementType value)
tpf::sstream & get_node_name(tpf::sstream &os)
bool graft(node_ptr_t &node_ptr)
const ElementType & get() const
node_ptr_t release_child(binary_node *ptr)
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.