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 = \"m_value: "
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();
240 if(node_ptr->m_value < this->m_value)
244 return this->m_left->graft(node_ptr);
247 node_ptr->m_parent =
this;
248 this->m_left = std::move(node_ptr);
249 this->m_left->update_height();
258 return this->m_right->graft(node_ptr);
261 node_ptr->m_parent =
this;
262 this->m_right = std::move(node_ptr);
263 this->m_right->update_height();
272 template<
typename Type,
typename... Types>
278 bool result = this->
insert(std::forward<Type>(arg));
280 if constexpr(
sizeof...(args) != 0)
283 return result && this->
insert(std::forward<Types>(args)...);
298 os << this->m_value <<
", ";
301 this->m_left->visit_nodes(os, order);
304 this->m_right->visit_nodes(os, order);
311 this->m_left->visit_nodes(os, order);
314 this->m_right->visit_nodes(os, order);
316 os << this->m_value <<
", ";
324 this->m_right->visit_nodes(os, order);
326 os << this->m_value <<
", ";
329 this->m_left->visit_nodes(os, order);
338 this->m_left->visit_nodes(os, order);
340 os << this->m_value <<
", ";
343 this->m_right->visit_nodes(os, order);
359 if(this->m_left && (ptr = this->m_left->
find(value, fmode, vmode)))
362 if( value < this->m_value)
365 if(this->m_right && (ptr = this->m_right->
find(value, fmode, vmode)))
374 if(this->m_right && (ptr = this->m_right->
find(value, fmode, vmode)))
377 if(value > this->m_value)
380 if(this->m_left && (ptr = this->m_left->
find(value, fmode, vmode)))
386 return this->
find(value);
394 return (!this->m_left && !this->m_right);
399 if(this->m_left && (ptr == this->m_left.
get()))
400 return std::move(this->m_left);
401 else if(this->m_right && (ptr == this->m_right.
get()))
402 return std::move(this->m_right);
412 if(!ptr)
return false;
424 = std::move(root_ptr->m_left);
428 = std::move(root_ptr->m_right);
433 right_child->m_parent =
nullptr;
439 root_ptr = std::move(right_child);
443 root_ptr->graft(left_child);
451 left_child->m_parent =
nullptr;
455 root_ptr = std::move(left_child);
486 ptr->m_parent->m_height = 1;
501 ptr->m_parent->
height(
true);
505 std::move(orphan->m_left);
508 std::move(orphan->m_right);
511 root_ptr->graft(right_child);
514 root_ptr->graft(left_child);
523template<
typename ElementType>
526template<
typename ElementType>
530 using node_ptr_t = std::unique_ptr<binary_node_t>;
532 binary_node_t::remove_node(node_ptr, value);
565 root.visit_nodes(os, visit_mode::ascending_order);
571 root.visit_nodes(os, visit_mode::descending_order);
586 root.
insert(8, 15, 7, 9, 12, 17, 6, 16, 18, 25);
588 if(
auto ptr = root.find(6))
590 stream <<
"Value found: " << ptr->get() <<
endl;
597 if(
auto ptr = root.find(20) )
599 stream <<
"Value found: " << ptr->get() <<
endl;
618 root.
insert(8, 15, 7, 9, 12, 17, 6, 16, 18);
628 stream <<
"Usage 1: " << appname <<
" graph node_list " <<
endL;
629 stream <<
"\tExample: " << appname <<
" graph 10 8 15 7 9 12 17 6 16 18 > binary_tree.gv" <<
endL;
630 stream <<
"\tdot -Tpng binary_tree.gv -o binary_tree.png" <<
endL;
632 stream <<
"Usage 2: " << appname <<
" list node_list " <<
endL;
633 stream <<
"\tExample: " << appname <<
" list 10 8 15 7 9 12 17 6 16 18" <<
endL;
635 stream <<
"Usage 3: " << appname <<
" {ascending|desecnding} {successor_of|predecessor_of|exact_match_of} value node_list " <<
endL;
636 stream <<
"\tExample: " << appname <<
" ascending successor_of 19 10 8 15 7 9 12 17 6 16 18" <<
endL;
637 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;
639 stream <<
"Usage 4: " << appname <<
" remove value node_list " <<
endL;
640 stream <<
"\tExample: " << appname <<
" remove 19 10 8 15 7 9 12 17 6 16 18" <<
endL;
641 stream <<
"\t\t" <<
" Find and remove node with value 19 from the list 10 8 15 7 9 12 17 6 16 18" <<
endL;
644int main(
int argc,
const char* argv[])
654 print_usage(appname,
"Program commandline syntax");
661 if(command ==
"graph")
665 for(
int i = 3; i < argc; ++i)
667 root.
insert( std::atoi(argv[i]) );
672 else if(command ==
"list")
677 for(
int i = 3; i < argc; ++i)
679 root.
insert( std::atoi(argv[i]) );
682 stream <<
"Ascending order: ";
683 root.visit_nodes(
stream, visit_mode::ascending_order);
686 stream <<
"Descending order: ";
687 root.visit_nodes(
stream, visit_mode::descending_order);
690 else if(command ==
"ascending"
691 || command ==
"descending")
697 find_mode fmode {find_mode::undefined};
699 if(command ==
"ascending")
700 vmode = visit_mode::ascending_order;
702 if(command ==
"descending")
703 vmode = visit_mode::descending_order;
705 if(vmode==visit_mode::undefined)
719 if(operation ==
"successor_of")
720 fmode = find_mode::successor;
722 if(operation ==
"predecessor_of")
723 fmode = find_mode::predecessor;
725 if(operation ==
"exact_match_of")
726 fmode = find_mode::exact_match;
728 if(fmode == find_mode::undefined)
734 int value = std::atoi(argv[3]);
739 for(
int i = 5; i < argc; ++i)
741 root.
insert( std::atoi(argv[i]) );
744 stream <<
"Ascending order: ";
745 root.visit_nodes(
stream, visit_mode::ascending_order);
748 stream <<
"Descending order: ";
749 root.visit_nodes(
stream, visit_mode::descending_order);
752 auto ptr = root.find(value, fmode, vmode);
756 if(fmode == find_mode::successor)
757 stream <<
"The successor of " << value;
758 else if (fmode == find_mode::predecessor)
759 stream <<
"The predecessor of " << value;
760 else if (fmode == find_mode::exact_match)
761 stream <<
"The exact match of " << value;
763 if(vmode == visit_mode::ascending_order)
764 stream <<
" in ascending order is ";
766 if(vmode == visit_mode::descending_order)
767 stream <<
" in descending order is ";
773 if(fmode == find_mode::successor)
774 stream <<
"The successor of " << value;
775 else if (fmode == find_mode::predecessor)
776 stream <<
"The predecessor of " << value;
777 else if (fmode == find_mode::exact_match)
778 stream <<
"The exact match of " << value;
780 if(vmode == visit_mode::ascending_order)
781 stream <<
" in ascending order is NOT found";
783 if(vmode == visit_mode::descending_order)
784 stream <<
" in descending order NOT found ";
789 else if(command ==
"remove")
792 using node_ptr_t = std::unique_ptr<binary_node_t>;
801 int value = std::atoi(argv[2]);
803 node_ptr_t root = std::make_unique<binary_node_t>(std::atoi(argv[3]));
805 for(
int i = 4; i < argc; ++i)
807 root->insert( std::atoi(argv[i]) );
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)
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.