Dec 2023 Data Structure Solution Mumbai University
Dec 2023 Data Structure Solution Mumbai University
Q1)
For eg. Stack ADT
1 In Stack ADT implementation instead of data being stored in each node, the pointer to data is stored.
2 The program allocates memory for the data and address is passed to the stack ADT.
3 The head node and the data nodes are encapsulated in the ADT. The calling function can only see the
pointer to the stack.
4 The stack head structure also contains a pointer to top and count of number of entries currently in stack. As
Push(), pop(),peek(),size(), isempty(), isfull() is already implemented in STACK ADT, only programmer has to
use this function for implementing program on stack
B) Evaluate the postfix expression 94*28+- using stack ADT Show the process stepwise.
Initialize an empty stack.
Scan the postfix expression from left to right.
For each character in the expression:
If the character is an operand (number), push it onto the stack.
If the character is an operator, pop the required number of operands from the stack, perform the
operation, and push the result back onto the stack.
C) Justify the statement with suitable example: "Circular queue overcomes the disadvantage of linear
queue".
A circular queue is the extended version of a regular queue where the last element is connected to the
first element. Thus forming a circle- like structure.
The circular queue solves the major limitation of the normal queue. In a normal queue, after a bit of
insertion and deletion, there will be non- usable empty space.
Here, indexes 0 and 1 can only be used after resetting the queue (deletion of all elements). This
reduces the actual size of the queue.
Q.2
A) Construct Huffman tree and determine the code for each symbol in the string BCAADDDCCACACAC
1. Calculate the frequency of each character in the string. Frequency of string
2. Sort the characters in increasing order of the frequency. These are stored in a priority queue Q.
4. Create an empty node z. Assign the minimum frequency to the left child of z and assign the second minimum
frequency to the right child of z. Set the value of the z as the sum of the above two minimum frequencies.
8. For each non-leaf node, assign 0 to the left edge and 1 to the right edge.
For sending the above string over a network, we have to send the tree as well as the above compressed-code.
A 5 11 5*2 = 10
B 1 100 1*3 = 3
C 6 0 6*1 = 6
D 3 101 3*3 = 9
Without encoding, the total size of the string was 120 bits. After encoding the size is reduced to
32 + 15 + 28 =75.
B) Discuss the cases of deleting a node from Binary Search Tree with suitable example.
A Binary Search Tree (BST) is a special type of tree where the value of the left child node’s always
less than the parent node and the value of the right child node is greater than the parent node.
Commonly performed operations on binary search trees are searching, insertion, and deletion.
Here, we see Delete operation in detail.
Delete Operation
In a binary search tree, the Delete function is used to delete the specified node.
But, delete a node from a binary search tree in such a way, that the property of the binary search tree
doesn't violate.
To achieve this there are three methods for deleting a node from a binary search tree are used.
In this, replace the node that is going to be deleted with its only child node and then delete the child
node, which now contains the value which is to be deleted.
Replace it with the NULL and free the allocated space.
Consider the following example where the node with one child node whose value = 25 is deleted from
the BST:
In-order Traversal Sequence of the following Tree is 15, 25, 30, 60, 65, 75, 85, 90, 95.
A node with two children can be deleted from the BST in the following two ways:
Way 1:
Way 2:
Q.3
struct node
{
int info;
struct node *ptr;
}*front,*rear,*temp,*front1;
int frontelement();
void enq(int data);
void deq();
void empty();
void display();
void create();
void queuesize();
int count = 0;
void main()
{
int no, ch, e;
printf("\n 1 - Enque");
printf("\n 2 - Deque");
printf("\n 3 - Front element");
printf("\n 4 - Empty");
printf("\n 5 - Exit");
printf("\n 6 - Display");
printf("\n 7 - Queue size");
create();
while (1)
{
printf("\n Enter choice : ");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf("Enter data : ");
scanf("%d", &no);
enq(no);
break;
case 2:
deq();
break;
case 3:
e = frontelement();
if (e != 0)
printf("Front element : %d", e);
else
printf("\n No front element in Queue as queue is empty");
break;
case 4:
empty();
break;
case 5:
exit(0);
case 6:
display();
break;
case 7:
queuesize();
break;
default:
printf("Wrong choice, Please enter correct choice ");
break;
}
}
}
rear = temp;
}
count++;
}
if (front1 == NULL)
{
printf("\n Error: Trying to display elements from empty queue");
return;
}
else
if (front1->ptr != NULL)
{
front1 = front1->ptr;
printf("\n Dequed value : %d", front->info);
free(front);
front = front1;
}
else
{
printf("\n Dequed value : %d", front->info);
free(front);
front = NULL;
rear = NULL;
}
count--;
}
/* Returns the front element of queue */
int frontelement()
{
if ((front != NULL) && (rear != NULL))
return(front->info);
else
return 0;
}
B) Construct an AVL tree by inserting the following elements in the given order Apply necessary
rotations wherever required. 54, 12, 24, 68, 85, 99, 42, 27, 87, 80
LR-Left Rotation
Right Rotation
Q.4
void bfs(int v)
{
for (i = 1; i <= n; i++)
if (adj[v][i] && !visited[i])
queue[++rear] = i;
if (front <= rear)
{
visited[queue[front]] = 1;
bfs(queue[front++]);
}
}
void main()
{
int v;
printf("Enter the number of vertices: ");
scanf("%d", &n);
for (i = 1; i <= n; i++)
{
queue[i] = 0;
visited[i] = 0;
}
printf("Enter graph data in matrix form: \n");
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++)
scanf("%d", &adj[i][j]);
printf("Enter the starting vertex: ");
scanf("%d", &v);
bfs(v);
printf("The node which are reachable are: \n");
for (i = 1; i <= n; i++)
if (visited[i])
printf("%d\t", i);
else
printf("BFS is not possible. Not all nodes are reachable");
return 0;
}
ABDCEGF
if (*headRef == NULL) {
newNode->prev = NULL;
*headRef = newNode;
return;
}
last->next = newNode;
newNode->prev = last;
}
Q.5
Inorder: 35, 41, 48, 52, 57, 72, 79, 85, 86, 90
Preorder: 57, 41, 35. 52, 48, 90, 72, 85, 79, 86
Topological sorting for Directed Acyclic Graph (DAG) is a linear ordering of vertices such that for
every directed edge uv, vertex u comes before v in the ordering. Topological Sorting for a graph is
not possible if the graph is not a DAG.
For example, a topological sorting of the following graph is “5 4 2 3 1 0”.
in-Degree: Compute the in-degree (number of incoming edges) for each vertex.
Queue: Initialize a queue with all vertices that have in-degree 0.
the Queue:
Remove a vertex from the front of the queue.
Add it to the topological ordering.
Decrease the in-degree of its neighbors by 1.
If any neighbor's in-degree becomes 0, add it to the queue
There can be more than one topological sorting for a graph. For example, another topological
sorting of the following graph is “4 5 2 3 1 0”. The first vertex in topological sorting is always a
vertex with in-degree as 0
C) What is Collision? Using linear probing, insert the following values in the hash table of size 11 & count
the no. of collisions:
Hash collision is when two distinct pieces of data in a hash table share the same hash value. The hash
value in this case is derived from a hash function which takes a data input and returns a fixed length of
bits.
Q6
int main() {
struct Node* head = NULL;
// Creating a sample list: 1 -> 2 -> 3 -> 4 -> NULL
appendNode(&head, 1);
appendNode(&head, 2);
appendNode(&head, 3);
appendNode(&head, 4);