CS-21001 (DS) - CS End Nov 2024
CS-21001 (DS) - CS End Nov 2024
(b) Write a recursive C function to print all the elements present in a single linked list in 1
reverse order.
Solution:
// Recursive function to print the linked list in reverse order
void printReverse(struct Node* head) {
if (head == NULL) {
return; // Base case: if the list is empty, do nothing
}
// Recursive call to go to the next node
printReverse(head->next);
KIIT-DU/2024/SOT/Autumn End Semester Examination-2024
// Print the current node's data after the recursion
printf("%d ", head->data);
}
(c) Construct a binary tree from the following sequence of tree traversal: 1
In-order traversal =dbfeagcljhk
Post-order traversal =dfebgljkhca
Solution:
(e) In a max-heap of size n, what is the time complexity of extracting the maximum element 1
and then inserting a new element?
Solution:
Time Complexity:
• Extracting the maximum element: O(log n)
• Inserting a new element: O(log n)
Thus, the total time complexity for extracting the maximum element and then inserting a
new element is O(log n) + O(log n) = O(log n).
Conclusion:
Both operations — extracting the maximum element and inserting a new element — have
a time complexity of O(log n) in a max-heap of size n.
KIIT-DU/2024/SOT/Autumn End Semester Examination-2024
(f) Suppose the depth of a fully complete binary tree is 6. If the level of the tree is starting 1
from 0, how many nodes will be there in the tree?
Solution:
Given the depth d=6
Total nodes=26+1−1=27−1=128−1=127
(g) Write a modified bubble sort function whose time complexity is O (n) when it is applied 1
to an already sorted array. (n is the number of elements in the array)
Solution:
void bubbleSort(int arr[], int n)
{
int i, j;
int swapped; // A flag to check if a swap was made
// Traverse through all array elements
for (i = 0; i < n - 1; i++)
{
swapped = 0;
if(swapped == 0)
{
break;
}
}
}
1
What is the time complexity of the following code:
(h)
int num = 1, counter= N;
while (counter> 0) {
num =num* i;
counter= counter/2;
}
Solution:
Time Complexity:
The while loop executes O(log2(N)) iterations, and the operations inside the loop (num = num ×
i and counter=counter/2) take constant time O(1).
Thus, the overall time complexity is: O(log2N)
(i) Consider the following function applied to a single linked list with odd no. of nodes : 1
struct node * fun ( )
{
struct node *p, *q;
p=q=start;// start points to the first node of the list
while(q!=NULL && q->next!=NULL)
KIIT-DU/2024/SOT/Autumn End Semester Examination-2024
{
q=q->next->next;
p=p->next;
}
return p;
}
The code will return _______ node of the list.
(A) Last node
(B) Node before the last node
(C) Middle node
(D) None of options
Solution:
The code will return the middle node of the list.
Correct Option: (C) Middle node
(j) Suppose the key values {63, 22, 50, 23, 49, 57, 81} are stored in a hash table using the 1
hash function: H(k)=K%7. Linear probing is used to resolve collision if present. After
inserting the key values, the content of the hash table at index 4, 5, 6, and 7 are_______.
Solution:
Content at Indices 4, 5, 6, and 7:
Index 4: 49
Index 5: 57
Index 6: 81
Index 7: Not part of the table (since table size is 7).
Thus, the content at indices 4, 5, and 6 is 49, 57, and 81, respectively. Index 7 is out of bounds.
Explanation:
return 0;
}
(b) What is the difference between a single linked list and double linked list representation? [5]
Write a C function to rotate a double linked list anti-clockwise by k nodes, where k is a
given positive integer and smaller than the number of nodes in linked list.. For example,
if the given linked list is A->B->C->D->E->F and k is 3, the list should be modified to
D->E->F->A->B->C.
Scheme of Evaluation: Step marks can be given based on the partial correctness of the
answer.
Solution:
A single linked list and a double linked list are both types of linked list data structures,
but they differ in how they store and access elements. Here's a comparison of the node
structure of the two:
1. Node Structure:
Approach:
// If the right child is NULL or already visited, process the current node
if (curr->right == NULL || curr->right == prev) {
printf("%d ", curr->data); // Process the current node
pop(stack); // Pop the node
prev = curr; // Set prev to the current node
curr = NULL; // Move to the next node
} else {
// Otherwise, move to the right child
curr = curr->right;
}
}
free(stack->arr);
free(stack);
}
Stack ADT:
The stack is implemented using an array of pointers to Node structures.
The stack provides basic operations: push(), pop(), and isEmpty().
(b) Write a C function to construct a complete binary tree (using linked list) from a given [5]
level order traversal sequence of the tree.
Eg. :
Input: A B C D E F G H I H
Output:
Scheme of Evaluation: Step marks can be given based on the partial correctness of the
answer.
Solution:
#include <stdio.h>
#include <stdlib.h>
if (queue->rear == NULL) {
queue->front = queue->rear = newQueueNode;
return;
}
queue->rear->next = newQueueNode;
queue->rear = newQueueNode;
}
if (queue->front == NULL) {
KIIT-DU/2024/SOT/Autumn End Semester Examination-2024
queue->rear = NULL;
}
free(temp);
return node;
}
// Function to construct a complete binary tree from a given level order traversal
struct TreeNode* buildCompleteBinaryTree(int* levelOrder, int n) {
if (n == 0) return NULL;
int main() {
// Example level order traversal sequence
int levelOrder[] = {1, 2, 3, 4, 5, 6, 7};
int n = sizeof(levelOrder) / sizeof(levelOrder[0]);
Example:
1. Start with the deque: [3, 2, 5, 1, 4] (Front → Back).
2. Insert 6 at the back → [3, 2, 5, 1, 4, 6].
3. Delete from the front → [2, 5, 1, 4, 6].
4. Delete from the back → [2, 5, 1, 4].
Example:
1. Start with the deque: [3, 2, 5, 1, 4] (Front → Back).
2. Insert 6 at the back → [3, 2, 5, 1, 4, 6].
3. Insert 7 at the front → [7, 3, 2, 5, 1, 4, 6].
4. Delete from the front → [3, 2, 5, 1, 4, 6].
#include <stdio.h>
#include <stdlib.h>
// Define a structure for the input-restricted deque
typedef struct Deque {
int *arr;
int front;
int rear;
int size;
} Deque;
// Clean up memory
free(deque->arr);
free(deque);
return 0;
}
OUTPUT:
Deque contents: 1 2 3 2 1
The deque is a palindrome.
(b) Write a C code to implement a priority queue using a binary heap. Explain it with an [5]
appropriate example.
Scheme of Evaluation: Step marks can be given based on the partial correctness of the
answer. Correct implementation of Priority Queue With / without using Binary Heap
should be given fullmark.
Solution:
KIIT-DU/2024/SOT/Autumn End Semester Examination-2024
#include <stdio.h>
#include <stdlib.h>
// Function to maintain the max-heap property while removing the root element
void heapifyDown(struct PriorityQueue* pq, int index) {
int largest = index;
int leftChild = 2 * index + 1;
int rightChild = 2 * index + 2;
KIIT-DU/2024/SOT/Autumn End Semester Examination-2024
// Compare with the left child
if (leftChild < pq->size && pq->arr[leftChild] > pq->arr[largest]) {
largest = leftChild;
}
// If the largest is not the current node, swap and continue heapifying down
if (largest != index) {
swap(&pq->arr[index], &pq->arr[largest]);
heapifyDown(pq, largest);
}
}
// Function to extract the root element (highest priority) from the priority queue
int extractMax(struct PriorityQueue* pq) {
if (pq->size == 0) {
printf("Priority Queue is empty\n");
return -1; // Indicates empty queue
}
return max;
}
6. What is hashing? Explain collisions in hashing. Draw and explain the different hash [5]
tables with open addressing (quadratic probing, double hashing), and chaining by using
(a)
hash functions h1(k) = k % 10, h2(k) = 7- (k % 7), as per requirements, with table size
10 (indexed from 0 to 9) by inserting the keys: 62, 53, 74, 32, 86, 23, 55 into the table in
the given order. (Note: The hash function h2(k) is only used for double hashing
technique)
Scheme of Evaluation: Step marks can be given based on the partial correctness of the
answer. What is Hashing + collision of Hashing: 1 mark, quadratic probing,: 1 mark
double hashing:2 mark, and chaining: 1 mark.
Solution:
What is Hashing?
Hashing is a process used to map data of arbitrary size (like a string or a file) to a fixed
size using a hash function. It is often used in data structures like hash tables to quickly
locate a data record given its search key. The hash function takes a key as input and
returns an index in a hash table where the corresponding value is stored.
Open Addressing: In open addressing, all elements are stored within the hash table
itself. When a collision occurs, the algorithm looks for the next available slot within the
table.
Quadratic Probing: In quadratic probing, if a collision occurs at index i, the algorithm
checks (i + 1^2), (i + 2^2), and so on until an empty slot is found.
Double Hashing: In double hashing, two hash functions are used. The second hash
function determines the step size for probing. If a collision occurs at index i, the next
index is determined by i + h2(k) where h2(k) is another hash function.
Chaining: In chaining, each slot in the hash table contains a linked list (or another
structure like a tree). When a collision occurs, the new key-value pair is added to the list
at the same index.
Problem Setup:
Hash Table Size = 10
Hash Functions:
h1(k)=k%10h1(k)=k%10
h2(k)=7−(k%10)h2(k)=7−(k%10) (used only for double hashing)
Keys to Insert: 62, 53, 74, 32, 86, 23, 55
Let's insert the keys using the different collision handling techniques.
Draw the undirected graph from matrix A, where states are vertices and edges indicate
connected states (route of visitor X). Explain the Breadth-first Search (BFS) algorithm
while applying it to traverse from start at AS to MZ. AS is the starting vertex and the
goal is to reach MZ.
Scheme of Evaluation: Step marks can be given based on the partial correctness of the
answer. Draw Graph: 2 marks, BFS: 3 marks.
Solution:
The graph can be represented as:
AVL Tree:
An AVL tree is a self-balancing binary search tree. Named after its inventors (Adelson-
Velsky and Landis), it ensures that the difference between the heights of the left and
right subtrees of any node (called the balance factor) is at most 1. This balance ensures
the tree remains approximately balanced, which helps maintain optimal time complexity
for search, insert, and delete operations.
// Function to perform reverse in-order traversal (right, root, left) and print the
nodes
void printDescending(struct Node* root) {
if (root == NULL) {
return;
}
*****