Data Structures and Algorithms
Community
Data Structures and Algorithms
2079 Boards
Section A
Answer any two questions.
1
Why do we need to balance the binary search tree? Justify with an example. Create an AVL tree from the data 24, 12, 8, 15, 35, 30, 57, 40, 45, 78.
10
Balancing a Binary Search Tree (BST) is essential to maintain optimal performance for its operations.
Why Balance a Binary Search Tree?
- Maintain Logarithmic Time Complexity: For a balanced BST, operations like search, insertion, and deletion have an average and worst-case time complexity of O(log N), where N is the number of nodes. This efficiency stems from the tree's height being proportional to log N.
- Prevent Degeneration: Without balancing, a BST can degenerate into a skewed tree, resembling a linked list. This occurs when elements are inserted in a strictly increasing or decreasing order. In such cases, the tree's height becomes O(N), leading to O(N) time complexity for operations, which is significantly slower than O(log N) for large datasets.
Justification with an Example
Consider inserting the elements 1, 2, 3, 4, 5 into an empty BST in that order:
-
Unbalanced BST:
1 \ 2 \ 3 \ 4 \ 5To search for
5, it requires traversing all 5 nodes, resulting in O(N) comparisons. Similarly, inserting6would also be an O(N) operation. -
Balanced BST (e.g., AVL tree) for the same elements:
3 / \ 2 4 / \ 1 5To search for
5, it requires traversing 3 nodes (3 -> 4 -> 5), which is O(log N) for N=5 (log2(5) is approximately 2.32). This demonstrates the performance advantage of balancing.
AVL Tree Construction
Data: 24, 12, 8, 15, 35, 30, 57, 40, 45, 78
Balance Factor (BF) = Height(Left Subtree) - Height(Right Subtree)
-
Insert 24:
24BF: 24(0)
-
Insert 12:
24
/
12
```
BF: 24(1), 12(0)
- Insert 8:
24
/
12
/
8 Imbalance at 24 (BF=2). Path: 24 -> 12 -> 8 (LL imbalance). **Rotation:** Right Rotation at 24.
12
/
8 24
```
BF: 12(0), 8(0), 24(0)
-
Insert 15:
12 / \ 8 24 / 15BF: 12(0), 8(0), 24(-1), 15(0)
-
Insert 35:
12 / \ 8 24 / \ 15 35BF: 12(0), 8(0), 24(0), 15(0), 35(0)
-
Insert 30:
12 / \ 8 24 / \ 15 35 / 30BF: 12(0), 8(0), 24(1), 15(0), 35(-1), 30(0)
-
Insert 57:
12 / \ 8 24 / \ 15 35 / \ 30 57BF: 12(0), 8(0), 24(0), 15(0), 35(0), 30(0), 57(0)
-
Insert 40:
12 / \ 8 24 / \ 15 35 / \ 30 57 / 40Imbalance at 24 (BF=2). Path: 24 -> 35 -> 57 -> 40 (R-L imbalance at 24's right subtree root, 35).
Rotation:- Right Rotation on 57 (right child of 35). This transforms the RL case into an RR case at 35.
Subtree35 (L:30, R:57(L:40))becomes35 (L:30, R:40(R:57)). - Left Rotation on 35 (right child of 24).
Subtree24 (L:15, R:35(L:30, R:40(R:57)))becomes24 (L:15, R:40(L:35(L:30), R:57)).
Current Tree:
12 / \ 8 24 / \ 15 40 / \ 35 57 / 30BF: All nodes now have a balance factor of -1, 0, or 1.
- Right Rotation on 57 (right child of 35). This transforms the RL case into an RR case at 35.
-
Insert 45:
12 / \ 8 24 / \ 15 40 / \ 35 57 / / 30 45BFs (from bottom up): 45(0), 57(-1), 30(0), 35(-1), 40(0), 15(0), 24(0), 8(0), 12(0).
All nodes are balanced. No rotation needed. -
Insert 78:
12 / \ 8 24 / \ 15 40 / \ 35 57 / / \ 30 45 78BFs (from bottom up): 45(0), 78(0), 57(0), 30(0), 35(-1), 40(0), 15(0), 24(0), 8(0), 12(0).
All nodes are balanced. No rotation needed.
Final AVL Tree Structure
12
/ \
8 24
/ \
15 40
/ \
35 57
/ / \
30 45 78
2
How recursive algorithm uses stack to store intermediate results? Illustrate with an example. Convert the infix expression A+B-(CD/E+F)-GH into postfix expression using stack.
10
Connect with us on Discord to become a contributor.
3
How do you insert and delete a node at kth position of the doubly linked list? Describe the process of implementing stack and queue using linked list.
10
Connect with us on Discord to become a contributor.
Section B
Answer any eight questions.
4
Sort the numbers 82,73,12,39,26,88,2,9,60,41 using shell sort.
5
Connect with us on Discord to become a contributor.
5
Why do we need asymptotic notation? Describe about Big oh notation with its curve.
5
Need for Asymptotic Notation:
- Machine Independence: Provides a way to analyze algorithm efficiency independent of specific hardware, programming languages, or compilers.
- Scalability Prediction: Focuses on the growth rate of an algorithm's running time or space requirements as the input size (n) tends towards infinity, crucial for predicting performance on large datasets.
- Comparison of Algorithms: Allows objective comparison of algorithms by their fundamental growth rates, rather than specific execution times which can vary.
- Worst-Case Analysis: Often used to describe the worst-case performance, providing a guarantee on the maximum resources an algorithm might consume.
Big O Notation (O-notation):
Big O notation describes the upper bound of an algorithm's running time or space complexity. It indicates the longest amount of time an algorithm will take to complete (worst-case scenario) or the maximum space it will require.
- Definition: A function
f(n)isO(g(n))if there exist positive constantscandn₀such that0 ≤ f(n) ≤ c * g(n)for alln ≥ n₀. - Meaning:
f(n)grows no faster thang(n)for sufficiently largen. It provides a loose upper bound.
Big O Curve Description:
Imagine a graph where the x-axis represents the input size n and the y-axis represents time/operations.
- Actual Function (
f(n)): Represents the actual running time of the algorithm. This curve might fluctuate for smallern. - Upper Bound Function (
c * g(n)): Represents the bounding function scaled by a constantc. - Relationship: For all input sizes
ngreater than or equal to some thresholdn₀, the curve of the actual functionf(n)will always lie below or on the curve of the scaled bounding functionc * g(n). This signifies thatc * g(n)provides an upper limit on the growth off(n).
6
Define queue. Explain about enqueue and dequeue operation in circular queue.
5
A queue is a linear data structure that follows the First-In, First-Out (FIFO) principle, where elements are added at one end (rear) and removed from the other end (front).
A circular queue is a linear queue in which the last element points back to the first element, forming a circle. This arrangement efficiently utilizes memory by allowing reuse of empty spaces.
Enqueue Operation (Adding an element)
- Check for Full Condition:
- A circular queue is full if
(rear + 1) % capacity == front. - If full, display "Queue Overflow" and exit.
- A circular queue is full if
- Add Element:
- If the queue is initially empty (
front == -1andrear == -1), setfront = 0. - Update
rear = (rear + 1) % capacity. - Place the new element at the
rearposition.
- If the queue is initially empty (
Dequeue Operation (Removing an element)
- Check for Empty Condition:
- A circular queue is empty if
front == -1. - If empty, display "Queue Underflow" and exit.
- A circular queue is empty if
- Remove Element:
- Store the element at the
frontposition as the element to be removed. - Handle Single Element Case: If
front == rear, it means the last element is being removed. Resetfront = -1andrear = -1. - General Case: Otherwise, update
front = (front + 1) % capacity. - Return the stored element.
- Store the element at the
7
Write a program to implement binary search.
5
Connect with us on Discord to become a contributor.
8
Find the MST of following graph using Prim’s algorithm.
5
Connect with us on Discord to become a contributor.
9
Assume you have to store the data {0,1,2,4,5,7} into a hash table of size 5, with hash function, h(x)=x%5. Apply linear probing and double hashing as collision resolution techniques.
5
Connect with us on Discord to become a contributor.
10
In which case the position of pivot element in quick sort always either in the last or the first position? Create a max heap from the numbers. {10,12,53,34,23,77,59,66,5,8}
5
Connect with us on Discord to become a contributor.
11
Evaluate the postfix expression 574-*8/4+ using stack.
5
Connect with us on Discord to become a contributor.
12
Write short noes on:
a. Priority Queue
b. Breadth First traversal of a graph
5
Connect with us on Discord to become a contributor.