Skip to content

Commit b495efa

Browse files
committed
Format CPP code using CLANG_FORMAT
1 parent 2a55493 commit b495efa

36 files changed

+1297
-1451
lines changed

src/cpp/BinarySearch.cpp

+29-42
Original file line numberDiff line numberDiff line change
@@ -3,52 +3,39 @@
33

44
using namespace std;
55

6-
int binarySearch(int value, vector<int> &vec, int leftIndex, int rightIndex)
7-
{
8-
int mid = (leftIndex + rightIndex) / 2;
9-
10-
if (leftIndex <= rightIndex)
11-
{
12-
if (value > vec[mid])
13-
{
14-
leftIndex = mid + 1;
15-
return binarySearch(value, vec, leftIndex, rightIndex);
16-
}
17-
else if (value < vec[mid])
18-
{
19-
rightIndex = mid - 1;
20-
return binarySearch(value, vec, leftIndex, rightIndex);
21-
}
22-
else
23-
{
24-
return mid;
25-
}
26-
}
27-
else
28-
{
29-
return -1;
6+
int binarySearch(int value, vector<int> &vec, int leftIndex, int rightIndex) {
7+
int mid = (leftIndex + rightIndex) / 2;
8+
9+
if (leftIndex <= rightIndex) {
10+
if (value > vec[mid]) {
11+
leftIndex = mid + 1;
12+
return binarySearch(value, vec, leftIndex, rightIndex);
13+
} else if (value < vec[mid]) {
14+
rightIndex = mid - 1;
15+
return binarySearch(value, vec, leftIndex, rightIndex);
16+
} else {
17+
return mid;
3018
}
19+
} else {
20+
return -1;
21+
}
3122
}
3223

33-
int main()
34-
{
35-
vector<int> vec;
36-
37-
for (int index = 1; index <= 50; index++)
38-
{
39-
vec.push_back(index);
40-
}
24+
int main() {
25+
vector<int> vec;
4126

42-
int value = 45;
27+
for (int index = 1; index <= 50; index++) {
28+
vec.push_back(index);
29+
}
4330

44-
int index = binarySearch(value, vec, 0, vec.size());
31+
int value = 45;
4532

46-
if (index >= 0)
47-
{
48-
cout << "Value " << to_string(value) << " found at position " << to_string(index) << endl;
49-
}
50-
else
51-
{
52-
cout << "Could not find the value " << to_string(value) << endl;
53-
}
33+
int index = binarySearch(value, vec, 0, vec.size());
34+
35+
if (index >= 0) {
36+
cout << "Value " << to_string(value) << " found at position "
37+
<< to_string(index) << endl;
38+
} else {
39+
cout << "Could not find the value " << to_string(value) << endl;
40+
}
5441
}

src/cpp/BinarySearchTree.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
* Structure for a binary tree node
55
*/
66
struct Node {
7-
int data; ///< The integer data value stored in the node.
8-
Node *left; ///< Pointer to the left child node.
9-
Node *right; ///< Pointer to the right child node.
7+
int data; ///< The integer data value stored in the node.
8+
Node *left; ///< Pointer to the left child node.
9+
Node *right; ///< Pointer to the right child node.
1010

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

1919
class BinarySearchTree {
20-
public:
20+
public:
2121
BinarySearchTree() : root(nullptr) {}
2222

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

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

35-
private:
35+
private:
3636
Node *root;
3737

3838
/**

src/cpp/BinaryTree.cpp

+91-107
Original file line numberDiff line numberDiff line change
@@ -3,124 +3,108 @@
33
using namespace std;
44

55
// Create a class for the BinaryTree
6-
class BinaryTree
7-
{
8-
// Create a struct for the TreeNode
9-
struct TreeNode
10-
{
11-
// Variables for the TreeNode
12-
int data;
13-
TreeNode* left;
14-
TreeNode* right;
15-
16-
// Constructor for the TreeNode
17-
TreeNode(int value) : data(value), left(nullptr), right(nullptr) {}
18-
};
19-
20-
// Private Variables and Functions
6+
class BinaryTree {
7+
// Create a struct for the TreeNode
8+
struct TreeNode {
9+
// Variables for the TreeNode
10+
int data;
11+
TreeNode *left;
12+
TreeNode *right;
13+
14+
// Constructor for the TreeNode
15+
TreeNode(int value) : data(value), left(nullptr), right(nullptr) {}
16+
};
17+
18+
// Private Variables and Functions
2119
private:
22-
TreeNode* root;
23-
24-
//Insert Function
25-
TreeNode* insert(TreeNode* root, int value)
26-
{
27-
if (root == nullptr)
28-
return new TreeNode(value);
29-
30-
if (value < root->data)
31-
root->left = insert(root->left, value);
32-
else
33-
root->right = insert(root->right, value);
34-
35-
return root;
36-
}
37-
38-
// Print Inorder Function
39-
void printInorder(TreeNode* head)
40-
{
41-
if (head != nullptr)
42-
{
43-
printInorder(head->left);
44-
cout << head->data << " ";
45-
printInorder(head->right);
46-
}
20+
TreeNode *root;
21+
22+
// Insert Function
23+
TreeNode *insert(TreeNode *root, int value) {
24+
if (root == nullptr)
25+
return new TreeNode(value);
26+
27+
if (value < root->data)
28+
root->left = insert(root->left, value);
29+
else
30+
root->right = insert(root->right, value);
31+
32+
return root;
33+
}
34+
35+
// Print Inorder Function
36+
void printInorder(TreeNode *head) {
37+
if (head != nullptr) {
38+
printInorder(head->left);
39+
cout << head->data << " ";
40+
printInorder(head->right);
4741
}
48-
49-
// Print Preorder Function
50-
void printPreorder(TreeNode* head)
51-
{
52-
if (head != nullptr)
53-
{
54-
cout << head->data << " ";
55-
printPreorder(head->left);
56-
printPreorder(head->right);
57-
}
42+
}
43+
44+
// Print Preorder Function
45+
void printPreorder(TreeNode *head) {
46+
if (head != nullptr) {
47+
cout << head->data << " ";
48+
printPreorder(head->left);
49+
printPreorder(head->right);
5850
}
59-
60-
// Print Postorder Function
61-
void printPostorder(TreeNode* head)
62-
{
63-
if (head != nullptr)
64-
{
65-
printPostorder(head->left);
66-
printPostorder(head->right);
67-
cout << head->data << " ";
68-
}
51+
}
52+
53+
// Print Postorder Function
54+
void printPostorder(TreeNode *head) {
55+
if (head != nullptr) {
56+
printPostorder(head->left);
57+
printPostorder(head->right);
58+
cout << head->data << " ";
6959
}
60+
}
7061

71-
// Public Functions
62+
// Public Functions
7263
public:
73-
// Constructor
74-
BinaryTree() : root(nullptr) {}
75-
76-
// Insert Function
77-
void insert(int value)
78-
{
79-
root = insert(root, value);
80-
}
81-
82-
// Print Inorder Function
83-
void printInorder()
84-
{
85-
printInorder(root);
86-
cout << endl;
87-
}
88-
89-
// Print Preorder Function
90-
void printPreorder()
91-
{
92-
printPreorder(root);
93-
cout << endl;
94-
}
95-
96-
// Print Postorder Function
97-
void printPostorder()
98-
{
99-
printPostorder(root);
100-
cout << endl;
101-
}
64+
// Constructor
65+
BinaryTree() : root(nullptr) {}
66+
67+
// Insert Function
68+
void insert(int value) { root = insert(root, value); }
69+
70+
// Print Inorder Function
71+
void printInorder() {
72+
printInorder(root);
73+
cout << endl;
74+
}
75+
76+
// Print Preorder Function
77+
void printPreorder() {
78+
printPreorder(root);
79+
cout << endl;
80+
}
81+
82+
// Print Postorder Function
83+
void printPostorder() {
84+
printPostorder(root);
85+
cout << endl;
86+
}
10287
};
10388

104-
int main()
105-
{
106-
// Create tree
107-
BinaryTree binaryTree;
89+
int main() {
90+
// Create tree
91+
BinaryTree binaryTree;
10892

109-
binaryTree.insert(10);
110-
binaryTree.insert(6);
111-
binaryTree.insert(15);
112-
binaryTree.insert(3);
113-
binaryTree.insert(8);
114-
binaryTree.insert(20);
93+
binaryTree.insert(10);
94+
binaryTree.insert(6);
95+
binaryTree.insert(15);
96+
binaryTree.insert(3);
97+
binaryTree.insert(8);
98+
binaryTree.insert(20);
11599

116-
cout << "InOrder: ";
117-
binaryTree.printInorder();
100+
cout << "InOrder: ";
101+
binaryTree.printInorder();
118102

119-
cout << "PreOrder: ";
120-
binaryTree.printPreorder();
103+
cout << "PreOrder: ";
104+
binaryTree.printPreorder();
121105

122-
cout << "PostOrder: ";
123-
binaryTree.printPostorder();
106+
cout << "PostOrder: ";
107+
binaryTree.printPostorder();
124108

125-
return 0;
109+
return 0;
126110
}

0 commit comments

Comments
 (0)