Skip to content

Commit 8d7f865

Browse files
Merge branch 'master' into cstdint
2 parents bcae88d + 920b6d4 commit 8d7f865

File tree

4 files changed

+169
-14
lines changed

4 files changed

+169
-14
lines changed

data_structures/morrisinorder.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,21 @@ void morrisInorder(Btree *root) {
8181
}
8282
}
8383

84+
void deleteAll(const Btree *const root) {
85+
if (root) {
86+
deleteAll(root->left);
87+
deleteAll(root->right);
88+
delete root;
89+
}
90+
}
91+
8492
int main() {
8593
// Testing morrisInorder funtion
8694
Btree *root = NULL;
8795
int i;
8896
for (i = 1; i <= 7; i++) insert(&root, i);
8997
cout << "Morris Inorder: ";
9098
morrisInorder(root);
99+
deleteAll(root);
91100
return 0;
92101
}

greedy_algorithms/huffman.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@ struct MinHeapNode {
2323
}
2424
};
2525

26+
void deleteAll(const MinHeapNode* const root) {
27+
if (root) {
28+
deleteAll(root->left);
29+
deleteAll(root->right);
30+
delete root;
31+
}
32+
}
33+
2634
// For comparison of
2735
// two heap nodes (needed in min heap)
2836
struct compare {
@@ -85,6 +93,7 @@ void HuffmanCodes(char data[], int freq[], int size) {
8593
// Print Huffman codes using
8694
// the Huffman tree built above
8795
printCodes(minHeap.top(), "");
96+
deleteAll(minHeap.top());
8897
}
8998

9099
// Driver program to test above functions

greedy_algorithms/kruskals_minimum_spanning_tree.cpp

Lines changed: 133 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020

2121
#include <array> /// for array
2222
#include <iostream> /// for IO operations
23+
#include <limits> /// for numeric limits
24+
#include <cstdint> /// for uint32_t
2325

2426
/**
2527
* @namespace
@@ -32,14 +34,19 @@ namespace greedy_algorithms {
3234
* @param graph The graph that will be used to find the edge
3335
* @returns void
3436
*/
35-
template <typename T>
36-
void findMinimumEdge(const int &infinity,
37-
const std::array<std::array<T, 6>, 6> &graph) {
37+
template <typename T, std::size_t N, std::size_t M>
38+
void findMinimumEdge(const T &infinity,
39+
const std::array<std::array<T, N>, M> &graph) {
40+
if (N != M) {
41+
std::cout << "\nWrong input passed. Provided array has dimensions " << N
42+
<< "x" << M << ". Please provide a square matrix.\n";
43+
return;
44+
}
3845
for (int i = 0; i < graph.size(); i++) {
3946
int min = infinity;
4047
int minIndex = 0;
4148
for (int j = 0; j < graph.size(); j++) {
42-
if (graph[i][j] != 0 && graph[i][j] < min) {
49+
if (i != j && graph[i][j] != 0 && graph[i][j] < min) {
4350
min = graph[i][j];
4451
minIndex = j;
4552
}
@@ -50,20 +57,132 @@ void findMinimumEdge(const int &infinity,
5057
}
5158
} // namespace greedy_algorithms
5259

60+
/**
61+
* @brief Self-test implementations
62+
* @returns void
63+
*/
64+
static void test() {
65+
/**
66+
* define a large value for int
67+
* define a large value for float
68+
* define a large value for double
69+
* define a large value for uint32_t
70+
*/
71+
constexpr int INFINITY_INT = std::numeric_limits<int>::max();
72+
constexpr float INFINITY_FLOAT = std::numeric_limits<float>::max();
73+
constexpr double INFINITY_DOUBLE = std::numeric_limits<double>::max();
74+
constexpr uint32_t INFINITY_UINT32 = UINT32_MAX;
75+
76+
// Test case with integer values
77+
std::cout << "\nTest Case 1 :\n";
78+
std::array<std::array<int, 6>, 6> graph1{
79+
0, 4, 1, 4, INFINITY_INT, INFINITY_INT,
80+
4, 0, 3, 8, 3, INFINITY_INT,
81+
1, 3, 0, INFINITY_INT, 1, INFINITY_INT,
82+
4, 8, INFINITY_INT, 0, 5, 7,
83+
INFINITY_INT, 3, 1, 5, 0, INFINITY_INT,
84+
INFINITY_INT, INFINITY_INT, INFINITY_INT, 7, INFINITY_INT, 0};
85+
greedy_algorithms::findMinimumEdge(INFINITY_INT, graph1);
86+
87+
// Test case with floating values
88+
std::cout << "\nTest Case 2 :\n";
89+
std::array<std::array<float, 3>, 3> graph2{
90+
0.0f, 2.5f, INFINITY_FLOAT,
91+
2.5f, 0.0f, 3.2f,
92+
INFINITY_FLOAT, 3.2f, 0.0f};
93+
greedy_algorithms::findMinimumEdge(INFINITY_FLOAT, graph2);
94+
95+
// Test case with double values
96+
std::cout << "\nTest Case 3 :\n";
97+
std::array<std::array<double, 5>, 5> graph3{
98+
0.0, 10.5, INFINITY_DOUBLE, 6.7, 3.3,
99+
10.5, 0.0, 8.1, 15.4, INFINITY_DOUBLE,
100+
INFINITY_DOUBLE, 8.1, 0.0, INFINITY_DOUBLE, 7.8,
101+
6.7, 15.4, INFINITY_DOUBLE, 0.0, 9.9,
102+
3.3, INFINITY_DOUBLE, 7.8, 9.9, 0.0};
103+
greedy_algorithms::findMinimumEdge(INFINITY_DOUBLE, graph3);
104+
105+
// Test Case with negative weights
106+
std::cout << "\nTest Case 4 :\n";
107+
std::array<std::array<int, 3>, 3> graph_neg{
108+
0, -2, 4,
109+
-2, 0, 3,
110+
4, 3, 0};
111+
greedy_algorithms::findMinimumEdge(INFINITY_INT, graph_neg);
112+
113+
// Test Case with Self-Loops
114+
std::cout << "\nTest Case 5 :\n";
115+
std::array<std::array<int, 3>, 3> graph_self_loop{
116+
2, 1, INFINITY_INT,
117+
INFINITY_INT, 0, 4,
118+
INFINITY_INT, 4, 0};
119+
greedy_algorithms::findMinimumEdge(INFINITY_INT, graph_self_loop);
120+
121+
// Test Case with no edges
122+
std::cout << "\nTest Case 6 :\n";
123+
std::array<std::array<int, 4>, 4> no_edges{
124+
0, INFINITY_INT, INFINITY_INT, INFINITY_INT,
125+
INFINITY_INT, 0, INFINITY_INT, INFINITY_INT,
126+
INFINITY_INT, INFINITY_INT, 0, INFINITY_INT,
127+
INFINITY_INT, INFINITY_INT, INFINITY_INT, 0};
128+
greedy_algorithms::findMinimumEdge(INFINITY_INT, no_edges);
129+
130+
// Test Case with a non-connected graph
131+
std::cout << "\nTest Case 7:\n";
132+
std::array<std::array<int, 4>, 4> partial_graph{
133+
0, 2, INFINITY_INT, 6,
134+
2, 0, 3, INFINITY_INT,
135+
INFINITY_INT, 3, 0, 4,
136+
6, INFINITY_INT, 4, 0};
137+
greedy_algorithms::findMinimumEdge(INFINITY_INT, partial_graph);
138+
139+
// Test Case with Directed weighted graph. The Krushkal algorithm does not give
140+
// optimal answer
141+
std::cout << "\nTest Case 8:\n";
142+
std::array<std::array<int, 4>, 4> directed_graph{
143+
0, 3, 7, INFINITY_INT, // Vertex 0 has edges to Vertex 1 and Vertex 2
144+
INFINITY_INT, 0, 2, 5, // Vertex 1 has edges to Vertex 2 and Vertex 3
145+
INFINITY_INT, INFINITY_INT, 0, 1, // Vertex 2 has an edge to Vertex 3
146+
INFINITY_INT, INFINITY_INT, INFINITY_INT, 0}; // Vertex 3 has no outgoing edges
147+
greedy_algorithms::findMinimumEdge(INFINITY_INT, directed_graph);
148+
149+
// Test case with wrong input passed
150+
std::cout << "\nTest Case 9:\n";
151+
std::array<std::array<int, 4>, 3> graph9{
152+
0, 5, 5, 5,
153+
5, 0, 5, 5,
154+
5, 5, 5, 5};
155+
greedy_algorithms::findMinimumEdge(INFINITY_INT, graph9);
156+
157+
// Test case with all the same values between every edge
158+
std::cout << "\nTest Case 10:\n";
159+
std::array<std::array<int, 5>, 5> graph10{
160+
0, 5, 5, 5, 5,
161+
5, 0, 5, 5, 5,
162+
5, 5, 0, 5, 5,
163+
5, 5, 5, 0, 5,
164+
5, 5, 5, 5, 0};
165+
greedy_algorithms::findMinimumEdge(INFINITY_INT, graph10);
166+
167+
// Test Case with uint32_t values
168+
std::cout << "\nTest Case 11 :\n";
169+
std::array<std::array<uint32_t, 4>, 4> graph_uint32{
170+
0, 5, INFINITY_UINT32, 9,
171+
5, 0, 2, INFINITY_UINT32,
172+
INFINITY_UINT32, 2, 0, 6,
173+
9, INFINITY_UINT32, 6, 0};
174+
greedy_algorithms::findMinimumEdge(INFINITY_UINT32, graph_uint32);
175+
176+
std::cout << "\nAll tests have successfully passed!\n";
177+
}
178+
53179
/**
54180
* @brief Main function
55181
* @returns 0 on exit
56182
*/
183+
57184
int main() {
58-
constexpr int INFINITY = 99999;
59-
std::array<std::array<int, 6>, 6> graph{
60-
0, 4, 1, 4, INFINITY, INFINITY,
61-
4, 0, 3, 8, 3, INFINITY,
62-
1, 3, 0, INFINITY, 1, INFINITY,
63-
4, 8, INFINITY, 0, 5, 7,
64-
INFINITY, 3, 1, 5, 0, INFINITY,
65-
INFINITY, INFINITY, INFINITY, 7, INFINITY, 0};
66-
67-
greedy_algorithms::findMinimumEdge(INFINITY, graph);
185+
test(); // run Self-test implementation
68186
return 0;
69187
}
188+

operations_on_datastructures/reverse_binary_tree.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ class BinaryTree {
9191
return pivot;
9292
}
9393

94+
BinaryTree(const BinaryTree&) = delete;
95+
BinaryTree& operator=(const BinaryTree&) = delete;
96+
9497
public:
9598
/**
9699
* @brief Creates a BinaryTree with a root pointing to NULL.
@@ -100,6 +103,21 @@ class BinaryTree {
100103
* @brief Creates a BinaryTree with a root with an initial value.
101104
*/
102105
explicit BinaryTree(int64_t data) { root = new Node(data); }
106+
107+
~BinaryTree() {
108+
std::vector<Node*> nodes;
109+
nodes.emplace_back(root);
110+
while (!nodes.empty()) {
111+
const auto cur_node = nodes.back();
112+
nodes.pop_back();
113+
if (cur_node) {
114+
nodes.emplace_back(cur_node->left);
115+
nodes.emplace_back(cur_node->right);
116+
delete cur_node;
117+
}
118+
}
119+
}
120+
103121
/**
104122
* @brief Adds a new Node to the Binary Tree
105123
*/

0 commit comments

Comments
 (0)