0% found this document useful (0 votes)
190 views7 pages

DSA Final Solution

The document is an exam paper for a Data Structures course. It contains 6 questions testing students' knowledge of data structures and algorithms. The questions cover topics like AVL trees, hash tables, binary trees, graphs, and heap sort. Students are instructed to answer all questions in the spaces provided and not use red ink. They are not allowed to ask for additional paper as rough sheets are attached.

Uploaded by

Jannat Nasir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
190 views7 pages

DSA Final Solution

The document is an exam paper for a Data Structures course. It contains 6 questions testing students' knowledge of data structures and algorithms. The questions cover topics like AVL trees, hash tables, binary trees, graphs, and heap sort. Students are instructed to answer all questions in the spaces provided and not use red ink. They are not allowed to ask for additional paper as rough sheets are attached.

Uploaded by

Jannat Nasir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

National University of Computer and Emerging Sciences, Lahore Campus

Course: Data Structures Course Code: CS2001


Program: BS (CS, SE, DS) Semester: Spring 2022
Duration: 180 Minutes Total Marks: 90
Paper Date: 08-June-2022 Weight 45 %
Section: ALL Page(s): 11
Exam: Final term Roll No.
Instruction/Notes: Attempt all questions. Answer in the space provided. You cannot ask for rough sheets
they are attached with this exam. Answers written on the rough sheet will not be
marked. Do not use red ink to answer the questions. In case of confusion or ambiguity
make a reasonable assumption.

Question 1: Built an AVL Tree by inserting in order of input arrival: [10+5 Marks] [CLO 1, CLO 2]

Insertion Sequence = {29, 95, 59, 49, 39, 42, 67, 63, 40} after inserting following values now delete below
sequence in order. Deletion Sequence = {49, 59, 63}

FAST School of Computing Page 1


FAST School of Computing Page 2
Question 2: Insert the following values (keys) in order of arrival using the following hash function.

[10 + 5 Marks] [CLO 1, CLO 4]

Note: Solve it using the open addressing technique.

𝐻𝑎𝑠ℎ 𝐹𝑢𝑛𝑐𝑡𝑖𝑜𝑛 = ∑ 𝑖, 𝑗 = 0: 𝑛 (𝑋𝑖 ∗ 𝑃𝑗 ) % 𝑡𝑎𝑏𝑙𝑒_𝑠𝑖𝑧𝑒

Here 𝑋𝑖 is ith input index starting from 0th location to nth while 𝑃𝑗 is the list of prime numbers starting
from j=0 means 1st prime number and so on. i.e., 2, 3, 5, 7, 11 …
e.g. 295 ➔ 2*2 + 9*3 + 5*5 = 56 % 7 = 0

Insert 295 Insert 59 Insert 30

0 295 0 295 0 295


1 1 1
2 2 59 2 59
3 3 3
4 4 4
5 5 5
6 6 6 30
Insert 952 Insert 933 Insert 851

0 295 0 295 0 295


1 952 1 952 1 952
2 59 2 59 2 59
3 3 933 3 933
4 4 4 851
5 5 5
6 30 6 30 6 30
Delete 952 Delete 933 Delete 30

0 295 0 295 0 295


1 933 1 851 1 851
2 59 2 59 2 59
3 851 3 3
4 4 4
5 5 5
6 30 6 30 6

FAST School of Computing Page 3


Question 3: Given the following binary tree [5 Marks] [CLO1]

List the nodes in the following orders:

Inorder : B D A G J E C H F I

Preorder : A B D C E G J F H I

Postorder : D B J G E H I F C A

FAST School of Computing Page 4


Question 4: Graph [3+2+10 Marks] [CLO 1, CLO 3]

Part 1: What storage mechanism you will


use to store the given scenario in terms of
a graph. Provide a reason for your choice.

a) Adjacency Matrix
b) Adjacency List.

Part 2: Which Graphing technique you


will use to find cheese for our mouse.

DFS As Branching factor is low and


target is in depth
Part 3: Apply your selected algorithm and
show the path from mouse to cheese in a given grid.

Note: Black boxes are hurdles while all the other cells with alphabets are nodes. The mouse has direct links to the
“A” node and “I” likewise all the other nodes in the grid are linked via a bidirectional graph. But nodes in diagonal
direction aren’t adjacent e.g. A & C don’t have any link. Just vertical and horizontally linked nodes are connected
in our given grid scenario.

DFS:
MOUSE I J K L M N CHEESE

OR

MOUSE A B C D E F G H CHEESE

Approach 1 Approach 2

Cheese Cheese
N H
M G
L F
K E
J D
I C
Mouse B
A
Mouse

FAST School of Computing Page 5


Question 5: Convert any unsorted array in ascending sorted order with time complexity no more than O ( n log n )

[20 Marks] [CLO 1, CLO 3]

Note: You have to use the heap data structure’s concept. You have to give the complete implementation of each
function. STL is not allowed.

void heapify(int arr[], int n, int i)


{
int largest = i; // Initialize largest as root
int l = 2 * i + 1; // left = 2*i + 1
int r = 2 * i + 2; // right = 2*i + 2

// If left child is larger than root


if (l < n && arr[l] > arr[largest])
largest = l;

// If right child is larger than largest so far


if (r < n && arr[r] > arr[largest])
largest = r;

// If largest is not root


if (largest != i) {
swap(arr[i], arr[largest]);

// Recursively heapify the affected sub-tree


heapify(arr, n, largest);
}
}

void heapSort(int arr[], int n)


{
// Build heap (rearrange array)
for (int i = n / 2 - 1; i >= 0; i--)
heapify(arr, n, i);

// One by one extract an element from heap


for (int i = n - 1; i >= 0; i--) {
// Move current root to end
swap(arr[0], arr[i]);

// call max heapify on the reduced heap


heapify(arr, i, 0);
}
}

FAST School of Computing Page 6


Question 6: Given a Binary Search Tree
(BST), convert it to a Binary Tree such that
every key of the original BST is changed to
key plus the sum of all greater keys in BST.
[20 Marks] [CLO1, CLO3]

Note Full credit for best / optimal solution in


terms of time and space efficiency. And you
are required to write recursive solutions
only for the given problem.

void Greater_BST(struct tNode* root)


{
int sum = 0;
struct sNode* st = NULL;
struct tNode* node = root;

while (!isEmpty(st) || node != NULL) {


// push all nodes up to (and including) this
// subtree's maximum on the stack

while (node != NULL) {


push(&st, node);
node = node->right;
}

node = pop(&st);

sum += node->data;
node->data = sum;

// all nodes with values between the current and its


// parent lie in the left subtree.
node = node->left;
}
}

FAST School of Computing Page 7

You might also like