Skip to content

Commit

Permalink
Format CPP code using CLANG_FORMAT
Browse files Browse the repository at this point in the history
  • Loading branch information
kelvins committed Oct 23, 2024
1 parent 2a55493 commit b495efa
Show file tree
Hide file tree
Showing 36 changed files with 1,297 additions and 1,451 deletions.
71 changes: 29 additions & 42 deletions src/cpp/BinarySearch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,52 +3,39 @@

using namespace std;

int binarySearch(int value, vector<int> &vec, int leftIndex, int rightIndex)
{
int mid = (leftIndex + rightIndex) / 2;

if (leftIndex <= rightIndex)
{
if (value > vec[mid])
{
leftIndex = mid + 1;
return binarySearch(value, vec, leftIndex, rightIndex);
}
else if (value < vec[mid])
{
rightIndex = mid - 1;
return binarySearch(value, vec, leftIndex, rightIndex);
}
else
{
return mid;
}
}
else
{
return -1;
int binarySearch(int value, vector<int> &vec, int leftIndex, int rightIndex) {
int mid = (leftIndex + rightIndex) / 2;

if (leftIndex <= rightIndex) {
if (value > vec[mid]) {
leftIndex = mid + 1;
return binarySearch(value, vec, leftIndex, rightIndex);
} else if (value < vec[mid]) {
rightIndex = mid - 1;
return binarySearch(value, vec, leftIndex, rightIndex);
} else {
return mid;
}
} else {
return -1;
}
}

int main()
{
vector<int> vec;

for (int index = 1; index <= 50; index++)
{
vec.push_back(index);
}
int main() {
vector<int> vec;

int value = 45;
for (int index = 1; index <= 50; index++) {
vec.push_back(index);
}

int index = binarySearch(value, vec, 0, vec.size());
int value = 45;

if (index >= 0)
{
cout << "Value " << to_string(value) << " found at position " << to_string(index) << endl;
}
else
{
cout << "Could not find the value " << to_string(value) << endl;
}
int index = binarySearch(value, vec, 0, vec.size());

if (index >= 0) {
cout << "Value " << to_string(value) << " found at position "
<< to_string(index) << endl;
} else {
cout << "Could not find the value " << to_string(value) << endl;
}
}
10 changes: 5 additions & 5 deletions src/cpp/BinarySearchTree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
* Structure for a binary tree node
*/
struct Node {
int data; ///< The integer data value stored in the node.
Node *left; ///< Pointer to the left child node.
Node *right; ///< Pointer to the right child node.
int data; ///< The integer data value stored in the node.
Node *left; ///< Pointer to the left child node.
Node *right; ///< Pointer to the right child node.

/**
* Constructor to create a new node with the given data.
Expand All @@ -17,7 +17,7 @@ struct Node {
};

class BinarySearchTree {
public:
public:
BinarySearchTree() : root(nullptr) {}

Node *find(int x) const { return _find(this->root, x); }
Expand All @@ -32,7 +32,7 @@ class BinarySearchTree {

void postorderTraversal() const { _printPostorder(this->root); }

private:
private:
Node *root;

/**
Expand Down
198 changes: 91 additions & 107 deletions src/cpp/BinaryTree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,124 +3,108 @@
using namespace std;

// Create a class for the BinaryTree
class BinaryTree
{
// Create a struct for the TreeNode
struct TreeNode
{
// Variables for the TreeNode
int data;
TreeNode* left;
TreeNode* right;

// Constructor for the TreeNode
TreeNode(int value) : data(value), left(nullptr), right(nullptr) {}
};

// Private Variables and Functions
class BinaryTree {
// Create a struct for the TreeNode
struct TreeNode {
// Variables for the TreeNode
int data;
TreeNode *left;
TreeNode *right;

// Constructor for the TreeNode
TreeNode(int value) : data(value), left(nullptr), right(nullptr) {}
};

// Private Variables and Functions
private:
TreeNode* root;

//Insert Function
TreeNode* insert(TreeNode* root, int value)
{
if (root == nullptr)
return new TreeNode(value);

if (value < root->data)
root->left = insert(root->left, value);
else
root->right = insert(root->right, value);

return root;
}

// Print Inorder Function
void printInorder(TreeNode* head)
{
if (head != nullptr)
{
printInorder(head->left);
cout << head->data << " ";
printInorder(head->right);
}
TreeNode *root;

// Insert Function
TreeNode *insert(TreeNode *root, int value) {
if (root == nullptr)
return new TreeNode(value);

if (value < root->data)
root->left = insert(root->left, value);
else
root->right = insert(root->right, value);

return root;
}

// Print Inorder Function
void printInorder(TreeNode *head) {
if (head != nullptr) {
printInorder(head->left);
cout << head->data << " ";
printInorder(head->right);
}

// Print Preorder Function
void printPreorder(TreeNode* head)
{
if (head != nullptr)
{
cout << head->data << " ";
printPreorder(head->left);
printPreorder(head->right);
}
}

// Print Preorder Function
void printPreorder(TreeNode *head) {
if (head != nullptr) {
cout << head->data << " ";
printPreorder(head->left);
printPreorder(head->right);
}

// Print Postorder Function
void printPostorder(TreeNode* head)
{
if (head != nullptr)
{
printPostorder(head->left);
printPostorder(head->right);
cout << head->data << " ";
}
}

// Print Postorder Function
void printPostorder(TreeNode *head) {
if (head != nullptr) {
printPostorder(head->left);
printPostorder(head->right);
cout << head->data << " ";
}
}

// Public Functions
// Public Functions
public:
// Constructor
BinaryTree() : root(nullptr) {}

// Insert Function
void insert(int value)
{
root = insert(root, value);
}

// Print Inorder Function
void printInorder()
{
printInorder(root);
cout << endl;
}

// Print Preorder Function
void printPreorder()
{
printPreorder(root);
cout << endl;
}

// Print Postorder Function
void printPostorder()
{
printPostorder(root);
cout << endl;
}
// Constructor
BinaryTree() : root(nullptr) {}

// Insert Function
void insert(int value) { root = insert(root, value); }

// Print Inorder Function
void printInorder() {
printInorder(root);
cout << endl;
}

// Print Preorder Function
void printPreorder() {
printPreorder(root);
cout << endl;
}

// Print Postorder Function
void printPostorder() {
printPostorder(root);
cout << endl;
}
};

int main()
{
// Create tree
BinaryTree binaryTree;
int main() {
// Create tree
BinaryTree binaryTree;

binaryTree.insert(10);
binaryTree.insert(6);
binaryTree.insert(15);
binaryTree.insert(3);
binaryTree.insert(8);
binaryTree.insert(20);
binaryTree.insert(10);
binaryTree.insert(6);
binaryTree.insert(15);
binaryTree.insert(3);
binaryTree.insert(8);
binaryTree.insert(20);

cout << "InOrder: ";
binaryTree.printInorder();
cout << "InOrder: ";
binaryTree.printInorder();

cout << "PreOrder: ";
binaryTree.printPreorder();
cout << "PreOrder: ";
binaryTree.printPreorder();

cout << "PostOrder: ";
binaryTree.printPostorder();
cout << "PostOrder: ";
binaryTree.printPostorder();

return 0;
return 0;
}
Loading

0 comments on commit b495efa

Please sign in to comment.