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};
54 if(m_left && child == m_left.
get())
75 auto ptr = this->
find(value);
77 return ptr && ptr->m_parent ?
78 ptr->m_parent->nearest_left_parent(ptr) :
nullptr;
84 if(m_right && child == m_right.
get())
105 auto ptr = this->
find(value);
107 return ptr && ptr->m_parent ?
108 ptr->m_parent->nearest_right_parent(ptr) :
nullptr;
111 int height(
bool bRecalculate =
false)
const
115 int left_height = 1, right_height = 1;
118 left_height += this->m_left->height(
false);
121 right_height += this->m_right->height(
false);
124 this->m_height = left_height > right_height ?
125 left_height : right_height;
128 return this->m_height;
134 auto parent_ptr = this->m_parent;
138 auto old_height = parent_ptr->m_height;
140 if(old_height != parent_ptr->height(
true))
141 parent_ptr = parent_ptr->m_parent;
149 if(this->m_left && this->m_left.get() == child)
152 else if(this->m_right && this->m_right.get() == child)
161 os <<
"node_" << this->m_value;
169 <<
" [ shape=oval, label = \"V: "
188 os <<
" [style = dashed, color = red] ; " <<
nl;
190 os <<
" [style = dashed, color = blue] ; " <<
nl;
197 this->m_left->get_node_name(os) <<
" [ color = red ] ;" <<
nl;
200 this->m_left->print_node(os);
207 this->m_right->get_node_name(os) <<
" [color = blue ] ; " <<
nl;
210 this->m_right->print_node(os);
217 os <<
"digraph G { " <<
nl;
226 const ElementType&
get()
const {
return this->m_value; }
232 if(value < this->m_value)
234 if(this->m_left) this->m_left->find_raw(value);
236 else if (value > this->m_value)
238 if(this->m_right) this->m_right->find_raw(value);
262 return this->m_left->
minimum();
270 return this->m_right->
maximum();
276 m_value{value}, m_parent{parent} { }
280 template<
typename Type>
285 if(value == this->m_value)
287 else if(value < this->m_value)
291 return this->m_left->insert(std::forward<Type>(value));
296 this->m_left.reset(
new binary_node{std::forward<Type>(value),
this} );
299 this->m_left->update_height();
308 return this->m_right->insert(std::forward<Type>(value));
313 this->m_right.reset(
new binary_node{std::forward<Type>(value),
this});
316 this->m_right->update_height();
326 if(node_ptr->m_value < this->m_value)
330 return this->m_left->graft(node_ptr);
333 node_ptr->m_parent =
this;
334 this->m_left = std::move(node_ptr);
335 this->m_left->update_height();
344 return this->m_right->graft(node_ptr);
347 node_ptr->m_parent =
this;
348 this->m_right = std::move(node_ptr);
349 this->m_right->update_height();
358 template<
typename Type,
typename... Types>
364 bool result = this->
insert(std::forward<Type>(arg));
366 if constexpr(
sizeof...(args) != 0)
369 return result && this->
insert(std::forward<Types>(args)...);
384 os << this->m_value <<
", ";
387 this->m_left->visit_nodes(os, order);
390 this->m_right->visit_nodes(os, order);
397 this->m_left->visit_nodes(os, order);
400 this->m_right->visit_nodes(os, order);
402 os << this->m_value <<
", ";
410 this->m_right->visit_nodes(os, order);
412 os << this->m_value <<
", ";
415 this->m_left->visit_nodes(os, order);
424 this->m_left->visit_nodes(os, order);
426 os << this->m_value <<
", ";
429 this->m_right->visit_nodes(os, order);
445 if(this->m_left && (ptr = this->m_left->
find(value, fmode, vmode)))
448 if( value < this->m_value)
451 if(this->m_right && (ptr = this->m_right->
find(value, fmode, vmode)))
460 if(this->m_right && (ptr = this->m_right->
find(value, fmode, vmode)))
463 if(value > this->m_value)
466 if(this->m_left && (ptr = this->m_left->
find(value, fmode, vmode)))
472 return this->
find(value);
480 return (!this->m_left && !this->m_right);
485 if(this->m_left && (ptr == this->m_left.
get()))
486 return std::move(this->m_left);
487 else if(this->m_right && (ptr == this->m_right.
get()))
488 return std::move(this->m_right);
498 if(!ptr)
return false;
510 = std::move(root_ptr->m_left);
514 = std::move(root_ptr->m_right);
519 right_child->m_parent =
nullptr;
525 root_ptr = std::move(right_child);
529 root_ptr->graft(left_child);
537 left_child->m_parent =
nullptr;
541 root_ptr = std::move(left_child);
572 ptr->m_parent->m_height = 1;
587 ptr->m_parent->
height(
true);
591 std::move(orphan->m_left);
594 std::move(orphan->m_right);
597 root_ptr->graft(right_child);
600 root_ptr->graft(left_child);
609template<
typename ElementType>
612template<
typename ElementType>
616 using node_ptr_t = std::unique_ptr<binary_node_t>;
618 binary_node_t::remove_node(node_ptr, value);
651 root.visit_nodes(os, visit_mode::ascending_order);
657 root.visit_nodes(os, visit_mode::descending_order);
672 root.
insert(8, 15, 7, 9, 12, 17, 6, 16, 18, 25);
674 if(
auto ptr = root.find(6))
676 stream <<
"Value found: " << ptr->get() <<
endl;
683 if(
auto ptr = root.find(20) )
685 stream <<
"Value found: " << ptr->get() <<
endl;
704 root.
insert(8, 15, 7, 9, 12, 17, 6, 16, 18);
714 stream <<
"Usage 1: " << appname <<
" graph node_list " <<
endL;
715 stream <<
"\tExample: " << appname <<
" graph 10 8 15 7 9 12 17 6 16 18 > binary_tree.gv" <<
endL;
716 stream <<
"\tdot -Tpng binary_tree.gv -o binary_tree.png" <<
endL;
718 stream <<
"Usage 2: " << appname <<
" list node_list " <<
endL;
719 stream <<
"\tExample: " << appname <<
" list 10 8 15 7 9 12 17 6 16 18" <<
endL;
721 stream <<
"Usage 3: " << appname <<
" {ascending|desecnding} {successor_of|predecessor_of|exact_match_of} value node_list " <<
endL;
722 stream <<
"\tExample: " << appname <<
" ascending successor_of 19 10 8 15 7 9 12 17 6 16 18" <<
endL;
723 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;
725 stream <<
"Usage 4: " << appname <<
" remove value node_list " <<
endL;
726 stream <<
"\tExample: " << appname <<
" remove 19 10 8 15 7 9 12 17 6 16 18" <<
endL;
727 stream <<
"\t\t" <<
" Find and remove node with value 19 from the list 10 8 15 7 9 12 17 6 16 18" <<
endL;
729 stream <<
"Usage 5: " << appname <<
" find value node_list " <<
endL;
730 stream <<
"\tExample: " << appname <<
" find 19 10 8 15 7 9 12 17 6 16 18" <<
endL;
731 stream <<
"\t\t" <<
" Find 19 from the list 10 8 15 7 9 12 17 6 16 18" <<
endL;
733 stream <<
"Usage 6: " << appname <<
" min_max node_list " <<
endL;
734 stream <<
"\tExample: " << appname <<
" min_max 10 8 15 7 9 12 17 6 16 18" <<
endL;
735 stream <<
"\t\t" <<
" Find minimum and maximum from the list 10 8 15 7 9 12 17 6 16 18" <<
endL;
737 stream <<
"Usage 7: " << appname <<
" nearest_parents value node_list " <<
endL;
738 stream <<
"\tExample: " << appname <<
" nearest_parents 9 10 8 15 7 9 12 17 6 16 18" <<
endL;
739 stream <<
"\t\t" <<
" Find the nearest left/right parents of node 9 from the list 10 8 15 7 9 12 17 6 16 18" <<
endL;
742int main(
int argc,
const char* argv[])
752 print_usage(appname,
"Program commandline syntax");
759 if(command ==
"graph")
763 for(
int i = 3; i < argc; ++i)
765 root.
insert( std::atoi(argv[i]) );
770 else if(command ==
"list")
775 for(
int i = 3; i < argc; ++i)
777 root.
insert( std::atoi(argv[i]) );
780 stream <<
"Ascending order: ";
781 root.visit_nodes(
stream, visit_mode::ascending_order);
784 stream <<
"Descending order: ";
785 root.visit_nodes(
stream, visit_mode::descending_order);
788 else if(command ==
"ascending"
789 || command ==
"descending")
795 find_mode fmode {find_mode::undefined};
797 if(command ==
"ascending")
798 vmode = visit_mode::ascending_order;
800 if(command ==
"descending")
801 vmode = visit_mode::descending_order;
803 if(vmode==visit_mode::undefined)
817 if(operation ==
"successor_of")
818 fmode = find_mode::successor;
820 if(operation ==
"predecessor_of")
821 fmode = find_mode::predecessor;
823 if(operation ==
"exact_match_of")
824 fmode = find_mode::exact_match;
826 if(fmode == find_mode::undefined)
832 int value = std::atoi(argv[3]);
837 for(
int i = 5; i < argc; ++i)
839 root.
insert( std::atoi(argv[i]) );
842 stream <<
"Ascending order: ";
843 root.visit_nodes(
stream, visit_mode::ascending_order);
846 stream <<
"Descending order: ";
847 root.visit_nodes(
stream, visit_mode::descending_order);
850 auto ptr = root.find(value, fmode, vmode);
854 if(fmode == find_mode::successor)
855 stream <<
"The successor of " << value;
856 else if (fmode == find_mode::predecessor)
857 stream <<
"The predecessor of " << value;
858 else if (fmode == find_mode::exact_match)
859 stream <<
"The exact match of " << value;
861 if(vmode == visit_mode::ascending_order)
862 stream <<
" in ascending order is ";
864 if(vmode == visit_mode::descending_order)
865 stream <<
" in descending order is ";
871 if(fmode == find_mode::successor)
872 stream <<
"The successor of " << value;
873 else if (fmode == find_mode::predecessor)
874 stream <<
"The predecessor of " << value;
875 else if (fmode == find_mode::exact_match)
876 stream <<
"The exact match of " << value;
878 if(vmode == visit_mode::ascending_order)
879 stream <<
" in ascending order is NOT found";
881 if(vmode == visit_mode::descending_order)
882 stream <<
" in descending order NOT found ";
887 else if(command ==
"remove")
890 using node_ptr_t = std::unique_ptr<binary_node_t>;
899 int value = std::atoi(argv[2]);
901 node_ptr_t root = std::make_unique<binary_node_t>(std::atoi(argv[3]));
903 for(
int i = 4; i < argc; ++i)
905 root->insert( std::atoi(argv[i]) );
910 else if(command ==
"find")
913 using node_ptr_t = std::unique_ptr<binary_node_t>;
922 int value = std::atoi(argv[2]);
924 node_ptr_t root = std::make_unique<binary_node_t>(std::atoi(argv[3]));
926 for(
int i = 4; i < argc; ++i)
928 root->insert( std::atoi(argv[i]) );
931 binary_node_t* ptr = root->find(value);
935 stream <<
"We found value: " << value <<
endl;
939 stream <<
"We failed to find the value: " << value <<
endl;
943 else if(command ==
"nearest_parents")
946 using node_ptr_t = std::unique_ptr<binary_node_t>;
955 int value = std::atoi(argv[2]);
957 node_ptr_t root = std::make_unique<binary_node_t>(std::atoi(argv[3]));
959 for(
int i = 4; i < argc; ++i)
961 root->insert( std::atoi(argv[i]) );
964 auto left_parent_ptr = root->nearest_left_parent(value);
967 stream <<
"The nearest left parent of the node "
968 << value <<
" is " << left_parent_ptr->get() <<
endl;
972 stream <<
"The nearest left parent of the node "
973 << value <<
" is NOT foound." <<
endl;
976 auto right_parent_ptr = root->nearest_right_parent(value);
979 stream <<
"The nearest right parent of the node "
980 << value <<
" is " << right_parent_ptr->get() <<
endl;
984 stream <<
"The nearest right parent of the node "
985 << value <<
" is NOT foound." <<
endl;
989 else if(command ==
"min_max")
992 using node_ptr_t = std::unique_ptr<binary_node_t>;
1001 node_ptr_t root = std::make_unique<binary_node_t>(std::atoi(argv[2]));
1003 for(
int i = 3; i < argc; ++i)
1005 root->insert( std::atoi(argv[i]) );
1008 binary_node_t* ptr_min = root->minimum();
1012 stream <<
"We found minim value: " << ptr_min->get() <<
endl;
1016 stream <<
"We failed to find minimum value " <<
endl;
1020 binary_node_t* ptr_max = root->maximum();
1024 stream <<
"We found maximum value: " << ptr_max->get() <<
endl;
1028 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
void nearest_left_parent_raw(binary_node *child)
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)
binary_node * nearest_right_parent(ElementType value)
binary_node * nearest_left_parent(ElementType value)
void nearest_right_parent_raw(binary_node *child)
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)
binary_node * nearest_right_parent(binary_node *child)
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)
binary_node * nearest_left_parent(binary_node *child)
int height(bool bRecalculate=false) const
void visit_nodes(tpf::sstream &os, visit_mode order=visit_mode::in_order)
Stream output operators << are implemented.