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; }
195 if(value < this->m_value)
198 return this->m_left->
find(value);
202 else if (value > this->m_value)
205 return this->m_right->
find(value);
216 m_value{value}, m_parent{parent} { }
220 template<
typename Type>
225 if(value == this->m_value)
227 else if(value < this->m_value)
231 return this->m_left->insert(std::forward<Type>(value));
236 this->m_left.reset(
new binary_node{std::forward<Type>(value),
this} );
239 this->m_left->update_height();
248 return this->m_right->insert(std::forward<Type>(value));
253 this->m_right.reset(
new binary_node{std::forward<Type>(value),
this});
256 this->m_right->update_height();
266 if(node_ptr->m_value < this->m_value)
270 return this->m_left->graft(node_ptr);
273 node_ptr->m_parent =
this;
274 this->m_left = std::move(node_ptr);
275 this->m_left->update_height();
284 return this->m_right->graft(node_ptr);
287 node_ptr->m_parent =
this;
288 this->m_right = std::move(node_ptr);
289 this->m_right->update_height();
298 template<
typename Type,
typename... Types>
304 bool result = this->
insert(std::forward<Type>(arg));
306 if constexpr(
sizeof...(args) != 0)
309 return result && this->
insert(std::forward<Types>(args)...);
324 os << this->m_value <<
", ";
327 this->m_left->visit_nodes(os, order);
330 this->m_right->visit_nodes(os, order);
337 this->m_left->visit_nodes(os, order);
340 this->m_right->visit_nodes(os, order);
342 os << this->m_value <<
", ";
350 this->m_right->visit_nodes(os, order);
352 os << this->m_value <<
", ";
355 this->m_left->visit_nodes(os, order);
364 this->m_left->visit_nodes(os, order);
366 os << this->m_value <<
", ";
369 this->m_right->visit_nodes(os, order);
385 if(this->m_left && (ptr = this->m_left->
find(value, fmode, vmode)))
388 if( value < this->m_value)
391 if(this->m_right && (ptr = this->m_right->
find(value, fmode, vmode)))
400 if(this->m_right && (ptr = this->m_right->
find(value, fmode, vmode)))
403 if(value > this->m_value)
406 if(this->m_left && (ptr = this->m_left->
find(value, fmode, vmode)))
412 return this->
find(value);
420 return (!this->m_left && !this->m_right);
425 if(this->m_left && (ptr == this->m_left.
get()))
426 return std::move(this->m_left);
427 else if(this->m_right && (ptr == this->m_right.
get()))
428 return std::move(this->m_right);
438 if(!ptr)
return false;
450 = std::move(root_ptr->m_left);
454 = std::move(root_ptr->m_right);
459 right_child->m_parent =
nullptr;
465 root_ptr = std::move(right_child);
469 root_ptr->graft(left_child);
477 left_child->m_parent =
nullptr;
481 root_ptr = std::move(left_child);
512 ptr->m_parent->m_height = 1;
527 ptr->m_parent->
height(
true);
531 std::move(orphan->m_left);
534 std::move(orphan->m_right);
537 root_ptr->graft(right_child);
540 root_ptr->graft(left_child);
549template<
typename ElementType>
552template<
typename ElementType>
556 using node_ptr_t = std::unique_ptr<binary_node_t>;
558 binary_node_t::remove_node(node_ptr, value);
591 root.visit_nodes(os, visit_mode::ascending_order);
597 root.visit_nodes(os, visit_mode::descending_order);
612 root.
insert(8, 15, 7, 9, 12, 17, 6, 16, 18, 25);
614 if(
auto ptr = root.find(6))
616 stream <<
"Value found: " << ptr->get() <<
endl;
623 if(
auto ptr = root.find(20) )
625 stream <<
"Value found: " << ptr->get() <<
endl;
644 root.
insert(8, 15, 7, 9, 12, 17, 6, 16, 18);
654 stream <<
"Usage 1: " << appname <<
" graph node_list " <<
endL;
655 stream <<
"\tExample: " << appname <<
" graph 10 8 15 7 9 12 17 6 16 18 > binary_tree.gv" <<
endL;
656 stream <<
"\tdot -Tpng binary_tree.gv -o binary_tree.png" <<
endL;
658 stream <<
"Usage 2: " << appname <<
" list node_list " <<
endL;
659 stream <<
"\tExample: " << appname <<
" list 10 8 15 7 9 12 17 6 16 18" <<
endL;
661 stream <<
"Usage 3: " << appname <<
" {ascending|desecnding} {successor_of|predecessor_of|exact_match_of} value node_list " <<
endL;
662 stream <<
"\tExample: " << appname <<
" ascending successor_of 19 10 8 15 7 9 12 17 6 16 18" <<
endL;
663 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;
665 stream <<
"Usage 4: " << appname <<
" remove value node_list " <<
endL;
666 stream <<
"\tExample: " << appname <<
" remove 19 10 8 15 7 9 12 17 6 16 18" <<
endL;
667 stream <<
"\t\t" <<
" Find and remove node with value 19 from the list 10 8 15 7 9 12 17 6 16 18" <<
endL;
669 stream <<
"Usage 5: " << appname <<
" find value node_list " <<
endL;
670 stream <<
"\tExample: " << appname <<
" find 19 10 8 15 7 9 12 17 6 16 18" <<
endL;
671 stream <<
"\t\t" <<
" Find 19 from the list 19 10 8 15 7 9 12 17 6 16 18" <<
endL;
674int main(
int argc,
const char* argv[])
684 print_usage(appname,
"Program commandline syntax");
691 if(command ==
"graph")
695 for(
int i = 3; i < argc; ++i)
697 root.
insert( std::atoi(argv[i]) );
702 else if(command ==
"list")
707 for(
int i = 3; i < argc; ++i)
709 root.
insert( std::atoi(argv[i]) );
712 stream <<
"Ascending order: ";
713 root.visit_nodes(
stream, visit_mode::ascending_order);
716 stream <<
"Descending order: ";
717 root.visit_nodes(
stream, visit_mode::descending_order);
720 else if(command ==
"ascending"
721 || command ==
"descending")
727 find_mode fmode {find_mode::undefined};
729 if(command ==
"ascending")
730 vmode = visit_mode::ascending_order;
732 if(command ==
"descending")
733 vmode = visit_mode::descending_order;
735 if(vmode==visit_mode::undefined)
749 if(operation ==
"successor_of")
750 fmode = find_mode::successor;
752 if(operation ==
"predecessor_of")
753 fmode = find_mode::predecessor;
755 if(operation ==
"exact_match_of")
756 fmode = find_mode::exact_match;
758 if(fmode == find_mode::undefined)
764 int value = std::atoi(argv[3]);
769 for(
int i = 5; i < argc; ++i)
771 root.
insert( std::atoi(argv[i]) );
774 stream <<
"Ascending order: ";
775 root.visit_nodes(
stream, visit_mode::ascending_order);
778 stream <<
"Descending order: ";
779 root.visit_nodes(
stream, visit_mode::descending_order);
782 auto ptr = root.find(value, fmode, vmode);
786 if(fmode == find_mode::successor)
787 stream <<
"The successor of " << value;
788 else if (fmode == find_mode::predecessor)
789 stream <<
"The predecessor of " << value;
790 else if (fmode == find_mode::exact_match)
791 stream <<
"The exact match of " << value;
793 if(vmode == visit_mode::ascending_order)
794 stream <<
" in ascending order is ";
796 if(vmode == visit_mode::descending_order)
797 stream <<
" in descending order is ";
803 if(fmode == find_mode::successor)
804 stream <<
"The successor of " << value;
805 else if (fmode == find_mode::predecessor)
806 stream <<
"The predecessor of " << value;
807 else if (fmode == find_mode::exact_match)
808 stream <<
"The exact match of " << value;
810 if(vmode == visit_mode::ascending_order)
811 stream <<
" in ascending order is NOT found";
813 if(vmode == visit_mode::descending_order)
814 stream <<
" in descending order NOT found ";
819 else if(command ==
"remove")
822 using node_ptr_t = std::unique_ptr<binary_node_t>;
831 int value = std::atoi(argv[2]);
833 node_ptr_t root = std::make_unique<binary_node_t>(std::atoi(argv[3]));
835 for(
int i = 4; i < argc; ++i)
837 root->insert( std::atoi(argv[i]) );
842 else if(command ==
"find")
845 using node_ptr_t = std::unique_ptr<binary_node_t>;
854 int value = std::atoi(argv[2]);
856 node_ptr_t root = std::make_unique<binary_node_t>(std::atoi(argv[3]));
858 for(
int i = 4; i < argc; ++i)
860 root->insert( std::atoi(argv[i]) );
863 binary_node_t* ptr = root->find(value);
867 stream <<
"We found value: " << value <<
endl;
871 stream <<
"We failed to find the value: " << 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)
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.