Skip to content

Commit cc860e1

Browse files
authored
Merge pull request #14 from Debashis08/release
Release
2 parents 349331d + 6440fb4 commit cc860e1

File tree

8 files changed

+232
-43
lines changed

8 files changed

+232
-43
lines changed

.github/CODEOWNERS

Lines changed: 0 additions & 4 deletions
This file was deleted.

Headers/0002_Tree/0001_BinarySearchTree.h

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#pragma once
22

3+
#include<vector>
34
#include<string>
45
using namespace std;
56
class Node
@@ -25,16 +26,20 @@ class BinarySearchTree
2526
Node* _FindPredecessorNode(Node* node);
2627
void _Transplant(Node* nodeU, Node* nodeV);
2728
void _DeleteNode(Node* node);
28-
string _RecursiveInorderTraversal(Node* node);
29-
string _RecursivePreorderTraversal(Node* node);
30-
string _RecursivePostorderTraversal(Node* node);
31-
string _MorrisInorderTraversal(Node* node);
32-
string _MorrisPreorderTraversal(Node* node);
33-
string _MorrisPostorderTraversal(Node* node);
29+
void _RecursiveInorderTraversal(Node* node, vector<int>& result);
30+
void _RecursivePreorderTraversal(Node* node, vector<int>& result);
31+
void _RecursivePostorderTraversal(Node* node, vector<int>& result);
32+
void _MorrisInorderTraversal(Node* node, vector<int>& result);
33+
void _MorrisPreorderTraversal(Node* node, vector<int>& result);
34+
void _MorrisPostorderTraversal(Node* node, vector<int>& result);
3435
public:
3536
BinarySearchTree();
3637
void InsertNode(int value);
3738
void DeleteNode(int value);
38-
string GetRecursiveInorderTravesalResult();
39-
string GetMorrisInorderTraversalResult();
39+
vector<int> GetRecursiveInorderTravesalResult();
40+
vector<int> GetRecursivePreorderTravesalResult();
41+
vector<int> GetRecursivePostorderTravesalResult();
42+
vector<int> GetMorrisInorderTraversalResult();
43+
vector<int> GetMorrisPreorderTraversalResult();
44+
vector<int> GetMorrisPostorderTraversalResult();
4045
};

SourceCodes/0002_Tree/0001_BinarySearchTree.cc

Lines changed: 123 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "../Headers/0002_Tree/0001_BinarySearchTree.h"
22
#include<iostream>
3+
#include<algorithm>
34
using namespace std;
45

56

@@ -164,37 +165,46 @@ void BinarySearchTree::_DeleteNode(Node* node)
164165
}
165166
}
166167

167-
string BinarySearchTree::_RecursiveInorderTraversal(Node* node)
168+
void BinarySearchTree::_RecursiveInorderTraversal(Node* node, vector<int>& result)
168169
{
169170
if (node == nullptr)
170171
{
171-
return "";
172+
return;
172173
}
173-
string leftSubTree = this->_RecursiveInorderTraversal(node->left);
174-
string currentNode = to_string(node->data);
175-
string rightSubTree = this->_RecursiveInorderTraversal(node->right);
174+
this->_RecursiveInorderTraversal(node->left, result);
175+
result.push_back(node->data);
176+
this->_RecursiveInorderTraversal(node->right, result);
177+
}
176178

177-
string result = leftSubTree;
178-
if (!leftSubTree.empty())
179+
void BinarySearchTree::_RecursivePreorderTraversal(Node* node, vector<int>& result)
180+
{
181+
if (node == nullptr)
179182
{
180-
result += " ";
183+
return;
181184
}
182-
result += currentNode;
183-
if (!rightSubTree.empty())
185+
result.push_back(node->data);
186+
this->_RecursivePreorderTraversal(node->left, result);
187+
this->_RecursivePreorderTraversal(node->right, result);
188+
}
189+
190+
void BinarySearchTree::_RecursivePostorderTraversal(Node* node, vector<int>& result)
191+
{
192+
if (node == nullptr)
184193
{
185-
result += " " + rightSubTree;
194+
return;
186195
}
187-
return result;
196+
this->_RecursivePostorderTraversal(node->left, result);
197+
this->_RecursivePostorderTraversal(node->right, result);
198+
result.push_back(node->data);
188199
}
189200

190-
string BinarySearchTree::_MorrisInorderTraversal(Node* node)
201+
void BinarySearchTree::_MorrisInorderTraversal(Node* node, vector<int>& result)
191202
{
192-
string result = "";
193203
while (node != nullptr)
194204
{
195205
if (node->left == nullptr)
196206
{
197-
result += to_string(node->data) + " ";
207+
result.push_back(node->data);
198208
node = node->right;
199209
}
200210
else
@@ -212,16 +222,74 @@ string BinarySearchTree::_MorrisInorderTraversal(Node* node)
212222
else
213223
{
214224
predecessor->right = nullptr;
215-
result += to_string(node->data) + " ";
225+
result.push_back(node->data);
216226
node = node->right;
217227
}
218228
}
219229
}
220-
if (!result.empty())
230+
}
231+
232+
void BinarySearchTree::_MorrisPreorderTraversal(Node* node, vector<int> & result)
233+
{
234+
while (node != nullptr)
221235
{
222-
result.pop_back();
236+
if (node->left == nullptr)
237+
{
238+
result.push_back(node->data);
239+
node = node->right;
240+
}
241+
else
242+
{
243+
Node* predecessor = node->left;
244+
while (predecessor->right != nullptr && predecessor->right != node)
245+
{
246+
predecessor = predecessor->right;
247+
}
248+
if (predecessor->right == nullptr)
249+
{
250+
predecessor->right = node;
251+
result.push_back(node->data);
252+
node = node->left;
253+
}
254+
else
255+
{
256+
predecessor->right = nullptr;
257+
node = node->right;
258+
}
259+
}
223260
}
224-
return result;
261+
}
262+
263+
void BinarySearchTree::_MorrisPostorderTraversal(Node* node, vector<int>& result)
264+
{
265+
while (node != nullptr)
266+
{
267+
if (node->right == nullptr)
268+
{
269+
result.push_back(node->data);
270+
node = node->left;
271+
}
272+
else
273+
{
274+
Node* predecessor = node->right;
275+
while (predecessor->left != nullptr && predecessor->left != node)
276+
{
277+
predecessor = predecessor->left;
278+
}
279+
if (predecessor->left == nullptr)
280+
{
281+
predecessor->left = node;
282+
result.push_back(node->data);
283+
node = node->right;
284+
}
285+
else
286+
{
287+
predecessor->left = nullptr;
288+
node = node->left;
289+
}
290+
}
291+
}
292+
reverse(result.begin(), result.end());
225293
}
226294

227295
void BinarySearchTree::InsertNode(int value)
@@ -236,12 +304,44 @@ void BinarySearchTree::DeleteNode(int value)
236304
this->_DeleteNode(node);
237305
}
238306

239-
string BinarySearchTree::GetRecursiveInorderTravesalResult()
307+
vector<int> BinarySearchTree::GetRecursiveInorderTravesalResult()
240308
{
241-
return this->_RecursiveInorderTraversal(this->_root);
309+
vector<int> result;
310+
this->_RecursiveInorderTraversal(this->_root, result);
311+
return result;
312+
}
313+
314+
vector<int> BinarySearchTree::GetRecursivePreorderTravesalResult()
315+
{
316+
vector<int> result;
317+
this->_RecursivePreorderTraversal(this->_root, result);
318+
return result;
319+
}
320+
321+
vector<int> BinarySearchTree::GetRecursivePostorderTravesalResult()
322+
{
323+
vector<int> result;
324+
this->_RecursivePostorderTraversal(this->_root, result);
325+
return result;
242326
}
243327

244-
string BinarySearchTree::GetMorrisInorderTraversalResult()
328+
vector<int> BinarySearchTree::GetMorrisInorderTraversalResult()
245329
{
246-
return this->_MorrisInorderTraversal(this->_root);
330+
vector<int> result;
331+
this->_MorrisInorderTraversal(this->_root, result);
332+
return result;
333+
}
334+
335+
vector<int> BinarySearchTree::GetMorrisPreorderTraversalResult()
336+
{
337+
vector<int> result;
338+
this->_MorrisPreorderTraversal(this->_root, result);
339+
return result;
340+
}
341+
342+
vector<int> BinarySearchTree::GetMorrisPostorderTraversalResult()
343+
{
344+
vector<int> result;
345+
this->_MorrisPostorderTraversal(this->_root, result);
346+
return result;
247347
}

Tests/0000_CommonUtilities/CMakeLists.txt

Whitespace-only changes.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#pragma once
2+
3+
#include<iostream>
4+
#include<vector>
5+
#include<string>
6+
using namespace std;
7+
8+
template<typename T>
9+
class UnitTestHelper
10+
{
11+
public:
12+
string VerifyVectorResult(vector<T> vector)
13+
{
14+
string result = "";
15+
for (auto iterator : vector)
16+
{
17+
result += to_string(iterator) + " ";
18+
}
19+
result.pop_back();
20+
return result;
21+
}
22+
};
Lines changed: 73 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,91 @@
11
#include <gtest/gtest.h>
22
#include<string>
33
#include "../Headers/0002_Tree/0001_BinarySearchTree.h"
4+
#include "../0000_CommonUtilities/UnitTestHelper.h"
45

5-
// Demonstrate some basic assertions.
66
namespace BinarySearchTreeTest
77
{
8-
TEST(BSTTesting, ShowBSTResultTest)
8+
UnitTestHelper<int> unitTestHelper;
9+
10+
TEST(BSTInsertData, RecursiveInorderTest)
11+
{
12+
BinarySearchTree bst;
13+
bst.InsertNode(50);
14+
bst.InsertNode(30);
15+
bst.InsertNode(60);
16+
17+
18+
string actualResult = unitTestHelper.VerifyVectorResult(bst.GetRecursiveInorderTravesalResult());
19+
string expectedResult = "30 50 60";
20+
21+
EXPECT_EQ(actualResult, expectedResult);
22+
}
23+
24+
TEST(BSTInsertData, RecursivePreorderTest)
925
{
1026
BinarySearchTree bst;
1127
bst.InsertNode(50);
1228
bst.InsertNode(30);
1329
bst.InsertNode(60);
1430

31+
string actualResult = unitTestHelper.VerifyVectorResult(bst.GetRecursivePreorderTravesalResult());
32+
string expectedResult = "50 30 60";
33+
34+
EXPECT_EQ(actualResult, expectedResult);
35+
}
36+
37+
TEST(BSTInsertData, RecursivePostorderTest)
38+
{
39+
BinarySearchTree bst;
40+
bst.InsertNode(50);
41+
bst.InsertNode(30);
42+
bst.InsertNode(60);
43+
44+
string actualResult = unitTestHelper.VerifyVectorResult(bst.GetRecursivePostorderTravesalResult());
45+
string expectedResult = "30 60 50";
46+
47+
EXPECT_EQ(actualResult, expectedResult);
48+
}
49+
50+
TEST(BSTInsertData, MorrisInorderTest)
51+
{
52+
BinarySearchTree bst;
53+
bst.InsertNode(50);
54+
bst.InsertNode(30);
55+
bst.InsertNode(60);
56+
1557

16-
string actualResult = bst.GetRecursiveInorderTravesalResult();
58+
string actualResult = unitTestHelper.VerifyVectorResult(bst.GetMorrisInorderTraversalResult());
1759
string expectedResult = "30 50 60";
1860

1961
EXPECT_EQ(actualResult, expectedResult);
2062
}
63+
64+
TEST(BSTInsertData, MorrisPreorderTest)
65+
{
66+
BinarySearchTree bst;
67+
bst.InsertNode(50);
68+
bst.InsertNode(30);
69+
bst.InsertNode(60);
70+
71+
72+
string actualResult = unitTestHelper.VerifyVectorResult(bst.GetMorrisPreorderTraversalResult());
73+
string expectedResult = "50 30 60";
74+
75+
EXPECT_EQ(actualResult, expectedResult);
76+
}
77+
78+
TEST(BSTInsertData, MorrisPostorderTest)
79+
{
80+
BinarySearchTree bst;
81+
bst.InsertNode(50);
82+
bst.InsertNode(30);
83+
bst.InsertNode(60);
84+
85+
86+
string actualResult = unitTestHelper.VerifyVectorResult(bst.GetMorrisPostorderTraversalResult());
87+
string expectedResult = "30 60 50";
88+
89+
EXPECT_EQ(actualResult, expectedResult);
90+
}
2191
}

Tests/0002_Tree/CMakeLists.txt

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,8 @@ add_executable(
1717
target_link_libraries(
1818
0002TreeTests
1919
GTest::gtest_main
20-
)
21-
22-
target_link_libraries(
23-
0002TreeTests
2420
0002TREE
2521
)
2622

27-
2823
include(GoogleTest)
2924
gtest_discover_tests(0002TreeTests)

Tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
add_subdirectory(0000_CommonUtilities)
12
add_subdirectory(0001_Basics)
23
add_subdirectory(0002_Tree)
34
add_subdirectory(0003_Graph)

0 commit comments

Comments
 (0)