Skip to content

Commit bac4e70

Browse files
committed
[feat/fix/docs]: Various improvements
Forgot to split commits per change, apologizes!
1 parent d958eec commit bac4e70

18 files changed

+353
-223
lines changed

backtracking/graph_coloring.cpp

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,57 @@
1-
#include <stdio.h>
1+
#include <iostream>
22

3-
// Number of vertices in the graph
3+
/*
4+
* Number of vertices in the graph
5+
*/
46
#define V 4
57

8+
/**
9+
* Prototypes
10+
*/
611
void printSolution(int color[]);
712

813
/* A utility function to check if the current color assignment
914
is safe for vertex v */
1015
bool isSafe(int v, bool graph[V][V], int color[], int c) {
1116
for (int i = 0; i < V; i++)
12-
if (graph[v][i] && c == color[i])
17+
if (graph[v][i] && c == color[i]) {
1318
return false;
19+
}
1420
return true;
1521
}
1622

1723
/* A recursive utility function to solve m coloring problem */
1824
void graphColoring(bool graph[V][V], int m, int color[], int v) {
19-
/* base case: If all vertices are assigned a color then
20-
return true */
25+
// base case:
26+
// If all vertices are assigned a color then return true
2127
if (v == V) {
2228
printSolution(color);
2329
return;
2430
}
2531

26-
/* Consider this vertex v and try different colors */
32+
// Consider this vertex v and try different colors
2733
for (int c = 1; c <= m; c++) {
28-
/* Check if assignment of color c to v is fine*/
34+
// Check if assignment of color c to v is fine
2935
if (isSafe(v, graph, color, c)) {
3036
color[v] = c;
3137

32-
/* recur to assign colors to rest of the vertices */
38+
// recur to assign colors to rest of the vertices
3339
graphColoring(graph, m, color, v + 1);
3440

35-
/* If assigning color c doesn't lead to a solution
36-
then remove it */
41+
// If assigning color c doesn't lead to a solution then remove it
3742
color[v] = 0;
3843
}
3944
}
4045
}
4146

4247
/* A utility function to print solution */
4348
void printSolution(int color[]) {
44-
printf(" Following are the assigned colors \n");
45-
for (int i = 0; i < V; i++) printf(" %d ", color[i]);
46-
printf("\n");
49+
std::cout << "Following are the assigned colors\n");
50+
for (int i = 0; i < V; i++) {
51+
std::cout << color[i];
52+
}
53+
54+
std::cout << "\n";
4755
}
4856

4957
// driver program to test above function
@@ -65,7 +73,7 @@ int main() {
6573

6674
int color[V];
6775

68-
for (int i = 0; i < V; i++) color[i] = 0;
76+
for (int i = 0; i < V; i++) { color[i] = 0 };
6977

7078
graphColoring(graph, m, color, 0);
7179
return 0;

backtracking/knight_tour.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ bool solve(int x, int y, int mov, int sol[n][n], int xmov[n], int ymov[n]) {
3636
}
3737
return false;
3838
}
39+
40+
/**
41+
* Main function
42+
*/
3943
int main() {
4044
// initialize();
4145

@@ -49,11 +53,11 @@ int main() {
4953
sol[0][0] = 0;
5054

5155
bool flag = solve(0, 0, 1, sol, xmov, ymov);
52-
if (flag == false)
56+
if (flag == false) {
5357
cout << "solution doesnot exist \n";
54-
else {
58+
} else {
5559
for (i = 0; i < n; i++) {
56-
for (j = 0; j < n; j++) cout << sol[i][j] << " ";
60+
for (j = 0; j < n; j++) { cout << sol[i][j] << " " };
5761
cout << "\n";
5862
}
5963
}

backtracking/n_queens.cpp

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,38 @@
11
#include <iostream>
22
#define N 4
3-
using namespace std;
43

54
void printSolution(int board[N][N]) {
6-
cout << "\n";
5+
std::cout << "\n";
76
for (int i = 0; i < N; i++) {
8-
for (int j = 0; j < N; j++) cout << "" << board[i][j];
9-
cout << "\n";
7+
for (int j = 0; j < N; j++) {
8+
std::cout << "" << board[i][j];
9+
}
10+
std::cout << "\n";
1011
}
1112
}
1213

1314
bool isSafe(int board[N][N], int row, int col) {
1415
int i, j;
1516

16-
/* Check this row on left side */
17+
// Check this row on left side
1718
for (i = 0; i < col; i++)
18-
if (board[row][i])
19+
if (board[row][i]) {
1920
return false;
21+
}
2022

21-
/* Check upper diagonal on left side */
22-
for (i = row, j = col; i >= 0 && j >= 0; i--, j--)
23-
if (board[i][j])
23+
// Check upper diagonal on left side
24+
for (i = row, j = col; i >= 0 && j >= 0; i--, j--) {
25+
if (board[i][j]) {
2426
return false;
27+
}
28+
}
2529

26-
/* Check lower diagonal on left side */
27-
for (i = row, j = col; j >= 0 && i < N; i++, j--)
28-
if (board[i][j])
30+
// Check lower diagonal on left side
31+
for (i = row, j = col; j >= 0 && i < N; i++, j--) {
32+
if (board[i][j]) {
2933
return false;
34+
}
35+
}
3036

3137
return true;
3238
}
@@ -37,20 +43,18 @@ void solveNQ(int board[N][N], int col) {
3743
return;
3844
}
3945

40-
/* Consider this column and try placing
41-
this queen in all rows one by one */
46+
// Consider this column and try placing
47+
// this queen in all rows one by one
4248
for (int i = 0; i < N; i++) {
43-
/* Check if queen can be placed on
44-
board[i][col] */
49+
// Check if queen can be placed on board[i][col]
4550
if (isSafe(board, i, col)) {
46-
/* Place this queen in board[i][col] */
47-
// cout<<"\n"<<col<<"can place"<<i;
51+
// Place this queen in board[i][col]
52+
// std::cout<<"\n"<<col<<"can place"<<i;
4853
board[i][col] = 1;
4954

50-
/* recur to place rest of the queens */
55+
// recur to place rest of the queens
5156
solveNQ(board, col + 1);
52-
53-
board[i][col] = 0; // BACKTRACK
57+
board[i][col] = 0; // BACKTRACK
5458
}
5559
}
5660
}

backtracking/n_queens_all_solution_optimised.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ void NQueenSol(int Board[n][n], int col) {
5050
}
5151
}
5252

53+
/**
54+
* Main function
55+
*/
5356
int main() {
5457
int Board[n][n] = {0};
5558
if (n % 2 == 0) {

backtracking/rat_maze.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,15 @@
99
#include <iostream>
1010
#define size 4
1111

12-
using namespace std;
13-
1412
int solveMaze(int currposrow, int currposcol, int maze[size][size],
1513
int soln[size][size]) {
1614
if ((currposrow == size - 1) && (currposcol == size - 1)) {
1715
soln[currposrow][currposcol] = 1;
1816
for (int i = 0; i < size; ++i) {
1917
for (int j = 0; j < size; ++j) {
20-
cout << soln[i][j];
18+
std::cout << soln[i][j];
2119
}
22-
cout << endl;
20+
std::cout << std::endl;
2321
}
2422
return 1;
2523
} else {

backtracking/sudoku_solve.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
#include <iostream>
2-
using namespace std;
3-
/// N=9;
42
int n = 9;
53

64
bool isPossible(int mat[][9], int i, int j, int no) {
@@ -28,15 +26,15 @@ bool isPossible(int mat[][9], int i, int j, int no) {
2826
void printMat(int mat[][9]) {
2927
for (int i = 0; i < n; i++) {
3028
for (int j = 0; j < n; j++) {
31-
cout << mat[i][j] << " ";
29+
std::cout << mat[i][j] << " ";
3230
if ((j + 1) % 3 == 0) {
33-
cout << '\t';
31+
std::cout << '\t';
3432
}
3533
}
3634
if ((i + 1) % 3 == 0) {
37-
cout << endl;
35+
std::cout << std::endl;
3836
}
39-
cout << endl;
37+
std::cout << std::endl;
4038
}
4139
}
4240

data_structures/avltree.cpp

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,16 @@ node *createNode(int data) {
2222
node *nn = new node();
2323
nn->data = data;
2424
nn->height = 0;
25-
nn->left = NULL;
26-
nn->right = NULL;
25+
nn->left = nullptr;
26+
nn->right = nullptr;
2727
return nn;
2828
}
2929

3030
/** Returns height of tree */
3131
int height(node *root) {
32-
if (root == NULL)
32+
if (root == nullptr) {
3333
return 0;
34+
}
3435
return 1 + std::max(height(root->left), height(root->right));
3536
}
3637

@@ -57,15 +58,15 @@ node *leftRotate(node *root) {
5758

5859
/** Returns node with minimum value in the tree */
5960
node *minValue(node *root) {
60-
if (root->left == NULL)
61+
if (root->left == nullptr)
6162
return root;
6263
return minValue(root->left);
6364
}
6465

6566
/** Balanced Insertion */
6667
node *insert(node *root, int item) {
6768
node *nn = createNode(item);
68-
if (root == NULL)
69+
if (root == nullptr)
6970
return nn;
7071
if (item < root->data)
7172
root->left = insert(root->left, item);
@@ -86,24 +87,25 @@ node *insert(node *root, int item) {
8687

8788
/** Balanced Deletion */
8889
node *deleteNode(node *root, int key) {
89-
if (root == NULL)
90+
if (root == nullptr) {
9091
return root;
91-
if (key < root->data)
92+
}
93+
94+
if (key < root->data) {
9295
root->left = deleteNode(root->left, key);
93-
else if (key > root->data)
96+
} else if (key > root->data) {
9497
root->right = deleteNode(root->right, key);
95-
96-
else {
98+
} else {
9799
// Node to be deleted is leaf node or have only one Child
98100
if (!root->right) {
99101
node *temp = root->left;
100102
delete (root);
101-
root = NULL;
103+
root = nullptr;
102104
return temp;
103105
} else if (!root->left) {
104106
node *temp = root->right;
105107
delete (root);
106-
root = NULL;
108+
root = nullptr;
107109
return temp;
108110
}
109111
// Node to be deleted have both left and right subtrees
@@ -133,7 +135,7 @@ void levelOrder(node *root) {
133135
/** Main function */
134136
int main() {
135137
// Testing AVL Tree
136-
node *root = NULL;
138+
node *root = nullptr;
137139
int i;
138140
for (i = 1; i <= 7; i++) root = insert(root, i);
139141
std::cout << "LevelOrder: ";

0 commit comments

Comments
 (0)