PU_Key_Solutions
PU_Key_Solutions
Adjacency Matrix:
A 2D array of size V×VV×V (where VV is the number of vertices) is used to represent the
graph. The entry matrix[i][j] is 1 (or the weight of the edge) if there is an edge between
vertex ii and vertex jj, otherwise 0 (or infinity for weighted graphs).
Adjacency List
An array of lists (or linked lists) is used, where each element in the array represents a vertex,
and the list associated with it contains all the vertices adjacent to it.
● Move the top n−1n-1n−1 disks from peg A to peg B using peg C as an auxiliary peg.
● Move the largest disk (nth disk) from peg A to peg C.
● Move the n−1n-1n−1 disks from peg B to peg C using peg A as an auxiliary peg.
TOH(n, A, C, B)
1. If n == 1:
Move disk from A to C
2. Else:
TOH(n-1, A, B, C) // Move n-1 disks from A to B using C
Move disk n from A to C
TOH(n-1, B, C, A) // Move n-1 disks from B to C using A
Q5: Define tail recursion with a suitable example.
Tail recursion is a type of recursion where the recursive call is the last operation before
returning the result. It allows for tail call optimization (TCO), reducing memory usage(stack
space).
Q6: Find the height of tree, if there are 1000000 nodes in a complete binary tree.
Formula:
NOTE: HOW ?
A Threaded Binary Tree is a binary tree where null pointers are replaced with threads that
point to the inorder predecessor or successor, enabling efficient traversal without recursion
or a stack.
● Space Optimization – Reduces memory usage by utilizing null pointers for storing
threads.
● Efficient Traversal – Speeds up inorder, preorder, and postorder traversals without
extra memory for recursion or stack.
Section B
Qa:
A networking company uses a compression technique to encode the message before
transmitting over the network. Suppose the piece of message (each character
occupies 7 bits) written in italic letter — When you are on the left you are on the right
when you are on the right, you are on the wrong.
Solve the following questions based on the above problem:
i. Construct Huffman Tree
ii. Decode the message following message
10111101010111111111100.
iii. Calculate the percentage Of space saved in the message after compression
i).
Note: You can draw Huffman tree as your wish and encoding code also can vary. But
Total bit size of message should be same(334 bits)
ii). Decode: 10111 101 010 111111 111100
● 10111 = "i"
● 101 = "n"
● 010 = "Space"
● 111111 = "u"
● 111100 = "r"
iii). Compression Ratio: 334/651 bits = 51.3% (If 8 bits: 334/744 = 44.89%)
Qb.(i)
Illustrate all functions or algorithms for circular queue operations, if it is implemented
using an array.
ii).
ABD+*E/FGHK/+*-
A B D + * E / F G H K / + * -
C.(i) Hashing
Total Collisions: 8
ii).
Selection Sort is inefficient for large datasets due to its O(n²) complexity.
Q4:a)
b).
Preorder (Root → Left → Right)
Inorder (Left → Root → Right)
Postorder (Left → Right → Root)
Q5). (a)
b). + A height-balanced binary tree is one where the height of the left and right subtrees
of every node differ by no more than one.
+ Balancing ensures that the tree remains efficient for operations like insertion,
deletion, and lookup.
Note : Always only consider 3 nodes and perform rotation, once rotation done link the
remaining nodes
Rough Works
10
b)
Q7(a)
C Program for Addition of Two Polynomials Using a Singly Linked List
void addPolynomials(struct Node* poly1, struct Node* poly2) {
struct Node* result = NULL;
struct Node **temp = &result;
while (poly1) {
*temp = createNode(poly1->coeff, poly1->pow);
poly1 = poly1->next;
temp = &((*temp)->next);
}
while (poly2) {
*temp = createNode(poly2->coeff, poly2->pow);
poly2 = poly2->next;
temp = &((*temp)->next);
}
Q7(b)