1
+ #include " pch.h"
2
+ #include < iostream>
3
+ #include < algorithm>
4
+ #include < vector>
5
+ template <class T >
6
+ struct BTNode
7
+ {
8
+ BTNode (const T& data = new T(), BTNode* left = nullptr , BTNode* right = nullptr ) :
9
+ data (data),
10
+ left (left),
11
+ right (right)
12
+ {
13
+
14
+ }
15
+ T data;
16
+ BTNode<T>* left;
17
+ BTNode<T>* right;
18
+ };
19
+
20
+ BTNode<int >* build_sample_tree ()
21
+ {
22
+ BTNode<int >* root = new BTNode<int >(2 );
23
+ BTNode<int >* n1 = new BTNode<int >(1 );
24
+ BTNode<int >* n2 = new BTNode<int >(3 );
25
+ BTNode<int >* n3 = new BTNode<int >(4 );
26
+ BTNode<int >* n4 = new BTNode<int >(6 );
27
+ root->left = n1;
28
+ root->right = n2;
29
+ n2->right = n3;
30
+ n3->right = n4;
31
+ return root;
32
+ }
33
+ template <class T >
34
+ int CountNodes (const BTNode<T>* root)
35
+ {
36
+ if (root == nullptr )
37
+ {
38
+ return 0 ;
39
+ }
40
+ return CountNodes (root->left ) + CountNodes (root->right ) + 1 ;
41
+ }
42
+ template <class T >
43
+ int Height (const BTNode<T>* root)
44
+ {
45
+ if (root == nullptr )
46
+ {
47
+ return 0 ;
48
+ }
49
+ return std::max (Height (root->left ), Height (root->right )) + 1 ;
50
+ }
51
+ template <class T >
52
+ T Maximum (const BTNode<T>* root)
53
+ {
54
+ if (root->right == nullptr )
55
+ {
56
+ return root->data ;
57
+ }
58
+ return Maximum (root->right );
59
+ }
60
+ template <class T >
61
+ void Pre_order_Traversal (const BTNode<T>* root)
62
+ {
63
+ if (root == nullptr )
64
+ {
65
+ return ;
66
+ }
67
+ std::cout << root->data << " " ;
68
+ Pre_order_Traversal (root->left );
69
+ Pre_order_Traversal (root->right );
70
+ }
71
+ template <class T >
72
+ void In_order_Traversal (const BTNode<T>* root)
73
+ {
74
+
75
+ if (root == nullptr )
76
+ {
77
+ return ;
78
+ }
79
+ Pre_order_Traversal (root->left );
80
+ std::cout << root->data << " " ;
81
+ Pre_order_Traversal (root->right );
82
+ }
83
+ template <class T >
84
+ void Post_order_Traversal (const BTNode<T>* root)
85
+ {
86
+ if (root == nullptr )
87
+ {
88
+ return ;
89
+ }
90
+ Pre_order_Traversal (root->left );
91
+ Pre_order_Traversal (root->right );
92
+ std::cout << root->data << " " ;
93
+ }
94
+ template <class T >
95
+ bool FindInBst (const BTNode<T>* root, const T value)
96
+ {
97
+ if (root == nullptr )
98
+ {
99
+ return false ;
100
+ }
101
+ if (root->data == value)
102
+ {
103
+ return true ;
104
+ }
105
+ if (root->data <= value)
106
+ {
107
+ return FindInBst (root->right , value);
108
+ }
109
+ if (root->data > value)
110
+ {
111
+ return FindInBst (root->left , value);
112
+ }
113
+ }
114
+
115
+ template <class T >
116
+ void XmlSerialize (BTNode<T>* root, std::ostream& out = std::cout)
117
+ {
118
+ if (root == nullptr )
119
+ {
120
+ return ;
121
+ }
122
+ out << " <node data=\" " ;
123
+ out << root->data ;
124
+ out << " \" >\n " ;
125
+ out << " <left>\n " ;
126
+ XmlSerialize (root->left ,out);
127
+ out << " </left>\n " ;
128
+ out << " <right>\n " ;
129
+ XmlSerialize (root->right ,out);
130
+ out << " </right>\n " ;
131
+ out << " </node>\n " ;
132
+ }
133
+ template <class T >
134
+ void WriteTree (const BTNode<T>* root, std::ostream& out = std::cout)
135
+ {
136
+ if (root == nullptr )
137
+ {
138
+ out << " ()" ;
139
+ return ;
140
+ }
141
+
142
+ out << " (" ;
143
+ out << root->data << " ;" ;
144
+ WriteTree (root->left , out);
145
+ out << " ;" ;
146
+ WriteTree (root->right , out);
147
+ out << " )" ;
148
+ }
149
+
150
+
151
+ template <class T >
152
+ bool isBSTHelper (const BTNode<T>* root, bool hasMin = false , const T& min = T(), bool hasMax = false, const T& max = T())
153
+ {
154
+ if (root == nullptr )
155
+ return true ;
156
+
157
+ if (hasMin && root->data <= min || hasMax && root->data > max)
158
+ return false ;
159
+
160
+ bool isLeftOK = isBSTHelper (root->left , hasMin, min, true , root->data );
161
+ bool isRightOK = isBSTHelper (root->right , true , root->data , hasMax, max);
162
+
163
+ return isLeftOK && isRightOK;
164
+ }
165
+
166
+ template <class T >
167
+ bool isBST (const BTNode<T>* root)
168
+ {
169
+ return isBSTHelper (root, false , T (), false , T ());
170
+ }
171
+
172
+
173
+ template <class T >
174
+ BTNode<T>* ReadTree (std::istream& in = std::cin)
175
+ {
176
+ char open_br;
177
+ in >> open_br;
178
+
179
+ char x;
180
+ in >> x;
181
+ if (x == ' )' )
182
+ {
183
+ return nullptr ;
184
+ }
185
+ in.putback (x);
186
+ BTNode<T>* root = new BTNode<T>;
187
+ in >> root->data ;
188
+ in >> x; // ;
189
+ root->left = ReadTree<T>(in);
190
+ in >> x; // ;
191
+ root->right = ReadTree<T>(in);
192
+ in >> x; // )
193
+ return root;
194
+ }
195
+
196
+
197
+ template <class T >
198
+ void XmlSerialize (const BTNode<T>* root, std::ostream& out = std::cout)
199
+ {
200
+ if (!root)
201
+ return ;
202
+
203
+ out << " <node data=\" " << root->data << " \" >\n " ;
204
+ out << " <left>\n " ;
205
+ XmlSerialize (root->left , out);
206
+ out << " </left>\n " ;
207
+ out << " <right>\n " ;
208
+ XmlSerialize (root->right , out);
209
+ out << " </right>\n " ;
210
+ out << " </node>" ;
211
+ }
212
+
213
+ template <class T >
214
+ BTNode<T>* XMLDeserialize (std::istream& in = std::cin)
215
+ {
216
+ char x;
217
+ if (!(in >> x)) // <
218
+ return nullptr ;
219
+
220
+ in >> x; // /
221
+ if (x != ' n' )
222
+ {
223
+ return nullptr ;
224
+ }
225
+ std::string temp;
226
+ // in >> temp; //ode
227
+ getline (in, temp, ' "' ); // ode data="
228
+ std::string value;
229
+ getline (in, value, ' "' );
230
+ BTNode<T>* root = new BTNode<T>;
231
+ root->data = boost::lexical_cast<T>(value);
232
+ in >> x; // >
233
+ getline (in, temp, ' >' ); // <left>
234
+ root->left = XMLDeserialize<T>(in);
235
+ getline (in, temp, ' >' ); // left>
236
+
237
+ getline (in, temp, ' >' ); // <right>
238
+ root->right = XMLDeserialize<T>(in);
239
+ getline (in, temp, ' >' ); // right>
240
+ getline (in, temp, ' >' ); // </node>
241
+ return root;
242
+
243
+ }
244
+
245
+ int main ()
246
+ {
247
+ auto a = build_sample_tree ();
248
+ std::cout << Height (a) << " \n " ;
249
+ std::cout << Maximum (a) << " \n " ;
250
+ Pre_order_Traversal (a);
251
+ std::cout << " \n " ;
252
+ In_order_Traversal (a);
253
+ std::cout << " \n " ;
254
+ Post_order_Traversal (a);
255
+ std::cout << " \n " ;
256
+ std::cout << FindInBst (a, 6 ) << " \n " ;
257
+ std::cout << FindInBst (a, 9 ) << " \n " ;
258
+ XmlSerialize (a);
259
+ }
0 commit comments