C++ Library Extensions 2022.12.09
To help learn modern C++ programming
057-binary_tree09.cpp
Go to the documentation of this file.
1#include <tpf_output.hpp>
2
3/*
4 binary_tree 50 17 72 12 23 54 76 9 14 19 67 > old.txt
5
6 binary_tree 50 17 72 12 23 54 76 9 14 19 67 25 > new.txt
7
8 binary_tree 50 17 72 12 23 54 76 9 14 19 67 25 18> newnew.txt
9
10 https://edotor.net/
11
12 */
13
15
16auto endl = tpf::endl; // single carriage return and flush to console output
17auto endL = tpf::endL; // double carriage returns and flush to console output
18
19auto nl = tpf::nl; // single carriage return without flush
20auto nL = tpf::nL; // double carriage returns without flush
21
22using string_t = std::string;
23
24template<typename ReturnType, typename... Types>
26 std::enable_if_t<tpf::types::is_same_v<tpf::remove_cv_ref_t<Types>...>, ReturnType>;
27
28template<typename ElementType>
29class binary_node
30{
31 public:
32 using node_ptr_t = std::unique_ptr<binary_node>;
33 enum class visit_mode: int{ undefined = 0, pre_order, in_order,
34 ascending_order = in_order, post_order, descending_order };
35
36 enum class find_mode: int { undefined = 0, predecessor = 1, exact_match = 2, successor = 3 };
37
38 enum class child_status: int { left_child = -1, no_child = 0, right_child = 1};
39 private:
40
41 ElementType m_value; // node's value
42
43 mutable int m_height{1}; // the height of a node
44
45 binary_node* m_parent; // pointer to point to the parent node
46
47 node_ptr_t m_left; // left child node
48 node_ptr_t m_right; // right child node
49
50 public:
51
53 {
54 if(m_left && child == m_left.get())
55 throw this;
56 else if(m_parent)
57 m_parent->nearest_left_parent_raw(this);
58 }
59
61 {
62 try
63 {
64 this->nearest_left_parent_raw(child);
65 return nullptr; // we failed to find the nearest left parent
66 }
67 catch(binary_node* ptr)
68 {
69 return ptr; // we found the nearest left parent
70 }
71 }
72
73 binary_node* nearest_left_parent(ElementType value)
74 {
75 auto ptr = this->find(value);
76
77 return ptr && ptr->m_parent ?
78 ptr->m_parent->nearest_left_parent(ptr) : nullptr;
79 }
80
83 {
84 if(m_right && child == m_right.get())
85 throw this;
86 else if(m_parent)
87 m_parent->nearest_right_parent_raw(this);
88 }
89
91 {
92 try
93 {
94 this->nearest_right_parent_raw(child);
95 return nullptr; // we failed to find the nearest left parent
96 }
97 catch(binary_node* ptr)
98 {
99 return ptr; // we found the nearest left parent
100 }
101 }
102
104 {
105 auto ptr = this->find(value);
106
107 return ptr && ptr->m_parent ?
108 ptr->m_parent->nearest_right_parent(ptr) : nullptr;
109 }
110
111 int height(bool bRecalculate = false) const
112 {
113 if(bRecalculate) // if bRecalculate is true,
114 { // we will recalculate the m_height member
115 int left_height = 1, right_height = 1;
116
117 if(this->m_left)
118 left_height += this->m_left->height(false); // false means we use cached value of m_height
119
120 if(this->m_right)
121 right_height += this->m_right->height(false); // false means we use cached value of m_height
122
123 // m_height is the greater of the two, left_height and right_height
124 this->m_height = left_height > right_height ?
125 left_height : right_height;
126 }
127
128 return this->m_height;
129 }
130
132 {
133 // the parent ptr of the current node
134 auto parent_ptr = this->m_parent;
135
136 while(parent_ptr)
137 {
138 auto old_height = parent_ptr->m_height;
139
140 if(old_height != parent_ptr->height(true))
141 parent_ptr = parent_ptr->m_parent;
142 else
143 break;
144 }
145 }
146
148 {
149 if(this->m_left && this->m_left.get() == child)
150
152 else if(this->m_right && this->m_right.get() == child)
153
155 else
157 }
158
160 {
161 os << "node_" << this->m_value;
162 return os;
163 }
164
166 {
167 // node_1 [ shape=box, label = " Root "] ;
168 get_node_name(os)
169 << " [ shape=oval, label = \"V: "
170 << this->m_value
171 /* << " H:"<< this->m_height */ << " \"] ;";
172
173 return os;
174 }
175
177 {
178 get_node_definition(os) << nl;
179
180 if(this->m_parent)
181 {
182 get_node_name(os) << " -> ";
183 this->m_parent->get_node_name(os);
184
185 auto status = this->m_parent->get_child_status(this);
186
187 if(status == child_status::left_child)
188 os << " [style = dashed, color = red] ; " << nl;
189 else if (status == child_status::right_child)
190 os << " [style = dashed, color = blue] ; " << nl;
191 }
192
193 // root -> left
194 if(this->m_left)
195 {
196 get_node_name(os) << " -> ";
197 this->m_left->get_node_name(os) << " [ color = red ] ;" << nl;
198
199 // recursion for left children
200 this->m_left->print_node(os);
201 }
202
203 // root -> right
204 if(this->m_right)
205 {
206 get_node_name(os) << " -> ";
207 this->m_right->get_node_name(os) << " [color = blue ] ; " << nl;
208
209 // recursion for right children
210 this->m_right->print_node(os);
211 }
212 }
214 {
215 tpf::sstream os;
216
217 os << "digraph G { " << nl;
218
219 print_node(os);
220
221 os <<"}";
222
223 return os.str();
224 }
225
226 const ElementType& get() const { return this->m_value; }
227
228 // if value is not found,
229 // this function returns nullptr
230 void find_raw(ElementType value)
231 {
232 if(value < this->m_value)
233 {
234 if(this->m_left) this->m_left->find_raw(value);
235 }
236 else if (value > this->m_value)
237 {
238 if(this->m_right) this->m_right->find_raw(value);
239 }
240 else
241 {
242 throw this;
243 }
244 }
245
246 binary_node* find(ElementType value)
247 {
248 try
249 {
250 this->find_raw(value);
251 return nullptr;
252 }
253 catch(binary_node* ptr)
254 {
255 return ptr;
256 }
257 }
258
260 {
261 if(this->m_left)
262 return this->m_left->minimum();
263 else
264 return this;
265 }
266
268 {
269 if(this->m_right)
270 return this->m_right->maximum();
271 else
272 return this;
273 }
274
275 binary_node(ElementType value = ElementType{}, binary_node* parent = nullptr):
276 m_value{value}, m_parent{parent} { }
277
278 // if insertion fails, returns false
279
280 template<typename Type>
281 // we enable this function only when Type and ElementType are same
283 insert(Type&& value)
284 {
285 if(value == this->m_value)
286 return false;
287 else if(value < this->m_value)
288 {
289 if(this->m_left)
290 // this is recursion
291 return this->m_left->insert(std::forward<Type>(value));
292 else
293 {
294 // a new node is inserted and set to this->m_left member
295 // new inserted node is a Leaf node.
296 this->m_left.reset(new binary_node{std::forward<Type>(value), this} );
297
298 // this call updates all m_height members of its parents
299 this->m_left->update_height();
300
301 return true;
302 }
303 }
304 else // value > this->m_value
305 {
306 if(this->m_right)
307 // this is also recursion
308 return this->m_right->insert(std::forward<Type>(value));
309 else
310 {
311 // a new node is inserted and set to this->m_right member
312 // the new inserted node is a LEAF node
313 this->m_right.reset( new binary_node{std::forward<Type>(value), this});
314
315 // update all m_height members of its parents
316 this->m_right->update_height();
317
318 return true;
319 }
320 }
321 }
322
323 // node_ptr_t == std::unique_ptr<binary_node>
324 bool graft(node_ptr_t& node_ptr)
325 {
326 if(node_ptr->m_value < this->m_value)
327 {
328 if(this->m_left)
329 // this is recursion
330 return this->m_left->graft(node_ptr);
331 else
332 {
333 node_ptr->m_parent = this;
334 this->m_left = std::move(node_ptr);
335 this->m_left->update_height();
336
337 return true;
338 }
339 }
340 else // node_ptr->m_value > this->m_value
341 {
342 if(this->m_right)
343 // this is also recursion
344 return this->m_right->graft(node_ptr);
345 else
346 {
347 node_ptr->m_parent = this;
348 this->m_right = std::move(node_ptr);
349 this->m_right->update_height();
350
351 return true;
352 }
353 }
354 }
355
356 // return true only if all parameters are inserted
357 // successfully, otherwise returns false
358 template<typename Type, typename... Types>
359 // we enable this function only when ElementType, Type, and Types... are
360 // of same type
361 enable_if_all_types_are_the_same_t<bool, ElementType, Type, Types...>
362 insert(Type&& arg, Types&&... args)
363 {
364 bool result = this->insert(std::forward<Type>(arg));
365
366 if constexpr(sizeof...(args) != 0)
367 {
368 // recursion
369 return result && this->insert(std::forward<Types>(args)...);
370 }
371 else
372 return result;
373 }
374
375 // for more information about visiting order
376 // Binary Tree (ALL Interview Questions)
377 // https://www.youtube.com/watch?v=VQTF_pRTZek&list=PLeIMaH7i8JDj7DnmO7lll97P1yZjMCpgY&index=2
379 {
380 switch(order)
381 {
383 {
384 os << this->m_value << ", ";
385
386 if(this->m_left)
387 this->m_left->visit_nodes(os, order);
388
389 if(this->m_right)
390 this->m_right->visit_nodes(os, order);
391
392 return;
393 }
395 {
396 if(this->m_left)
397 this->m_left->visit_nodes(os, order);
398
399 if(this->m_right)
400 this->m_right->visit_nodes(os, order);
401
402 os << this->m_value << ", ";
403
404 return;
405 }
406
408 {
409 if(this->m_right)
410 this->m_right->visit_nodes(os, order);
411
412 os << this->m_value << ", ";
413
414 if(this->m_left)
415 this->m_left->visit_nodes(os, order);
416
417 return;
418 }
419
421 default: // in_order
422 {
423 if(this->m_left)
424 this->m_left->visit_nodes(os, order);
425
426 os << this->m_value << ", ";
427
428 if(this->m_right)
429 this->m_right->visit_nodes(os, order);
430
431 return;
432 }
433 }
434 }
435
436 // if fails, return nullptr
437 binary_node* find(ElementType value, find_mode fmode,
439 {
440 binary_node* ptr = nullptr;
441
442 if(((vmode==visit_mode::ascending_order) && (fmode==find_mode::successor))||
444 {
445 if(this->m_left && (ptr = this->m_left->find(value, fmode, vmode)))
446 return ptr;
447
448 if( value < this->m_value)
449 return this;
450
451 if(this->m_right && (ptr = this->m_right->find(value, fmode, vmode)))
452 return ptr;
453
454 // we failed
455 return ptr;
456 }
457 else if(( (vmode == visit_mode::ascending_order)&&(fmode==find_mode::predecessor)) ||
459 {
460 if(this->m_right && (ptr = this->m_right->find(value, fmode, vmode)))
461 return ptr;
462
463 if(value > this->m_value)
464 return this;
465
466 if(this->m_left && (ptr = this->m_left->find(value, fmode, vmode)))
467 return ptr;
468
469 return ptr;
470 }
471 else if(fmode == find_mode::exact_match)
472 return this->find(value);
473 else
474 return nullptr;
475 }
476
478 {
479 // both m_left and m_right are nullptr
480 return (!this->m_left && !this->m_right);
481 }
482
484 {
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);
489 else
490 return nullptr;
491 }
492
493 static bool remove_node(node_ptr_t& root_ptr, ElementType value)
494 {
495 binary_node* ptr = root_ptr->find(value);
496
497 // node with "value" does not exist
498 if(!ptr) return false;
499
500 // if parent is nullptr, then it is root node
501 if(!ptr->m_parent)
502 {
503 // ptr is a root node of type binary_node*
504 // root_ptr is a root node of type
505 // std::unique_ptr<binary_node>;
506
507 // we move left child to left_child
508 // after move, root_ptr->m_left is invalid
509 node_ptr_t left_child
510 = std::move(root_ptr->m_left);
511
512 // after move, root_ptr->m_right is invalid
513 node_ptr_t right_child
514 = std::move(root_ptr->m_right);
515
516 if(right_child) // new root
517 {
518 // because root node does not have its parent.
519 right_child->m_parent = nullptr;
520
521 // existing root_ptr is destroyed
522 // and right_child is moved to root_ptr
523 // at this point, existing old root is
524 // properly released.
525 root_ptr = std::move(right_child);
526
527 // do not forget this part
528 if(left_child)
529 root_ptr->graft(left_child);
530
531 return true;
532 }
533 else if(left_child) // right_child is nullptr
534 // left_child is a new root.
535 {
536 // because root node does not have its parent.
537 left_child->m_parent = nullptr;
538
539 // after move, the older root is released
540 // and left_child is moved to root_ptr
541 root_ptr = std::move(left_child);
542
543 return true;
544 }
545 else
546 {
547 // since we destroyed the root
548 // we should not access the root after this statment
549 root_ptr.reset();
550
551 return true;
552 }
553 }
554 else // non-root node
555 {
556 // ptr is the node to remove
557 // is not a root node.
558
559 if(ptr->is_leaf_node())
560 {
561 // this code cuts off the relation
562 // between the child node and its parent.
563 node_ptr_t orphan =
564 ptr->m_parent->release_child(ptr);
565
566 // when the orphan goes off this block,
567 // the allocated memory for orphan is
568 // cleaned up. and orphan.get() == ptr
569
570 if(ptr->m_parent->is_leaf_node())
571 {
572 ptr->m_parent->m_height = 1;
573 ptr->m_parent->update_height();
574 }
575
576 return true;
577 }
578 else // not a leaf node,
579 {
580 // cut off the relationship between ptr
581 // and its parent.
582 node_ptr_t orphan =
583 ptr->m_parent->release_child(ptr);
584
585 // this is for update the height of the parents
586 // nodes
587 ptr->m_parent->height(true);
588 ptr->m_parent->update_height();
589
590 node_ptr_t left_child =
591 std::move(orphan->m_left);
592
593 node_ptr_t right_child =
594 std::move(orphan->m_right);
595
596 if(right_child)
597 root_ptr->graft(right_child);
598
599 if(left_child)
600 root_ptr->graft(left_child);
601
602 return true;
603 }
604 }
605 }
606}; // end of binary_node class
607
608// std::unique_ptr<binary_node<ElementType>>
609template<typename ElementType>
611
612template<typename ElementType>
613void test_remove_node(node_ptr_t<ElementType>& node_ptr, ElementType value)
614{
615 using binary_node_t = binary_node<ElementType>;
616 using node_ptr_t = std::unique_ptr<binary_node_t>;
617
618 binary_node_t::remove_node(node_ptr, value);
619
620 if(node_ptr)
621 {
622 stream << node_ptr->build_digraph() << endl;
623 }
624
625}
626
628{
629 // In courtesy of Vivekanand Khyade - Algorithm Every Day
630 // Introduction to AVL tree (Why AVL tree is needed?)
631 // https://youtu.be/5Q-__zhQ2Gs?t=10
632
633 binary_node<int> root{10};
634
636
637 root.insert(8);
638 root.insert(15);
639
640 root.insert(7);
641 root.insert(9);
642 root.insert(12);
643 root.insert(17);
644
645 root.insert(6);
646 root.insert(16);
647 root.insert(18);
648
649 tpf::sstream os;
650
651 root.visit_nodes(os, visit_mode::ascending_order);
652 stream << "Ascending-order: " << endL;
653 stream << os.str() << endL;
654
655 os.clear();
656
657 root.visit_nodes(os, visit_mode::descending_order);
658 stream << "Descending-order: " << endL;
659 stream << os.str() << endL;
660}
661
663{
664 // In courtesy of Vivekanand Khyade - Algorithm Every Day
665 // Introduction to AVL tree (Why AVL tree is needed?)
666 // https://youtu.be/5Q-__zhQ2Gs?t=10
667
668 binary_node<int> root{10};
669
671
672 root.insert(8, 15, 7, 9, 12, 17, 6, 16, 18, 25);
673
674 if(auto ptr = root.find(6))
675 {
676 stream << "Value found: " << ptr->get() << endl;
677 }
678 else
679 {
680 stream <<"Value not found " << endl;
681 }
682
683 if(auto ptr = root.find(20) )
684 {
685 stream << "Value found: " << ptr->get() << endl;
686 }
687 else
688 {
689 stream << "Value not found " << endl;
690 }
691}
692
693
695{
696 // In courtesy of Vivekanand Khyade - Algorithm Every Day
697 // Introduction to AVL tree (Why AVL tree is needed?)
698 // https://youtu.be/5Q-__zhQ2Gs?t=10
699
700 binary_node<int> root{10};
701
703
704 root.insert(8, 15, 7, 9, 12, 17, 6, 16, 18);
705
706 stream << root.build_digraph() << endl;
707
708}
709
710void print_usage(string_t appname, const char* msg)
711{
712 stream << "Message: " << msg << endL;
713
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;
717
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;
720
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;
724
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;
728
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;
732
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;
736
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;
740}
741
742int main(int argc, const char* argv[])
743{
744 // test_ascending_descending_order();
745 // test_find_binary_tree();
746 // test_build_digraph();
747
748 string_t appname{argv[0]};
749
750 if(argc < 2)
751 {
752 print_usage(appname, "Program commandline syntax");
753
754 return 0;
755 }
756
757 string_t command{argv[1]};
758
759 if(command == "graph")
760 {
761 binary_node<int> root { std::atoi(argv[2])};
762
763 for(int i = 3; i < argc; ++i)
764 {
765 root.insert( std::atoi(argv[i]) );
766 }
767
768 stream << root.build_digraph() << endl;
769 }
770 else if(command == "list")
771 {
772 binary_node<int> root { std::atoi(argv[2])};
774
775 for(int i = 3; i < argc; ++i)
776 {
777 root.insert( std::atoi(argv[i]) );
778 }
779
780 stream <<"Ascending order: ";
781 root.visit_nodes(stream, visit_mode::ascending_order);
782 stream << endl;
783
784 stream <<"Descending order: ";
785 root.visit_nodes(stream, visit_mode::descending_order);
786 stream << endl;
787 }
788 else if(command == "ascending"
789 || command == "descending")
790 {
792 using find_mode = binary_node<int>::find_mode;
793
794 visit_mode vmode {visit_mode::undefined};
795 find_mode fmode {find_mode::undefined};
796
797 if(command == "ascending")
798 vmode = visit_mode::ascending_order;
799
800 if(command == "descending")
801 vmode = visit_mode::descending_order;
802
803 if(vmode==visit_mode::undefined)
804 {
805 print_usage(appname, "Undefined visit mode");
806 return 0;
807 }
808
809 if(argc < 5)
810 {
811 print_usage(appname, "Syntax Error");
812 return 0;
813 }
814
815 string_t operation{argv[2]};
816
817 if(operation == "successor_of")
818 fmode = find_mode::successor;
819
820 if(operation == "predecessor_of")
821 fmode = find_mode::predecessor;
822
823 if(operation == "exact_match_of")
824 fmode = find_mode::exact_match;
825
826 if(fmode == find_mode::undefined)
827 {
828 print_usage(appname, "Undefined find mode");
829 return 0;
830 }
831
832 int value = std::atoi(argv[3]);
833
834 binary_node<int> root { std::atoi(argv[4])};
836
837 for(int i = 5; i < argc; ++i)
838 {
839 root.insert( std::atoi(argv[i]) );
840 }
841
842 stream <<"Ascending order: ";
843 root.visit_nodes(stream, visit_mode::ascending_order);
844 stream << endl;
845
846 stream <<"Descending order: ";
847 root.visit_nodes(stream, visit_mode::descending_order);
848 stream << endl;
849
850 auto ptr = root.find(value, fmode, vmode);
851
852 if(ptr)
853 {
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;
860
861 if(vmode == visit_mode::ascending_order)
862 stream << " in ascending order is ";
863
864 if(vmode == visit_mode::descending_order)
865 stream << " in descending order is ";
866
867 stream << ptr->get() << endL;
868 }
869 else
870 {
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;
877
878 if(vmode == visit_mode::ascending_order)
879 stream << " in ascending order is NOT found";
880
881 if(vmode == visit_mode::descending_order)
882 stream << " in descending order NOT found ";
883
884 stream << endL;
885 }
886 }
887 else if(command == "remove")
888 {
889 using binary_node_t = binary_node<int>;
890 using node_ptr_t = std::unique_ptr<binary_node_t>;
891
892 // binary_tree remove 10 10
893 if(argc < 4)
894 {
895 print_usage(appname, "Syntax Error");
896 return 0;
897 }
898
899 int value = std::atoi(argv[2]);
900
901 node_ptr_t root = std::make_unique<binary_node_t>(std::atoi(argv[3]));
902
903 for(int i = 4; i < argc; ++i)
904 {
905 root->insert( std::atoi(argv[i]) );
906 }
907
908 test_remove_node(root, value);
909 }
910 else if(command == "find")
911 {
912 using binary_node_t = binary_node<int>;
913 using node_ptr_t = std::unique_ptr<binary_node_t>;
914
915 // binary_tree remove 10 10
916 if(argc < 4)
917 {
918 print_usage(appname, "Syntax Error");
919 return 0;
920 }
921
922 int value = std::atoi(argv[2]);
923
924 node_ptr_t root = std::make_unique<binary_node_t>(std::atoi(argv[3]));
925
926 for(int i = 4; i < argc; ++i)
927 {
928 root->insert( std::atoi(argv[i]) );
929 }
930
931 binary_node_t* ptr = root->find(value);
932
933 if(ptr)
934 {
935 stream << "We found value: " << value << endl;
936 }
937 else
938 {
939 stream << "We failed to find the value: " << value << endl;
940 }
941 }
942
943 else if(command == "nearest_parents")
944 {
945 using binary_node_t = binary_node<int>;
946 using node_ptr_t = std::unique_ptr<binary_node_t>;
947
948 // binary_tree remove 10 10
949 if(argc < 4)
950 {
951 print_usage(appname, "Syntax Error");
952 return 0;
953 }
954
955 int value = std::atoi(argv[2]);
956
957 node_ptr_t root = std::make_unique<binary_node_t>(std::atoi(argv[3]));
958
959 for(int i = 4; i < argc; ++i)
960 {
961 root->insert( std::atoi(argv[i]) );
962 }
963
964 auto left_parent_ptr = root->nearest_left_parent(value);
965 if(left_parent_ptr)
966 {
967 stream <<"The nearest left parent of the node "
968 << value << " is " << left_parent_ptr->get() << endl;
969 }
970 else
971 {
972 stream <<"The nearest left parent of the node "
973 << value << " is NOT foound." << endl;
974 }
975
976 auto right_parent_ptr = root->nearest_right_parent(value);
977 if(right_parent_ptr)
978 {
979 stream <<"The nearest right parent of the node "
980 << value << " is " << right_parent_ptr->get() << endl;
981 }
982 else
983 {
984 stream <<"The nearest right parent of the node "
985 << value << " is NOT foound." << endl;
986 }
987 }
988
989 else if(command == "min_max")
990 {
991 using binary_node_t = binary_node<int>;
992 using node_ptr_t = std::unique_ptr<binary_node_t>;
993
994 // binary_tree min_max 10 12
995 if(argc < 3)
996 {
997 print_usage(appname, "Syntax Error");
998 return 0;
999 }
1000
1001 node_ptr_t root = std::make_unique<binary_node_t>(std::atoi(argv[2]));
1002
1003 for(int i = 3; i < argc; ++i)
1004 {
1005 root->insert( std::atoi(argv[i]) );
1006 }
1007
1008 binary_node_t* ptr_min = root->minimum();
1009
1010 if(ptr_min)
1011 {
1012 stream << "We found minim value: " << ptr_min->get() << endl;
1013 }
1014 else
1015 {
1016 stream << "We failed to find minimum value " << endl;
1017 }
1018
1019
1020 binary_node_t* ptr_max = root->maximum();
1021
1022 if(ptr_min)
1023 {
1024 stream << "We found maximum value: " << ptr_max->get() << endl;
1025 }
1026 else
1027 {
1028 stream << "We failed to find maximum value " << endl;
1029 }
1030 }
1031}
std::string string_t
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
auto endL
tpf::sstream stream
auto nL
void test_remove_node(node_ptr_t< ElementType > &node_ptr, ElementType value)
void print_usage(string_t appname, const char *msg)
auto endl
void test_ascending_descending_order()
void test_find_binary_tree()
int main(int argc, const char *argv[])
auto nl
void test_build_digraph()
typename graph_t::visit_mode visit_mode
Definition: 060-graph02.cpp:8
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)
ElementType get() const
binary_node * nearest_right_parent(ElementType value)
binary_node * nearest_left_parent(ElementType value)
void nearest_right_parent_raw(binary_node *child)
string_t build_digraph()
const ElementType & get() const
node_ptr_t release_child(binary_node *ptr)
binary_node * maximum()
void print_node(tpf::sstream &os)
std::unique_ptr< binary_node > node_ptr_t
tpf::sstream & get_node_definition(tpf::sstream &os)
binary_node * minimum()
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)
std::string str() const
Definition: tpf_output.hpp:951
string_stream & clear()
Definition: tpf_output.hpp:916
constexpr auto endL
Definition: tpf_output.hpp:974
constexpr auto nL
Definition: tpf_output.hpp:972
constexpr auto endl
Definition: tpf_output.hpp:973
constexpr auto nl
Definition: tpf_output.hpp:971
Stream output operators << are implemented.