Skip to content

Commit 1a7dd43

Browse files
authored
Merge pull request #9 from Debashis08/feature-tree-implementation
Feature tree implementation
2 parents 88f4b93 + fe9db0a commit 1a7dd43

File tree

5 files changed

+343
-0
lines changed

5 files changed

+343
-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: 246 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,246 @@
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+
}
118+
119+
void BinarySearchTree::_Transplant(Node* nodeU, Node* nodeV)
120+
{
121+
if (nodeU->parent == nullptr)
122+
{
123+
this->_root = nodeV;
124+
}
125+
else if (nodeU == nodeU->parent->left)
126+
{
127+
nodeU->parent->left = nodeV;
128+
}
129+
else
130+
{
131+
nodeU->parent->right = nodeV;
132+
}
133+
134+
if (nodeV != nullptr)
135+
{
136+
nodeV->parent = nodeU->parent;
137+
}
138+
}
139+
140+
void BinarySearchTree::_DeleteNode(Node* node)
141+
{
142+
if (node->left == nullptr)
143+
{
144+
this->_Transplant(node, node->right);
145+
}
146+
else if (node->right == nullptr)
147+
{
148+
this->_Transplant(node, node->left);
149+
}
150+
else
151+
{
152+
Node* nodeY = this->_FindMinimumValueNode(node->right);
153+
if (nodeY->parent != node)
154+
{
155+
this->_Transplant(nodeY, nodeY->right);
156+
nodeY->right = node->right;
157+
nodeY->right->parent = nodeY;
158+
}
159+
this->_Transplant(node, nodeY);
160+
nodeY->left = node->left;
161+
nodeY->left->parent = nodeY;
162+
delete node;
163+
}
164+
}
165+
166+
string BinarySearchTree::_RecursiveInorderTraversal(Node* node)
167+
{
168+
if (node == nullptr)
169+
{
170+
return "";
171+
}
172+
string leftSubTree = this->_RecursiveInorderTraversal(node->left);
173+
string currentNode = to_string(node->data);
174+
string rightSubTree = this->_RecursiveInorderTraversal(node->right);
175+
176+
string result = leftSubTree;
177+
if (!leftSubTree.empty())
178+
{
179+
result += " ";
180+
}
181+
result += currentNode;
182+
if (!rightSubTree.empty())
183+
{
184+
result += " " + rightSubTree;
185+
}
186+
return result;
187+
}
188+
189+
string BinarySearchTree::_MorrisInorderTraversal(Node* node)
190+
{
191+
string result = "";
192+
while (node != nullptr)
193+
{
194+
if (node->left == nullptr)
195+
{
196+
result += to_string(node->data) + " ";
197+
node = node->right;
198+
}
199+
else
200+
{
201+
Node* predecessor = node->left;
202+
while (predecessor->right != nullptr && predecessor->right != node)
203+
{
204+
predecessor = predecessor->right;
205+
}
206+
if (predecessor->right == nullptr)
207+
{
208+
predecessor->right = node;
209+
node = node->left;
210+
}
211+
else
212+
{
213+
predecessor->right = nullptr;
214+
result += to_string(node->data) + " ";
215+
node = node->right;
216+
}
217+
}
218+
}
219+
if (!result.empty())
220+
{
221+
result.pop_back();
222+
}
223+
return result;
224+
}
225+
226+
void BinarySearchTree::InsertNode(int value)
227+
{
228+
Node* node = new Node(value, nullptr, nullptr, nullptr);
229+
this->_InsertNode(node);
230+
}
231+
232+
void BinarySearchTree::DeleteNode(int value)
233+
{
234+
Node* node = this->_FindNode(value);
235+
this->_DeleteNode(node);
236+
}
237+
238+
string BinarySearchTree::GetRecursiveInorderTravesalResult()
239+
{
240+
return this->_RecursiveInorderTraversal(this->_root);
241+
}
242+
243+
string BinarySearchTree::GetMorrisInorderTraversalResult()
244+
{
245+
return this->_MorrisInorderTraversal(this->_root);
246+
}

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)