0% found this document useful (0 votes)
3 views

PU_Key_Solutions

The document contains various computer science questions covering topics such as stack evaluation of postfix expressions, graph representation techniques, expression trees, algorithms like Tower of Hanoi, tail recursion, threaded binary trees, and Huffman coding. It also discusses sorting algorithms, circular queue operations, and polynomial addition using linked lists. Additionally, it touches on tree traversal methods and height-balanced binary trees.

Uploaded by

Archit
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)
3 views

PU_Key_Solutions

The document contains various computer science questions covering topics such as stack evaluation of postfix expressions, graph representation techniques, expression trees, algorithms like Tower of Hanoi, tail recursion, threaded binary trees, and Huffman coding. It also discusses sorting algorithms, circular queue operations, and polynomial addition using linked lists. Additionally, it touches on tree traversal methods and height-balanced binary trees.

Uploaded by

Archit
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/ 14

SECTION A

Q1: Use stack to evaluate the following postfix expression: 2 3 9 * + 2 3 ^ - 6 2 / +.​

Q2: Describe various types Of graph representations techniques.

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.

Q3: Construct an expression tree for algebraic expression: (a - b) / ((c * d) + e).

Q4: Illustrate the algorithm of the Tower of Hanoi.

●​ 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).

int factorial(int n, int acc = 1) {


​ if (n == 0) return acc;
​ return factorial(n - 1, n * acc); ​ ​ // Tail recursive call
}

Q6: Find the height of tree, if there are 1000000 nodes in a complete binary tree.
Formula:

NOTE: HOW ?

Q7: Outline the importance of a threaded binary tree.

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"

Putting it together: "in ur"​

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​

Hash Table: {98, 66, 37, 38, 72, 11, 48}

Total Collisions: 8
ii).

Q3(i) Heap Sort


ii). Selection Sort - 100 Integer, Worst case Analysis​
void selectionSort(int arr[], int n) {
​ int i, j, min_idx, temp;
​ for (i = 0; i < n - 1; i++) {
​ min_idx = i;
​ for (j = i + 1; j < n; j++) {
​ if (arr[j] < arr[min_idx])
​ min_idx = j;
​ }
​ // Swap the found minimum element with the first element
​ temp = arr[min_idx];
​ arr[min_idx] = arr[i];
​ arr[i] = temp;
​ }
}
int main() {
​ int arr[SIZE];
​ srand(time(0)); // Seed for random number generation
​ // Generating 100 random integers between 1 and 1000
​ for (int i = 0; i < SIZE; i++)
​ arr[i] = rand() % 1000 + 1;

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

Q6) (a) Prims’​

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 && poly2) {


​ if (poly1->pow > poly2->pow) {
​ *temp = createNode(poly1->coeff, poly1->pow);
​ poly1 = poly1->next;
​ } else if (poly1->pow < poly2->pow) {
​ *temp = createNode(poly2->coeff, poly2->pow);
​ poly2 = poly2->next;
​ } else {
​ *temp = createNode(poly1->coeff + poly2->coeff, poly1->pow);
​ poly1 = poly1->next;
​ poly2 = poly2->next;
​ }
​ temp = &((*temp)->next);
​ }

​ 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)​

You might also like