Skip to content

Commit cd84f82

Browse files
Merge branch 'master' into cstdint
2 parents 10e2edb + f789e3b commit cd84f82

File tree

4 files changed

+115
-94
lines changed

4 files changed

+115
-94
lines changed

data_structures/circular_queue_using_linked_list.cpp

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,49 +5,65 @@ struct node {
55
struct node* next;
66
};
77
class Queue {
8-
node* front=nullptr;
9-
node* rear=nullptr;
8+
node* front = nullptr;
9+
node* rear = nullptr;
1010

11-
public:
12-
Queue() = default;
11+
Queue(const Queue&) = delete;
12+
Queue& operator=(const Queue&) = delete;
13+
14+
public:
15+
Queue() = default;
16+
~Queue() {
17+
while (front) {
18+
dequeue();
19+
}
20+
}
21+
22+
private:
1323
void createNode(int val) {
1424
auto* nn = new node;
1525
nn->data = val;
1626
nn->next = nullptr;
1727
front = nn;
1828
rear = nn;
1929
}
30+
31+
public:
2032
void enqueue(int val) {
2133
if (front == nullptr || rear == nullptr) {
2234
createNode(val);
23-
}
24-
else {
25-
node* nn;
26-
nn = new node;
35+
} else {
36+
node* nn = new node;
2737
nn->data = val;
2838
rear->next = nn;
2939
nn->next = front;
3040
rear = nn;
3141
}
3242
}
3343
void dequeue() {
34-
node* n;
35-
n = front;
36-
if (n) {
44+
if (front == nullptr) {
45+
return;
46+
}
47+
const node* const n = front;
48+
if (front == rear) {
49+
front = nullptr;
50+
rear = nullptr;
51+
} else {
3752
front = front->next;
38-
delete n;
53+
rear->next = front;
3954
}
55+
delete n;
4056
}
4157
void traverse() {
42-
node* ptr;
43-
ptr = front;
44-
if (ptr) {
45-
do {
46-
std::cout << ptr->data << " ";
47-
ptr = ptr->next;
48-
} while (ptr != rear->next);
49-
std::cout << front->data << std::endl;
58+
if (front == nullptr) {
59+
return;
5060
}
61+
const node* ptr = front;
62+
do {
63+
std::cout << ptr->data << ' ';
64+
ptr = ptr->next;
65+
} while (ptr != front);
66+
std::cout << '\n';
5167
}
5268
};
5369
int main(void) {
Lines changed: 70 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,81 @@
1-
/** Print all the Catalan numbers from 0 to n, n being the user input.
2-
3-
* A Catalan number satifies the following two properties:
4-
* C(0) = C(1) = 1; C(n) = sum(C(i).C(n-i-1)), from i = 0 to n-1
1+
/**
2+
* @file
3+
* @brief Provides utilities to compute Catalan numbers using dynamic
4+
programming.
5+
* A Catalan numbers satisfy these recurrence relations:
6+
* C(0) = C(1) = 1; C(n) = sum(C(i).C(n-i-1)), for i = 0 to n-1
57
* Read more about Catalan numbers here:
68
https://en.wikipedia.org/wiki/Catalan_number
9+
https://oeis.org/A000108/
710
*/
811

9-
#include <iostream>
10-
using namespace std;
11-
12-
int *cat; // global array to hold catalan numbers
13-
14-
unsigned long int catalan_dp(int n) {
15-
/** Using the tabulation technique in dynamic programming,
16-
this function computes the first `n+1` Catalan numbers
17-
18-
Parameter
19-
---------
20-
n: The number of catalan numbers to be computed.
12+
#include <cassert> /// for assert
13+
#include <cstdint> /// for std::uint64_t
14+
#include <cstdlib> /// for std::size_t
15+
#include <numeric> /// for std::transform_reduce
16+
#include <vector> /// for std::vector
2117

22-
Returns
23-
-------
24-
cat[n]: An array containing the first `n+1` Catalan numbers
25-
*/
26-
27-
// By definition, the first two Catalan numbers are 1
28-
cat[0] = cat[1] = 1;
29-
30-
// Compute the remaining numbers from index 2 to index n, using tabulation
31-
for (int i = 2; i <= n; i++) {
32-
cat[i] = 0;
33-
for (int j = 0; j < i; j++)
34-
cat[i] += cat[j] * cat[i - j - 1]; // applying the definition here
18+
/**
19+
* @brief computes and caches Catalan numbers
20+
*/
21+
class catalan_numbers {
22+
using value_type = std::uint64_t;
23+
std::vector<value_type> known{1, 1};
24+
25+
value_type compute_next() {
26+
return std::transform_reduce(known.begin(), known.end(), known.rbegin(),
27+
static_cast<value_type>(), std::plus<>(),
28+
std::multiplies<>());
3529
}
3630

37-
// Return the result
38-
return cat[n];
39-
}
40-
41-
int main(int argc, char *argv[]) {
42-
int n;
43-
cout << "Enter n: ";
44-
cin >> n;
45-
46-
cat = new int[n + 1];
47-
48-
cout << "Catalan numbers from 0 to " << n << " are:\n";
49-
for (int i = 0; i <= n; i++) {
50-
cout << "catalan (" << i << ") = " << catalan_dp(i) << endl;
51-
// NOTE: Since `cat` is a global array, calling `catalan_dp`
52-
// repeatedly will not recompute the the values already computed
53-
// as in case of pre-computed values, the array will simply return them,
54-
// instead of recomputing them.
31+
void add() { known.push_back(this->compute_next()); }
32+
33+
public:
34+
/**
35+
* @brief computes the n-th Catalan number and updates the cache.
36+
* @return the n-th Catalan number
37+
*/
38+
value_type get(std::size_t n) {
39+
while (known.size() <= n) {
40+
this->add();
41+
}
42+
return known[n];
5543
}
56-
57-
return 0;
44+
};
45+
46+
void test_catalan_numbers_up_to_20() {
47+
// data verified with https://oeis.org/A000108/
48+
catalan_numbers cn;
49+
assert(cn.get(0) == 1ULL);
50+
assert(cn.get(1) == 1ULL);
51+
assert(cn.get(2) == 2ULL);
52+
assert(cn.get(3) == 5ULL);
53+
assert(cn.get(4) == 14ULL);
54+
assert(cn.get(5) == 42ULL);
55+
assert(cn.get(6) == 132ULL);
56+
assert(cn.get(7) == 429ULL);
57+
assert(cn.get(8) == 1430ULL);
58+
assert(cn.get(9) == 4862ULL);
59+
assert(cn.get(10) == 16796ULL);
60+
assert(cn.get(11) == 58786ULL);
61+
assert(cn.get(12) == 208012ULL);
62+
assert(cn.get(13) == 742900ULL);
63+
assert(cn.get(14) == 2674440ULL);
64+
assert(cn.get(15) == 9694845ULL);
65+
assert(cn.get(16) == 35357670ULL);
66+
assert(cn.get(17) == 129644790ULL);
67+
assert(cn.get(18) == 477638700ULL);
68+
assert(cn.get(19) == 1767263190ULL);
69+
assert(cn.get(20) == 6564120420ULL);
5870
}
5971

60-
/** Sample Test Case:
61-
62-
$ cd "Dynamic Programming"
63-
$ g++ Catalan-Numbers.cpp
64-
$ ./a.exe
65-
66-
Enter n: 5
67-
Catalan numbers from 0 to 5 are:
68-
catalan (0) = 1
69-
catalan (1) = 1
70-
catalan (2) = 2
71-
catalan (3) = 5
72-
catalan (4) = 14
73-
catalan (5) = 42
72+
void test_catalan_numbers_25() {
73+
// data verified with https://oeis.org/A000108/
74+
catalan_numbers cn;
75+
assert(cn.get(25) == 4861946401452ULL);
76+
}
7477

75-
*/
78+
int main() {
79+
test_catalan_numbers_up_to_20();
80+
test_catalan_numbers_25();
81+
}

greedy_algorithms/huffman.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,15 @@ void deleteAll(const MinHeapNode* const root) {
3434
// For comparison of
3535
// two heap nodes (needed in min heap)
3636
struct compare {
37-
bool operator()(MinHeapNode* l, MinHeapNode* r)
38-
39-
{
40-
return (l->freq > r->freq);
37+
bool operator()(const MinHeapNode* const l,
38+
const MinHeapNode* const r) const {
39+
return l->freq > r->freq;
4140
}
4241
};
4342

4443
// Prints huffman codes from
4544
// the root of Huffman Tree.
46-
void printCodes(struct MinHeapNode* root, string str) {
45+
void printCodes(struct MinHeapNode* root, const string& str) {
4746
if (!root)
4847
return;
4948

@@ -56,8 +55,8 @@ void printCodes(struct MinHeapNode* root, string str) {
5655

5756
// The main function that builds a Huffman Tree and
5857
// print codes by traversing the built Huffman Tree
59-
void HuffmanCodes(char data[], int freq[], int size) {
60-
struct MinHeapNode *left, *right, *top;
58+
void HuffmanCodes(const char data[], const int freq[], int size) {
59+
struct MinHeapNode *left, *right;
6160

6261
// Create a min heap & inserts all characters of data[]
6362
priority_queue<MinHeapNode*, vector<MinHeapNode*>, compare> minHeap;
@@ -82,7 +81,7 @@ void HuffmanCodes(char data[], int freq[], int size) {
8281
// of this new node. Add this node
8382
// to the min heap '$' is a special value
8483
// for internal nodes, not used
85-
top = new MinHeapNode('$', left->freq + right->freq);
84+
auto* const top = new MinHeapNode('$', left->freq + right->freq);
8685

8786
top->left = left;
8887
top->right = right;

search/median_search2.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ static void test() {
103103
}
104104
temp->next = nullptr;
105105

106-
ListNode* median = search::median_search2::middleNode(head1);
106+
const ListNode* const median = search::median_search2::middleNode(head1);
107107
assert(3 == median->val); // 3 is the value of the median node.
108108
search::median_search2::deleteAll(head1);
109109
std::cout << "test case:1 passed\n";
@@ -124,7 +124,7 @@ static void test() {
124124
}
125125
temp2->next = nullptr;
126126

127-
ListNode* median1 = search::median_search2::middleNode(head2);
127+
const ListNode* const median1 = search::median_search2::middleNode(head2);
128128
assert(4 == median1->val); // 4 is the value of the median node.
129129
search::median_search2::deleteAll(head2);
130130
std::cout << "test case:2 passed\n";

0 commit comments

Comments
 (0)