Data Strcuture 2023 Answer Paper
Data Strcuture 2023 Answer Paper
Both types of fragmentation can lead to inefficient memory usage, slowing down
programs or causing them to run out of usable memory. This is a concern when
analyzing data structures that dynamically allocate memory, such as linked lists,
trees, or hash tables. Proper memory management strategies are needed to minimize
fragmentation and optimize performance.
Q2)
(a) Design an algorithm to implement circular queue using an array.
Ans:-
1:-Initialize the Circular Queue:-
Create an array of fixed size.
Set front and rear to -1 (representing an empty queue).
Set capacity to the size of the array.
(b)Explain quick sort with example by giving its algorithm and comment on its
complexity
Ans:-Quick Sort is a highly efficient sorting algorithm that uses a divide-and-conquer
strategy to sort an array or list. It works by selecting a 'pivot' element from the array
and partitioning the other elements into two sub-arrays according to whether they
are less than or greater than the pivot. The sub-arrays are then sorted recursively.
Algorithm:-
Step 1: Initialize the Quick Sort
Input: An array A of n elements, with indices ranging from 0 to n-1.
Call the Function:
Start the Quick Sort by calling quicksort(A, 0, n-1).
Step 2: Define the Quick Sort Function
Base Condition:
Check if low < high. If not, return (the sub-array is already sorted).
Partitioning:
Call the partition(A, low, high) function to partition the array.
Store the returned index as pivot_index.
Recursive Calls:
Recursively call quicksort(A, low, pivot_index - 1) for the left sub-array.
Recursively call quicksort(A, pivot_index + 1, high) for the right sub-array.
Complexity Analysis:-
Q4)
(a) Write an algorithm to covert infix expression to postfix expression.
Ans:-Infix to Postfix Conversion: Simple Steps
Objective: Convert an infix expression (like A + B * C) to postfix notation (like A B C
* +).
Steps to Follow
1:-Initialize:
Create an empty stack for operators.
Create an empty list for the output (postfix expression).
4:-Pop Remaining Operators:After reading the entire expression, pop all remaining
operators from the stack to the output list.
Example Walkthrough
Infix Expression: A + B * C
Initialize:
output = []
stack = []
Process Tokens:
Read A: Add to output → output = [A]
Read +: Push to stack → stack = [+]
Read B: Add to output → output = [A, B]
Read *: Push to stack (higher precedence than +) → stack = [+, *]
Read C: Add to output → output = [A, B, C]
Separate chaining: This method involves making a linked list out of the slot where
the collision happened, then adding the new key to the list. Separate chaining is the
term used to describe how this connected list of slots resembles a chain. It is more
frequently utilized when we are unsure of the number of keys to add or remove.
Time complexity
Its worst-case complexity for searching is o(n).
Its worst-case complexity for deletion is o(n).
Linear probing: This involves doing a linear probe for the following slot when a
collision occurs and continuing to do so until an empty slot is discovered.
The worst time to search for an element in linear probing is O. The cache performs
best with linear probing, but clustering is a concern. This method’s key benefit is that
it is simple to calculate.
Quadratic probing: When a collision happens in this, we probe for the i2-nd slot in
the ith iteration, continuing to do so until an empty slot is discovered. In comparison
to linear probing, quadratic probing has a worse cache performance. Additionally,
clustering is less of a concern with quadratic probing.
Double hashing: In this, you employ a different hashing algorithm, and in the
ith iteration, you look for (i * hash 2(x)). The determination of two hash functions
requires more time. Although there is no clustering issue, the performance of the
cache is relatively poor when using double probing.
Q4 )
Kruskal's Algorithm:
Kruskal’s algorithm builds the MST by sorting all edges by their weights and adding
the smallest edges one by one, ensuring no cycles are formed. The steps are as follows:
Steps:
Pick the smallest edge and add it to the MST if it doesn't form a cycle.
At this point, adding any other edges would form a cycle, so we stop. The total cost
is:1+2+2+4+4=13
Prim’s Algorithm:
Prim's algorithm starts with a single vertex and grows the MST by adding the
smallest edge that connects a vertex inside the MST to a vertex outside the MST.
Steps:
1:-Start with vertex A and add the smallest edge connected to A.
Add edge (A-B) with weight 1 → MST: (A-B), total cost: 1.
2:-From vertices in the MST (A, B), choose the smallest edge connecting to a vertex
outside the MST.
Add edge (A-C) with weight 4 → MST: (A-B), (A-C), total cost: 5.
3:-From vertices in the MST (A, B, C), add the smallest edge.
Add edge (D-E) with weight 2 → MST: (A-B), (A-C), (D-E), total cost: 7.
4:-Continue adding the smallest edges until all vertices are connected.
Add edge (E-F) with weight 2 → MST: (A-B), (A-C), (D-E), (E-F), total cost: 9.
Add edge (D-F) with weight 4 → MST: (A-B), (A-C), (D-E), (E-F), (D-F), total cost: 13.
Minimum Spanning Tree (MST) using Prim's:
The MST consists of the edges: (A-B), (A-C), (D-E), (E-F), (D-F) with a total cost of 13.
(c) Define AVL Tree. Step by step construct a AVL tree for the following data 23, 12,
25, 01, 45,63, 27, 29 ,90,78,5,6,10
Q5)
B)
C)Game Tree
Definition:
A Game Tree is a conceptual representation of all possible moves in a game. Each
node represents a state of the game, and edges represent possible moves from one
state to another.
Properties:
Nodes: Each node represents a game state.
Edges: Each edge represents a player's move.
Leaves: Terminal nodes represent outcomes (win/loss/draw).
D)
E)B+-Tree
Definition:
A B+-Tree is a self-balancing tree data structure that maintains sorted data and
allows for efficient insertion, deletion, and search operations. It is an extension of B-
trees where all values are stored at the leaf nodes.
Properties:
Balanced: All leaf nodes are at the same level.
Multi-way: Each node can have multiple children.
Order: Typically defined by a parameter mmm (maximum children per node).
Leaf Nodes: Contains pointers to the actual data records.