Skip to content

Release #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Sep 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions .github/CODEOWNERS

This file was deleted.

21 changes: 13 additions & 8 deletions Headers/0002_Tree/0001_BinarySearchTree.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include<vector>
#include<string>
using namespace std;
class Node
Expand All @@ -25,16 +26,20 @@ class BinarySearchTree
Node* _FindPredecessorNode(Node* node);
void _Transplant(Node* nodeU, Node* nodeV);
void _DeleteNode(Node* node);
string _RecursiveInorderTraversal(Node* node);
string _RecursivePreorderTraversal(Node* node);
string _RecursivePostorderTraversal(Node* node);
string _MorrisInorderTraversal(Node* node);
string _MorrisPreorderTraversal(Node* node);
string _MorrisPostorderTraversal(Node* node);
void _RecursiveInorderTraversal(Node* node, vector<int>& result);
void _RecursivePreorderTraversal(Node* node, vector<int>& result);
void _RecursivePostorderTraversal(Node* node, vector<int>& result);
void _MorrisInorderTraversal(Node* node, vector<int>& result);
void _MorrisPreorderTraversal(Node* node, vector<int>& result);
void _MorrisPostorderTraversal(Node* node, vector<int>& result);
public:
BinarySearchTree();
void InsertNode(int value);
void DeleteNode(int value);
string GetRecursiveInorderTravesalResult();
string GetMorrisInorderTraversalResult();
vector<int> GetRecursiveInorderTravesalResult();
vector<int> GetRecursivePreorderTravesalResult();
vector<int> GetRecursivePostorderTravesalResult();
vector<int> GetMorrisInorderTraversalResult();
vector<int> GetMorrisPreorderTraversalResult();
vector<int> GetMorrisPostorderTraversalResult();
};
146 changes: 123 additions & 23 deletions SourceCodes/0002_Tree/0001_BinarySearchTree.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "../Headers/0002_Tree/0001_BinarySearchTree.h"
#include<iostream>
#include<algorithm>
using namespace std;


Expand Down Expand Up @@ -164,37 +165,46 @@ void BinarySearchTree::_DeleteNode(Node* node)
}
}

string BinarySearchTree::_RecursiveInorderTraversal(Node* node)
void BinarySearchTree::_RecursiveInorderTraversal(Node* node, vector<int>& result)
{
if (node == nullptr)
{
return "";
return;
}
string leftSubTree = this->_RecursiveInorderTraversal(node->left);
string currentNode = to_string(node->data);
string rightSubTree = this->_RecursiveInorderTraversal(node->right);
this->_RecursiveInorderTraversal(node->left, result);
result.push_back(node->data);
this->_RecursiveInorderTraversal(node->right, result);
}

string result = leftSubTree;
if (!leftSubTree.empty())
void BinarySearchTree::_RecursivePreorderTraversal(Node* node, vector<int>& result)
{
if (node == nullptr)
{
result += " ";
return;
}
result += currentNode;
if (!rightSubTree.empty())
result.push_back(node->data);
this->_RecursivePreorderTraversal(node->left, result);
this->_RecursivePreorderTraversal(node->right, result);
}

void BinarySearchTree::_RecursivePostorderTraversal(Node* node, vector<int>& result)
{
if (node == nullptr)
{
result += " " + rightSubTree;
return;
}
return result;
this->_RecursivePostorderTraversal(node->left, result);
this->_RecursivePostorderTraversal(node->right, result);
result.push_back(node->data);
}

string BinarySearchTree::_MorrisInorderTraversal(Node* node)
void BinarySearchTree::_MorrisInorderTraversal(Node* node, vector<int>& result)
{
string result = "";
while (node != nullptr)
{
if (node->left == nullptr)
{
result += to_string(node->data) + " ";
result.push_back(node->data);
node = node->right;
}
else
Expand All @@ -212,16 +222,74 @@ string BinarySearchTree::_MorrisInorderTraversal(Node* node)
else
{
predecessor->right = nullptr;
result += to_string(node->data) + " ";
result.push_back(node->data);
node = node->right;
}
}
}
if (!result.empty())
}

void BinarySearchTree::_MorrisPreorderTraversal(Node* node, vector<int> & result)
{
while (node != nullptr)
{
result.pop_back();
if (node->left == nullptr)
{
result.push_back(node->data);
node = node->right;
}
else
{
Node* predecessor = node->left;
while (predecessor->right != nullptr && predecessor->right != node)
{
predecessor = predecessor->right;
}
if (predecessor->right == nullptr)
{
predecessor->right = node;
result.push_back(node->data);
node = node->left;
}
else
{
predecessor->right = nullptr;
node = node->right;
}
}
}
return result;
}

void BinarySearchTree::_MorrisPostorderTraversal(Node* node, vector<int>& result)
{
while (node != nullptr)
{
if (node->right == nullptr)
{
result.push_back(node->data);
node = node->left;
}
else
{
Node* predecessor = node->right;
while (predecessor->left != nullptr && predecessor->left != node)
{
predecessor = predecessor->left;
}
if (predecessor->left == nullptr)
{
predecessor->left = node;
result.push_back(node->data);
node = node->right;
}
else
{
predecessor->left = nullptr;
node = node->left;
}
}
}
reverse(result.begin(), result.end());
}

void BinarySearchTree::InsertNode(int value)
Expand All @@ -236,12 +304,44 @@ void BinarySearchTree::DeleteNode(int value)
this->_DeleteNode(node);
}

string BinarySearchTree::GetRecursiveInorderTravesalResult()
vector<int> BinarySearchTree::GetRecursiveInorderTravesalResult()
{
return this->_RecursiveInorderTraversal(this->_root);
vector<int> result;
this->_RecursiveInorderTraversal(this->_root, result);
return result;
}

vector<int> BinarySearchTree::GetRecursivePreorderTravesalResult()
{
vector<int> result;
this->_RecursivePreorderTraversal(this->_root, result);
return result;
}

vector<int> BinarySearchTree::GetRecursivePostorderTravesalResult()
{
vector<int> result;
this->_RecursivePostorderTraversal(this->_root, result);
return result;
}

string BinarySearchTree::GetMorrisInorderTraversalResult()
vector<int> BinarySearchTree::GetMorrisInorderTraversalResult()
{
return this->_MorrisInorderTraversal(this->_root);
vector<int> result;
this->_MorrisInorderTraversal(this->_root, result);
return result;
}

vector<int> BinarySearchTree::GetMorrisPreorderTraversalResult()
{
vector<int> result;
this->_MorrisPreorderTraversal(this->_root, result);
return result;
}

vector<int> BinarySearchTree::GetMorrisPostorderTraversalResult()
{
vector<int> result;
this->_MorrisPostorderTraversal(this->_root, result);
return result;
}
Empty file.
22 changes: 22 additions & 0 deletions Tests/0000_CommonUtilities/UnitTestHelper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#pragma once

#include<iostream>
#include<vector>
#include<string>
using namespace std;

template<typename T>
class UnitTestHelper
{
public:
string VerifyVectorResult(vector<T> vector)
{
string result = "";
for (auto iterator : vector)
{
result += to_string(iterator) + " ";
}
result.pop_back();
return result;
}
};
76 changes: 73 additions & 3 deletions Tests/0002_Tree/0001_BinarySearchTreeTest.cc
Original file line number Diff line number Diff line change
@@ -1,21 +1,91 @@
#include <gtest/gtest.h>
#include<string>
#include "../Headers/0002_Tree/0001_BinarySearchTree.h"
#include "../0000_CommonUtilities/UnitTestHelper.h"

// Demonstrate some basic assertions.
namespace BinarySearchTreeTest
{
TEST(BSTTesting, ShowBSTResultTest)
UnitTestHelper<int> unitTestHelper;

TEST(BSTInsertData, RecursiveInorderTest)
{
BinarySearchTree bst;
bst.InsertNode(50);
bst.InsertNode(30);
bst.InsertNode(60);


string actualResult = unitTestHelper.VerifyVectorResult(bst.GetRecursiveInorderTravesalResult());
string expectedResult = "30 50 60";

EXPECT_EQ(actualResult, expectedResult);
}

TEST(BSTInsertData, RecursivePreorderTest)
{
BinarySearchTree bst;
bst.InsertNode(50);
bst.InsertNode(30);
bst.InsertNode(60);

string actualResult = unitTestHelper.VerifyVectorResult(bst.GetRecursivePreorderTravesalResult());
string expectedResult = "50 30 60";

EXPECT_EQ(actualResult, expectedResult);
}

TEST(BSTInsertData, RecursivePostorderTest)
{
BinarySearchTree bst;
bst.InsertNode(50);
bst.InsertNode(30);
bst.InsertNode(60);

string actualResult = unitTestHelper.VerifyVectorResult(bst.GetRecursivePostorderTravesalResult());
string expectedResult = "30 60 50";

EXPECT_EQ(actualResult, expectedResult);
}

TEST(BSTInsertData, MorrisInorderTest)
{
BinarySearchTree bst;
bst.InsertNode(50);
bst.InsertNode(30);
bst.InsertNode(60);


string actualResult = bst.GetRecursiveInorderTravesalResult();
string actualResult = unitTestHelper.VerifyVectorResult(bst.GetMorrisInorderTraversalResult());
string expectedResult = "30 50 60";

EXPECT_EQ(actualResult, expectedResult);
}

TEST(BSTInsertData, MorrisPreorderTest)
{
BinarySearchTree bst;
bst.InsertNode(50);
bst.InsertNode(30);
bst.InsertNode(60);


string actualResult = unitTestHelper.VerifyVectorResult(bst.GetMorrisPreorderTraversalResult());
string expectedResult = "50 30 60";

EXPECT_EQ(actualResult, expectedResult);
}

TEST(BSTInsertData, MorrisPostorderTest)
{
BinarySearchTree bst;
bst.InsertNode(50);
bst.InsertNode(30);
bst.InsertNode(60);


string actualResult = unitTestHelper.VerifyVectorResult(bst.GetMorrisPostorderTraversalResult());
string expectedResult = "30 60 50";

EXPECT_EQ(actualResult, expectedResult);
}
}
5 changes: 0 additions & 5 deletions Tests/0002_Tree/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,8 @@ add_executable(
target_link_libraries(
0002TreeTests
GTest::gtest_main
)

target_link_libraries(
0002TreeTests
0002TREE
)


include(GoogleTest)
gtest_discover_tests(0002TreeTests)
1 change: 1 addition & 0 deletions Tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
add_subdirectory(0000_CommonUtilities)
add_subdirectory(0001_Basics)
add_subdirectory(0002_Tree)
add_subdirectory(0003_Graph)
Expand Down
Loading