ICT208 Algorithms and Data Structures
ICT208 Algorithms and Data Structures
1. Part A.....................................................................................................................................................2
1.1 Maze Exploration Using Recursive Function Code.........................................................................2
1.2 Snapshot...........................................................................................................................................4
1.2.1 Declaration Of 2d Array............................................................................................................4
1.2.2 Backtracking of Path.................................................................................................................4
1.2.3 Exploration Path........................................................................................................................5
1.2.4 Output.......................................................................................................................................5
2. Part B.....................................................................................................................................................6
2.1 Huffman Decoding Using Recursive Function................................................................................6
2.1.1 Code..............................................................................................................................................6
2.1.2 Snapshot..................................................................................................................................10
Figure 1 2D array.......................................................................................................................................3
Figure 2 path backtracking.........................................................................................................................3
Figure 3 Display of path............................................................................................................................4
Figure 4 Dimension Entry..........................................................................................................................4
Figure 5 Path Entry....................................................................................................................................4
Figure 6 Path Display.................................................................................................................................5
Figure 7character input..............................................................................................................................9
Figure 8output of Huffman decoding.........................................................................................................9
1. Part A
1.1 Maze Exploration Using Recursive Function Code
Here is the code that has been developed for the completion of the given task.
#include <iostream>
using namespace std;
// boolean for the rat to determine whether it is safe or not
bool isSafe(int **arr, int x, int y, int n)
{
if (x < n && y < n && arr[x][y] == 1)
{
return true;
}
return false;
}
// for the maze problem
bool inMaze(int **arr, int x, int y, int n, int **slArr)
{
if (x == n - 1 && y == n - 1)
{
slArr[x][y] = 1;
return true;
}
if (isSafe(arr, x, y, n))
{
slArr[x][y] = 1;
if (inMaze(arr, x + 1, y, n, slArr))
{
return true;
}
if (inMaze(arr, x, y + 1, n, slArr))
{
return true;
}
slArr[x][y] = 0;
return false;
}
return false;
}
// main function
int main()
{
int n;
cout << "Enter dimensions of the maze ";
cin >> n;
int **arr = new int *[n];
1
for (int i = 0; i < n; i++)
{
arr[i] = new int[n];
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
cin >> arr[i][j];
}
}
int **slArr = new int *[n];
for (int i = 0; i < n; i++)
{
slArr[i] = new int[n];
for (int j = 0; j < n; j++)
{
slArr[i][j] = 0;
}
}
if (inMaze(arr, 0, 0, n, slArr))
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
cout << slArr[i][j];
}
cout << endl;
}
}
return 0;
}
2
1.2 Snapshot
1.2.1 Declaration Of 2d Array
Figure 1 2D array
3
1.2.3 Exploration Path
Fi
gure 3 Display of path
1.2.4 Output
4
Figure 6 Path Display
This is the way of performing the maze concept for the rat by using the recursive function and here are
the output and the code of this concept.
2. Part B
2.1 Huffman Decoding Using Recursive Function
2.1.1 Code
#include <iostream>
#include <cstdlib>
using namespace std;
#define MAX_TREE_HT 100
struct MinHeapNode {
char data;
unsigned freq;
struct MinHeapNode *left, *right;
};
struct MinHeap {
unsigned size;
unsigned capacity;
struct MinHeapNode** array;
};
struct MinHeapNode* newNode(char data, unsigned freq)
{
struct MinHeapNode* temp = (struct MinHeapNode*)malloc (sizeof(struct MinHeapNode));
temp->left = temp->right = NULL;
temp->data = data;
temp->freq = freq;
return temp;
}
struct MinHeap* createMinHeap(unsigned capacity)
{
struct MinHeap* minHeap= (struct MinHeap*)malloc(sizeof(struct MinHeap));
minHeap->size = 0;
minHeap->capacity = capacity;
minHeap->array = (struct MinHeapNode**)malloc(minHeap->
capacity * sizeof(struct MinHeapNode*));
return minHeap;
}
5
void swapMinHeapNode(struct MinHeapNode** a, struct MinHeapNode** b)
{
struct MinHeapNode* t = *a;
*a = *b;
*b = t;
}
void minHeapify(struct MinHeap* minHeap, int idx)
{
int smallest = idx;
int left = 2 * idx + 1;
int right = 2 * idx + 2;
if (left < minHeap->size && minHeap->array[left]-> freq < minHeap->array[smallest]->freq)
smallest = left;
if (right < minHeap->size && minHeap->array[right]->
freq < minHeap->array[smallest]->freq)
smallest = right;
if (smallest != idx) {
swapMinHeapNode(&minHeap->array[smallest],
&minHeap->array[idx]);
minHeapify(minHeap, smallest);
}
}
int isSizeOne(struct MinHeap* minHeap)
{
return (minHeap->size == 1);
}
struct MinHeapNode* extractMin(struct MinHeap* minHeap)
{
struct MinHeapNode* temp = minHeap->array[0];
minHeap->array[0]
= minHeap->array[minHeap->size - 1];
--minHeap->size;
minHeapify(minHeap, 0);
return temp;
}
void insertMinHeap(struct MinHeap* minHeap,
struct MinHeapNode* minHeapNode)
{
++minHeap->size;
int i = minHeap->size - 1;
while (i && minHeapNode->freq < minHeap->array[(i - 1) / 2]->freq) {
minHeap->array[i] = minHeap->array[(i - 1) / 2];
i = (i - 1) / 2;
}
minHeap->array[i] = minHeapNode;
}
void buildMinHeap(struct MinHeap* minHeap)
6
int n = minHeap->size - 1;
int i;
for (i = (n - 1) / 2; i >= 0; --i)
minHeapify(minHeap, i);
}
void printArr(int arr[], int n)
{
int i;
for (i = 0; i < n; ++i)
cout<< arr[i];
cout<<"\n";
}
int isLeaf(struct MinHeapNode* root)
{
return !(root->left) && !(root->right);
}
struct MinHeap* createAndBuildMinHeap(char data[], int freq[], int size)
{
struct MinHeap* minHeap = createMinHeap(size);
for (int i = 0; i < size; ++i)
minHeap->array[i] = newNode(data[i], freq[i]);
minHeap->size = size;
buildMinHeap(minHeap);
return minHeap;
}
struct MinHeapNode* buildHuffmanTree(char data[], int freq[], int size)
{
struct MinHeapNode *left, *right, *top;
7
printCodes(root->right, arr, top + 1);
}
if (isLeaf(root)) {
cout<< root->data <<": ";
printArr(arr, top);
}
}
void HuffmanCodes(char data[], int freq[], int size)
{
8
2.1.2 Snapshot
This is the process for performing the Huffman decoding for any character given. There is also the code
and the snapshot of the output after performing the running of the code.