C++ Library Extensions 2022.12.09
To help learn modern C++ programming
048-binary_tree.cpp
Go to the documentation of this file.
1#include <tpf_output.hpp>
2
6
7using string_t = std::string;
8
9template<typename ElementType>
10class node
11{
12 public:
13
14 using node_ptr_t = std::unique_ptr<node>;
15
16 private:
17 ElementType m_value;
18
19 node_ptr_t m_left;
20 node_ptr_t m_right;
21
22 public:
23 node(ElementType value=ElementType{}):
24 m_value{value} { }
25
26 node_ptr_t& left() { return this->m_left; }
27 const node_ptr_t& left() const { return this->m_left; }
28
29 node_ptr_t& right() { return this->m_right; }
30 const node_ptr_t& right() const { return this->m_right; }
31
32 node& insert(ElementType value)
33 {
34 if(value < this->m_value)
35 {
36 if(this->m_left)
37 this->m_left->insert(value);
38 else
39 this->m_left.reset(new node{value});
40 }
41 else if(value > this->m_value)
42 {
43 if(this->m_right)
44 this->m_right->insert(value);
45 else
46 this->m_right.reset(new node{value});
47 }
48
49 return *this;
50 }
51
53 {
54 std::stringstream os;
55 os<< "node_" << this->m_value;
56 return os.str();
57 }
58
60 {
61 std::stringstream os;
62
63 os << get_node_name()
64 << " [ shape=oval, label=\" "
65 << this->m_value << " \"]";
66
67 return os.str();
68 }
69
70 void print_node(std::stringstream& os)
71 {
72 if(this->m_left)
73 {
74 os << get_node_name() << " -> "
75 << this->m_left->get_node_name() << ";" << std::endl;
76
77 this->m_left->print_node(os);
78 }
79
80 if(this->m_right)
81 {
82 os << get_node_name() << " -> "
83 << this->m_right->get_node_name() << ";" << std::endl;
84
85 this->m_right->print_node(os);
86 }
87
89 }
90
92 {
93 std::stringstream os;
94 os << "digraph G { "<< std::endl;
95
96 print_node(os);
97
98 os <<" }";
99
100 return os.str();
101 }
102
103 void print(bool bLeft=true)
104 {
105 if(bLeft)
106 {
107 if(this->m_left)
108 this->m_left->print(bLeft);
109
110 stream << "{ " << this->m_value << ", "
111 << this->m_balance << " }" << endl;
112
113 if(this->m_right)
114 this->m_right->print(bLeft);
115 }
116 else
117 {
118 if(this->m_right)
119 this->m_right->print(bLeft);
120
121 stream << "{ " << this->m_value << ", "
122 << this->m_balance << " }" << endl;
123
124 if(this->m_left)
125 this->m_left->print(bLeft);
126 }
127 }
128};
129
130 int main(int argc, const char* argv[])
131 {
132 if(argc < 2)
133 {
134 stream << "Usage: " << argv[0] << " [list of node values] " << endl;
135 return 0;
136 }
137
138 node<int> root{std::atoi(argv[1])};
139
140 for(int i=2; i < argc; ++i)
141 {
142 root.insert(std::atoi(argv[i]));
143 }
144
145 stream << root.build_digraph() << endl;
146
147 return 0;
148 }
auto endL
tpf::sstream stream
std::string string_t
auto endl
int main(int argc, const char *argv[])
string_t get_node_definition()
node(ElementType value=ElementType{})
void print_node(std::stringstream &os)
void print(bool bLeft=true)
const node_ptr_t & right() const
node_ptr_t & left()
std::unique_ptr< node > node_ptr_t
node & insert(ElementType value)
string_t get_node_name()
node_ptr_t & right()
const node_ptr_t & left() const
string_t build_digraph()
constexpr auto endL
Definition: tpf_output.hpp:974
constexpr auto endl
Definition: tpf_output.hpp:973
Stream output operators << are implemented.