@@ -164,86 +164,46 @@ void BinarySearchTree::_DeleteNode(Node* node)
164
164
}
165
165
}
166
166
167
- string BinarySearchTree::_RecursiveInorderTraversal (Node* node)
167
+ void BinarySearchTree::_RecursiveInorderTraversal (Node* node, vector< int >& result )
168
168
{
169
169
if (node == nullptr )
170
170
{
171
- return " " ;
171
+ return ;
172
172
}
173
- string leftSubTree = this ->_RecursiveInorderTraversal (node->left );
174
- string currentNode = to_string (node->data );
175
- string rightSubTree = this ->_RecursiveInorderTraversal (node->right );
176
-
177
- string result = leftSubTree;
178
- if (!leftSubTree.empty ())
179
- {
180
- result += " " ;
181
- }
182
- result += currentNode;
183
- if (!rightSubTree.empty ())
184
- {
185
- result += " " ;
186
- }
187
- result += rightSubTree;
188
- return result;
173
+ this ->_RecursiveInorderTraversal (node->left , result);
174
+ result.push_back (node->data );
175
+ this ->_RecursiveInorderTraversal (node->right , result);
189
176
}
190
177
191
- string BinarySearchTree::_RecursivePreorderTraversal (Node* node)
178
+ void BinarySearchTree::_RecursivePreorderTraversal (Node* node, vector< int >& result )
192
179
{
193
180
if (node == nullptr )
194
181
{
195
- return " " ;
196
- }
197
- string currentNode = to_string (node->data );
198
- string leftSubTree = this ->_RecursivePreorderTraversal (node->left );
199
- string rightSubTree = this ->_RecursivePreorderTraversal (node->right );
200
-
201
- string result = currentNode;
202
- if (!leftSubTree.empty ())
203
- {
204
- result += " " ;
182
+ return ;
205
183
}
206
- result += leftSubTree;
207
- if (!rightSubTree.empty ())
208
- {
209
- result += " " ;
210
- }
211
- result += rightSubTree;
212
- return result;
184
+ result.push_back (node->data );
185
+ this ->_RecursivePreorderTraversal (node->left , result);
186
+ this ->_RecursivePreorderTraversal (node->right , result);
213
187
}
214
188
215
- string BinarySearchTree::_RecursivePostorderTraversal (Node* node)
189
+ void BinarySearchTree::_RecursivePostorderTraversal (Node* node, vector< int >& result )
216
190
{
217
191
if (node == nullptr )
218
192
{
219
- return " " ;
220
- }
221
- string leftSubTree = this ->_RecursivePostorderTraversal (node->left );
222
- string rightSubTree = this ->_RecursivePostorderTraversal (node->right );
223
- string currentNode = to_string (node->data );
224
-
225
- string result = leftSubTree;
226
- if (!leftSubTree.empty ())
227
- {
228
- result += " " ;
229
- }
230
- result += rightSubTree;
231
- if (!rightSubTree.empty ())
232
- {
233
- result += " " ;
193
+ return ;
234
194
}
235
- result += currentNode;
236
- return result;
195
+ this ->_RecursivePostorderTraversal (node->left , result);
196
+ this ->_RecursivePostorderTraversal (node->right , result);
197
+ result.push_back (node->data );
237
198
}
238
199
239
- string BinarySearchTree::_MorrisInorderTraversal (Node* node)
200
+ void BinarySearchTree::_MorrisInorderTraversal (Node* node, vector< int >& result )
240
201
{
241
- string result = " " ;
242
202
while (node != nullptr )
243
203
{
244
204
if (node->left == nullptr )
245
205
{
246
- result += to_string (node->data ) + " " ;
206
+ result. push_back (node->data );
247
207
node = node->right ;
248
208
}
249
209
else
@@ -261,26 +221,20 @@ string BinarySearchTree::_MorrisInorderTraversal(Node* node)
261
221
else
262
222
{
263
223
predecessor->right = nullptr ;
264
- result += to_string (node->data ) + " " ;
224
+ result. push_back (node->data );
265
225
node = node->right ;
266
226
}
267
227
}
268
228
}
269
- if (!result.empty ())
270
- {
271
- result.pop_back ();
272
- }
273
- return result;
274
229
}
275
230
276
- string BinarySearchTree::_MorrisPreorderTraversal (Node* node)
231
+ void BinarySearchTree::_MorrisPreorderTraversal (Node* node, vector< int > & result )
277
232
{
278
- string result = " " ;
279
233
while (node != nullptr )
280
234
{
281
235
if (node->left == nullptr )
282
236
{
283
- result += to_string (node->data ) + " " ;
237
+ result. push_back (node->data );
284
238
node = node->right ;
285
239
}
286
240
else
@@ -293,7 +247,7 @@ string BinarySearchTree::_MorrisPreorderTraversal(Node* node)
293
247
if (predecessor->right == nullptr )
294
248
{
295
249
predecessor->right = node;
296
- result += to_string (node->data ) + " " ;
250
+ result. push_back (node->data );
297
251
node = node->left ;
298
252
}
299
253
else
@@ -303,21 +257,15 @@ string BinarySearchTree::_MorrisPreorderTraversal(Node* node)
303
257
}
304
258
}
305
259
}
306
- if (!result.empty ())
307
- {
308
- result.pop_back ();
309
- }
310
- return result;
311
260
}
312
261
313
- string BinarySearchTree::_MorrisPostorderTraversal (Node* node)
262
+ void BinarySearchTree::_MorrisPostorderTraversal (Node* node, vector< int >& result )
314
263
{
315
- string result = " " ;
316
264
while (node != nullptr )
317
265
{
318
266
if (node->right == nullptr )
319
267
{
320
- result += to_string (node->data ) + " " ;
268
+ result. push_back (node->data );
321
269
node = node->left ;
322
270
}
323
271
else
@@ -330,7 +278,7 @@ string BinarySearchTree::_MorrisPostorderTraversal(Node* node)
330
278
if (predecessor->left == nullptr )
331
279
{
332
280
predecessor->left = node;
333
- result += to_string (node->data ) + " " ;
281
+ result. push_back (node->data );
334
282
node = node->right ;
335
283
}
336
284
else
@@ -340,12 +288,7 @@ string BinarySearchTree::_MorrisPostorderTraversal(Node* node)
340
288
}
341
289
}
342
290
}
343
- if (!result.empty ())
344
- {
345
- result.pop_back ();
346
- }
347
291
reverse (result.begin (), result.end ());
348
- return result;
349
292
}
350
293
351
294
void BinarySearchTree::InsertNode (int value)
@@ -360,32 +303,44 @@ void BinarySearchTree::DeleteNode(int value)
360
303
this ->_DeleteNode (node);
361
304
}
362
305
363
- string BinarySearchTree::GetRecursiveInorderTravesalResult ()
306
+ vector< int > BinarySearchTree::GetRecursiveInorderTravesalResult ()
364
307
{
365
- return this ->_RecursiveInorderTraversal (this ->_root );
308
+ vector<int > result;
309
+ this ->_RecursiveInorderTraversal (this ->_root , result);
310
+ return result;
366
311
}
367
312
368
- string BinarySearchTree::GetRecursivePreorderTravesalResult ()
313
+ vector< int > BinarySearchTree::GetRecursivePreorderTravesalResult ()
369
314
{
370
- return this ->_RecursivePreorderTraversal (this ->_root );
315
+ vector<int > result;
316
+ this ->_RecursivePreorderTraversal (this ->_root , result);
317
+ return result;
371
318
}
372
319
373
- string BinarySearchTree::GetRecursivePostorderTravesalResult ()
320
+ vector< int > BinarySearchTree::GetRecursivePostorderTravesalResult ()
374
321
{
375
- return this ->_RecursivePostorderTraversal (this ->_root );
322
+ vector<int > result;
323
+ this ->_RecursivePostorderTraversal (this ->_root , result);
324
+ return result;
376
325
}
377
326
378
- string BinarySearchTree::GetMorrisInorderTraversalResult ()
327
+ vector< int > BinarySearchTree::GetMorrisInorderTraversalResult ()
379
328
{
380
- return this ->_MorrisInorderTraversal (this ->_root );
329
+ vector<int > result;
330
+ this ->_MorrisInorderTraversal (this ->_root , result);
331
+ return result;
381
332
}
382
333
383
- string BinarySearchTree::GetMorrisPreorderTraversalResult ()
334
+ vector< int > BinarySearchTree::GetMorrisPreorderTraversalResult ()
384
335
{
385
- return this ->_MorrisPreorderTraversal (this ->_root );
336
+ vector<int > result;
337
+ this ->_MorrisPreorderTraversal (this ->_root , result);
338
+ return result;
386
339
}
387
340
388
- string BinarySearchTree::GetMorrisPostorderTraversalResult ()
341
+ vector< int > BinarySearchTree::GetMorrisPostorderTraversalResult ()
389
342
{
390
- return this ->_MorrisPostorderTraversal (this ->_root );
343
+ vector<int > result;
344
+ this ->_MorrisPostorderTraversal (this ->_root , result);
345
+ return result;
391
346
}
0 commit comments