Skip to content

Commit 349331d

Browse files
authored
Merge pull request #12 from Debashis08/release
Release
2 parents 0f871a9 + 9298d7c commit 349331d

File tree

5 files changed

+344
-0
lines changed

5 files changed

+344
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#pragma once
2+
3+
#include<string>
4+
using namespace std;
5+
class Node
6+
{
7+
public:
8+
int data;
9+
Node* parent;
10+
Node* left;
11+
Node* right;
12+
13+
Node(int data, Node* parent, Node* left, Node* right);
14+
};
15+
16+
class BinarySearchTree
17+
{
18+
private:
19+
Node* _root;
20+
void _InsertNode(Node* node);
21+
Node* _FindNode(int value);
22+
Node* _FindMinimumValueNode(Node* node);
23+
Node* _FindMaximumValueNode(Node* node);
24+
Node* _FindSuccessorNode(Node* node);
25+
Node* _FindPredecessorNode(Node* node);
26+
void _Transplant(Node* nodeU, Node* nodeV);
27+
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);
34+
public:
35+
BinarySearchTree();
36+
void InsertNode(int value);
37+
void DeleteNode(int value);
38+
string GetRecursiveInorderTravesalResult();
39+
string GetMorrisInorderTraversalResult();
40+
};
Lines changed: 247 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,247 @@
1+
#include "../Headers/0002_Tree/0001_BinarySearchTree.h"
2+
#include<iostream>
3+
using namespace std;
4+
5+
6+
Node::Node(int data, Node* parent, Node* left, Node* right)
7+
{
8+
this->data = data;
9+
this->parent = parent;
10+
this->left = left;
11+
this->right = right;
12+
}
13+
14+
BinarySearchTree::BinarySearchTree()
15+
{
16+
this->_root = nullptr;
17+
}
18+
19+
20+
void BinarySearchTree::_InsertNode(Node* node)
21+
{
22+
Node* nodeY = nullptr;
23+
Node* nodeX = this->_root;
24+
while (nodeX != nullptr)
25+
{
26+
nodeY = nodeX;
27+
if (node->data < nodeX->data)
28+
{
29+
nodeX = nodeX->left;
30+
}
31+
else
32+
{
33+
nodeX = nodeX->right;
34+
}
35+
}
36+
node->parent = nodeY;
37+
if (nodeY == nullptr)
38+
{
39+
this->_root = node;
40+
}
41+
else if (node->data < nodeY->data)
42+
{
43+
nodeY->left = node;
44+
}
45+
else
46+
{
47+
nodeY->right = node;
48+
}
49+
}
50+
51+
Node* BinarySearchTree::_FindNode(int value)
52+
{
53+
Node* node = this->_root;
54+
while (node != nullptr)
55+
{
56+
if (value < node->data)
57+
{
58+
node = node->left;
59+
}
60+
else if(value > node->data)
61+
{
62+
node = node->right;
63+
}
64+
else
65+
{
66+
break;
67+
}
68+
}
69+
return node;
70+
}
71+
72+
Node* BinarySearchTree::_FindMinimumValueNode(Node* node)
73+
{
74+
while (node->left != nullptr)
75+
{
76+
node = node->left;
77+
}
78+
return node;
79+
}
80+
81+
Node* BinarySearchTree::_FindMaximumValueNode(Node* node)
82+
{
83+
while (node->right != nullptr)
84+
{
85+
node = node->right;
86+
}
87+
return node;
88+
}
89+
90+
Node* BinarySearchTree::_FindSuccessorNode(Node* node)
91+
{
92+
if (node->right != nullptr)
93+
{
94+
return this->_FindMinimumValueNode(node->right);
95+
}
96+
Node* nodeY = node->parent;
97+
while (nodeY != nullptr && node == nodeY->right)
98+
{
99+
node = nodeY;
100+
nodeY = nodeY->parent;
101+
}
102+
return nodeY;
103+
}
104+
105+
Node* BinarySearchTree::_FindPredecessorNode(Node* node)
106+
{
107+
if (node->left != nullptr)
108+
{
109+
return this->_FindMaximumValueNode(node->left);
110+
}
111+
Node* nodeY = node->parent;
112+
while (nodeY != nullptr && node == nodeY->left)
113+
{
114+
node = nodeY;
115+
nodeY = nodeY->parent;
116+
}
117+
return nodeY;
118+
}
119+
120+
void BinarySearchTree::_Transplant(Node* nodeU, Node* nodeV)
121+
{
122+
if (nodeU->parent == nullptr)
123+
{
124+
this->_root = nodeV;
125+
}
126+
else if (nodeU == nodeU->parent->left)
127+
{
128+
nodeU->parent->left = nodeV;
129+
}
130+
else
131+
{
132+
nodeU->parent->right = nodeV;
133+
}
134+
135+
if (nodeV != nullptr)
136+
{
137+
nodeV->parent = nodeU->parent;
138+
}
139+
}
140+
141+
void BinarySearchTree::_DeleteNode(Node* node)
142+
{
143+
if (node->left == nullptr)
144+
{
145+
this->_Transplant(node, node->right);
146+
}
147+
else if (node->right == nullptr)
148+
{
149+
this->_Transplant(node, node->left);
150+
}
151+
else
152+
{
153+
Node* nodeY = this->_FindMinimumValueNode(node->right);
154+
if (nodeY->parent != node)
155+
{
156+
this->_Transplant(nodeY, nodeY->right);
157+
nodeY->right = node->right;
158+
nodeY->right->parent = nodeY;
159+
}
160+
this->_Transplant(node, nodeY);
161+
nodeY->left = node->left;
162+
nodeY->left->parent = nodeY;
163+
delete node;
164+
}
165+
}
166+
167+
string BinarySearchTree::_RecursiveInorderTraversal(Node* node)
168+
{
169+
if (node == nullptr)
170+
{
171+
return "";
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 += " " + rightSubTree;
186+
}
187+
return result;
188+
}
189+
190+
string BinarySearchTree::_MorrisInorderTraversal(Node* node)
191+
{
192+
string result = "";
193+
while (node != nullptr)
194+
{
195+
if (node->left == nullptr)
196+
{
197+
result += to_string(node->data) + " ";
198+
node = node->right;
199+
}
200+
else
201+
{
202+
Node* predecessor = node->left;
203+
while (predecessor->right != nullptr && predecessor->right != node)
204+
{
205+
predecessor = predecessor->right;
206+
}
207+
if (predecessor->right == nullptr)
208+
{
209+
predecessor->right = node;
210+
node = node->left;
211+
}
212+
else
213+
{
214+
predecessor->right = nullptr;
215+
result += to_string(node->data) + " ";
216+
node = node->right;
217+
}
218+
}
219+
}
220+
if (!result.empty())
221+
{
222+
result.pop_back();
223+
}
224+
return result;
225+
}
226+
227+
void BinarySearchTree::InsertNode(int value)
228+
{
229+
Node* node = new Node(value, nullptr, nullptr, nullptr);
230+
this->_InsertNode(node);
231+
}
232+
233+
void BinarySearchTree::DeleteNode(int value)
234+
{
235+
Node* node = this->_FindNode(value);
236+
this->_DeleteNode(node);
237+
}
238+
239+
string BinarySearchTree::GetRecursiveInorderTravesalResult()
240+
{
241+
return this->_RecursiveInorderTraversal(this->_root);
242+
}
243+
244+
string BinarySearchTree::GetMorrisInorderTraversalResult()
245+
{
246+
return this->_MorrisInorderTraversal(this->_root);
247+
}

SourceCodes/0002_Tree/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Specify the source files
2+
set(0002TREE_SOURCES
3+
0001_BinarySearchTree.cc
4+
)
5+
6+
# Create a library target
7+
add_library(0002TREE ${0002TREE_SOURCES})
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include <gtest/gtest.h>
2+
#include<string>
3+
#include "../Headers/0002_Tree/0001_BinarySearchTree.h"
4+
5+
// Demonstrate some basic assertions.
6+
namespace BinarySearchTreeTest
7+
{
8+
TEST(BSTTesting, ShowBSTResultTest)
9+
{
10+
BinarySearchTree bst;
11+
bst.InsertNode(50);
12+
bst.InsertNode(30);
13+
bst.InsertNode(60);
14+
15+
16+
string actualResult = bst.GetRecursiveInorderTravesalResult();
17+
string expectedResult = "30 50 60";
18+
19+
EXPECT_EQ(actualResult, expectedResult);
20+
}
21+
}

Tests/0002_Tree/CMakeLists.txt

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
include(FetchContent)
2+
FetchContent_Declare(
3+
googletest
4+
URL https://github.com/google/googletest/archive/03597a01ee50ed33e9dfd640b249b4be3799d395.zip
5+
)
6+
# For Windows: Prevent overriding the parent project's compiler/linker settings
7+
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
8+
FetchContent_MakeAvailable(googletest)
9+
10+
11+
enable_testing()
12+
13+
add_executable(
14+
0002TreeTests
15+
0001_BinarySearchTreeTest.cc)
16+
17+
target_link_libraries(
18+
0002TreeTests
19+
GTest::gtest_main
20+
)
21+
22+
target_link_libraries(
23+
0002TreeTests
24+
0002TREE
25+
)
26+
27+
28+
include(GoogleTest)
29+
gtest_discover_tests(0002TreeTests)

0 commit comments

Comments
 (0)