Q1.
For the provided Binary Tree, list its Inorder, Postorder, Preorder, and Level-Order
traversal sequences.
Example Tree:
mathematica
CopyEdit
/\
B C
/\ \
D E F
Inorder (LNR): D B E A C F
Preorder (NLR): A B D E C F
Postorder (LRN): D E B F C A
Level-order: A B C D E F
Q2. Build Binary Tree from Inorder & Postorder
Inorder: c b d a f e
Postorder: c d b f e a
Steps:
1. Last in postorder (a) is root.
2. In inorder, elements left of a → c b d (left subtree), right → f e (right subtree)
3. Recursively apply the process.
Constructed Tree:
css
CopyEdit
/\
b e
/\ /
c df
Q3. Define Binary Tree & Complete Binary Tree with Example + Build Tree from Preorder &
Inorder
Binary Tree: A tree where each node has at most 2 children.
Example:
markdown
CopyEdit
/\
2 3
Complete Binary Tree: All levels are completely filled except possibly the last, which is filled
from left to right.
Example:
markdown
CopyEdit
/\
2 3
Preorder: GBQACKFPDERH
Inorder: QBKCFAGPEDHR
Constructed Tree (Only textual hint due to complexity):
• Root: G (first of preorder)
• Inorder left of G: QBKCFA → left subtree
• Inorder right of G: PEDHR → right subtree
• Recurse to form full tree.
Q4. Construct Binary Tree from Traversals
Preorder: ABDGHCEIF
Postorder: GDHBAEICF
Steps:
• Root: A
• Next in preorder: B → left subtree
• From postorder, GDHB → left subtree of A
• Recurse...
Resulting Tree:
mathematica
CopyEdit
/\
B C
/\ \
D H E
/ \
G I
Q5. Draw tree for expression
E = (2x + y) * (5a - b)^3
Expression Tree:
markdown
CopyEdit
/\
+ ^
/\ /\
2x y - 3
/\
5a b
Q6. Recursive & Iterative Inorder Traversal
Recursive Inorder:
java
CopyEdit
void inorder(Node root) {
if (root != null) {
inorder(root.left);
System.out.print(root.data + " ");
inorder(root.right);
Iterative Inorder:
java
CopyEdit
void inorderIterative(Node root) {
Stack<Node> stack = new Stack<>();
Node current = root;
while (current != null || !stack.isEmpty()) {
while (current != null) {
stack.push(current);
current = current.left;
current = stack.pop();
System.out.print(current.data + " ");
current = current.right;
Time Complexity: Both are O(n), where n is number of nodes.
Q7. Complete Binary Tree with 27 nodes
• Height (h) = ⌊log₂(27)⌋ = 4
• Levels: 0 to 4
• Max nodes at level 4 = 2⁴ = 16
• Nodes at level 4 (lowest) = 27 - nodes in levels 0 to 3
→ Levels 0-3 = 1+2+4+8 = 15
→ So, nodes at lowest level = 27 - 15 = 12
Height: 4
Nodes at lowest level: 12
Q8. Define Complete Binary Tree and Full Binary Tree
Complete Binary Tree: All levels full except possibly last, filled left to right.
Full Binary Tree: Every node has 0 or 2 children.
Example Complete:
markdown
CopyEdit
/\
2 3
/\
4 5
Example Full:
markdown
CopyEdit
/\
2 3
Q9. Bubble Sort Working
Bubble Sort compares adjacent elements and swaps if needed, pushing larger elements to the
end in each pass.
Example (Array: 5 1 4 2):
• Pass 1: [1 4 2 5]
• Pass 2: [1 2 4 5]
• Pass 3: [1 2 4 5] (sorted)
Time Complexity:
• Best: O(n) (if optimized)
• Worst: O(n²)
Q10. Define Minimum Spanning Tree (MST) & Application
Minimum Spanning Tree (MST): A subset of edges connecting all vertices with minimum
total edge weight, without cycles.
Application:
• Network Design (like laying cables, road systems)
• Example: Connecting cities with minimal road cost.
Q11. Explain the working principle of the Binary Search algorithm
Binary Search is a divide-and-conquer algorithm used on sorted arrays.
1. Find the middle element.
2. If it's the target, return it.
3. If the target is smaller, search in the left half.
4. If larger, search in the right half.
5. Repeat until found or range is empty.
Time Complexity: O(log n)
Example (Search 41 in [3, 6, 8, 11, 18, 20, 41, 71, 90])
• mid = 20 → go right → mid = 41 → found
Q12. Compare Adjacency Matrix vs Adjacency List
Feature Adjacency Matrix Adjacency List
Storage O(V²) O(V + E)
Edge existence check O(1) O(V)
Space Efficient For dense graphs For sparse graphs
Implementation 2D array Array of lists or linked list
Feature Adjacency Matrix Adjacency List
Example matrix[i][j] = 1 if edge exists list[i] contains neighbors of node i
Q13. Count of distinct binary trees with 4 nodes
Formula: For n nodes, no. of distinct binary trees = Catalan Number Cₙ
Catalan Number (Cn) = (2n)! / [(n + 1)! * n!]
So for n = 4:
C₄ = (2×4)! / [(4+1)! × 4!] = 40320 / (120 × 24) = 14
Answer: 14 distinct binary trees
Q14. Minimum swaps to convert into max heap
Array: 90, 20, 41, 18, 11, 3, 6, 8, 12, 7, 71, 99
Max-heap: Parent ≥ Children.
Steps:
1. Build max heap using heapify (bottom-up approach).
2. Count swaps during heapification.
Minimum swaps = 6 (by heapifying bottom-up)
Q15. Difference: Complete Graph vs Connected Graph & Graph vs Tree
Feature Complete Graph Connected Graph
Definition Every node connected to all others Path exists between every pair
Edge Count n(n-1)/2 edges At least (n−1) edges
Graph vs Tree:
Feature Graph Tree
Cycles May have cycles No cycles
Feature Graph Tree
Edges ≥ n−1 Exactly n−1 edges
Root Not necessary One root node
Example Network of cities Family tree, file directory
Q16. Represent Binary Tree using Array
Formula:
• Root at index 0
• Left child at 2i+1
• Right child at 2i+2
Example Tree:
css
CopyEdit
/\
B C
Array Representation:
[A, B, C, D, _, _, _]
Index: 0 1 2 3
Q17. Max comparisons in BST search
Worst Case: When tree is skewed (like a linked list).
Comparisons = height of BST = n (in worst case)
For balanced BST, comparisons = log₂(n)
Example:
For n = 15 → Max comparisons = 4 (balanced), 15 (skewed)
Q18. Define Connected and Strongly Connected Graph
Connected Graph (Undirected): There is a path between every pair of vertices.
Strongly Connected Graph (Directed): There is a directed path from every vertex to every
other.
Example:
• Connected: Any tree
• Strongly Connected: A → B → C → A
Q19. Ways of Graph Representation in Memory
1. Adjacency Matrix
• 2D array of size V×V
• matrix[i][j] = 1 if edge exists
2. Adjacency List
• Array of linked lists
• list[i] = all neighbors of vertex i
3. Edge List
• List of edges: (u, v)
Given Graph:
mathematica
CopyEdit
Vertices: A, B, C, D
Edges: A-B, A-C, B-D
• Adjacency Matrix:
css
CopyEdit
ABCD
A[0110]
B[1001]
C[1000]
D[0100]
• Adjacency List:
css
CopyEdit
A→B→C
B→A→D
C→A
D→B
• Edge List:
[(A,B), (A,C), (B,D)]
Q20. Threaded Binary Tree + Memory Representation
Threaded Binary Tree:
Used to make inorder traversal faster without recursion or stack by replacing null pointers with
"threads" to inorder predecessor/successor.
Types:
• Left Threaded
• Right Threaded
• Fully Threaded
Example:
For Tree:
markdown
CopyEdit
10
/ \
5 15
• Normal null pointers in 5's left and right will be replaced:
o Left thread → inorder predecessor (NULL)
o Right thread → inorder successor (10)
Memory Representation:
Each node stores:
• Data
• Left pointer
• Right pointer
• LeftThread (boolean)
• RightThread (boolean)
Q21. Prove that a full binary tree of height 'h' has a maximum of (2^(h+1) - 1) nodes
Proof by induction:
Base case:
Height h = 0 → Only root node → nodes = 1
→ Formula: 2^(0+1) - 1 = 2 - 1 = 1
Inductive step:
Assume for height h, max nodes = 2^(h+1) - 1
At height h+1:
• Left subtree = 2^(h+1) - 1
• Right subtree = 2^(h+1) - 1
• Add root = 1
Total nodes = 2^(h+2) - 1
Hence, proved.
Q22. Difference: Binary Search vs Linear Search + Apply Binary Search
Feature Linear Search Binary Search
Data Requirement Unsorted/Sorted Only Sorted
Time Complexity O(n) O(log n)
Method Scan one-by-one Divide and conquer
Array: 11, 22, 30, 33, 40, 44, 55, 60, 66, 77, 80, 88, 99
Find: 40
1. mid = 6 → 55
2. go left → mid = 2 → 30
3. go right → mid = 4 → 40 found
Time Complexity:
• Best: O(1)
• Worst: O(log n)
Q23. Define BST, AVL Tree, B-Tree + Differences with Examples
BST: Binary Search Tree → left < root < right
Example:
markdown
CopyEdit
10
/ \
5 15
AVL Tree: Balanced BST → Difference in height (balance factor) ≤ 1
Example: Same as BST but keeps rotating to stay balanced
B-Tree: Generalized search tree for disk storage (multi-level indexing)
• Each node can have more than 2 children
• Used in DBMS, file systems
Feature BST AVL Tree B-Tree
Balance No Yes Yes
Nodes/Level 2 max 2 max More than 2
Used in RAM searches Memory Disk-based storage
Q24. Short notes: Hashing Techniques, Garbage Collection
Hashing Techniques:
Used to map keys to values using hash functions.
Techniques:
• Chaining: Use linked list for collisions
• Linear Probing: Next empty slot
• Quadratic Probing: i² steps
• Double Hashing: Use second hash
Garbage Collection:
Automatic memory management → identifies and deletes unused objects.
Techniques:
• Mark and Sweep
• Reference Counting
• Generational GC (Java)
Q25. Define Indexing & Primary vs Secondary Indices
Indexing: Technique to speed up record retrieval using pointers/index files.
Primary Index:
• Based on primary key
• Sorted file
• Example: Student ID → Index for Student table
Secondary Index:
• Based on non-primary attribute
• Not necessarily sorted
• Example: Index on “Department” field
Feature Primary Index Secondary Index
Uniqueness Unique Not necessarily unique
Attribute Primary Key Non-key attribute
File Order Sorted Unsorted
Q26. C Function & Algorithm to Search in BST
Algorithm:
1. Start at root
2. If key == root → found
3. If key < root → go left
4. Else → go right
5. Repeat until found or NULL
C Code:
CopyEdit
struct Node {
int data;
struct Node* left;
struct Node* right;
};
struct Node* search(struct Node* root, int key) {
if (root == NULL || root->data == key)
return root;
if (key < root->data)
return search(root->left, key);
else
return search(root->right, key);
Q27. Use Merge Sort to sort: 15, 10, 5, 20, 25, 30, 40, 35
Steps:
1. Divide array:
[15,10,5,20] and [25,30,40,35]
→ [15,10] [5,20] [25,30] [40,35]
2. Merge step-by-step:
• [10,15], [5,20] → [5,10,15,20]
• [25,30], [35,40] → [25,30,35,40]
• Final merge:
[5,10,15,20,25,30,35,40]
Sorted Array: 5, 10, 15, 20, 25, 30, 35, 40
Q28. Graph Traversal Techniques + Applications
1. DFS (Depth First Search):
Goes deep before backtracking
• Application: Solving puzzles, topological sort, cycle detection
2. BFS (Breadth First Search):
Level-wise traversal
• Application: Shortest path in unweighted graph, social networks
3. Dijkstra/ A etc*:
Weighted graphs for shortest paths
Real-world examples:
• Google Maps → Dijkstra
• Facebook friend suggestions → BFS
• Compilers → DFS in syntax tree
Q29. Define AVL Tree + Rotations + Construct with values 15 to 1
AVL Tree: Self-balancing BST with balance factor ∈ {-1, 0, 1}
Rotations:
• LL → Right Rotation
• RR → Left Rotation
• LR → Left + Right
• RL → Right + Left
Inserting 15 to 1 (descending):
• Causes LL imbalance → multiple Right Rotations
• Final AVL Tree is balanced (almost like a full tree)
Result (Balanced AVL Tree):
markdown
CopyEdit
/ \
4 12
/\ / \
2 6 10 14
/\/\/\/\
1 3 5 7 9 11 13 15
Q30. Build Binary Tree from Inorder & Postorder
Inorder: B, I, D, A, C, G, E, H, F
Postorder: I, D, B, G, C, H, F, E, A
1. Last of postorder: A → root
2. In inorder, left of A → B I D; right → C G E H F
3. Recursively build subtrees
Constructed Tree:
mathematica
CopyEdit
/ \
B E
\ /\
DC F
/ \ /
I GH
Q31. Describe Min Heap Sort with Example + Heap Insertion & Deletion Algorithm
Min Heap Sort:
A min heap is a binary tree where each parent is smaller than its children.
To sort using min-heap:
1. Build Min Heap
2. Remove root (min), place at end
3. Heapify again
4. Repeat
Example:
Input: [4, 10, 3, 5, 1]
Heap: [1, 4, 3, 10, 5]
Sorted (ascending): [1, 3, 4, 5, 10]
Heap Insertion (Min Heap):
plaintext
CopyEdit
1. Add new element at end
2. Compare with parent
3. Swap if smaller
4. Repeat until heap property is restored
Heap Deletion (Remove Min):
plaintext
CopyEdit
1. Remove root
2. Replace with last element
3. Heapify down: compare with smaller child
4. Swap and repeat
Q32. What is the maximum height of an AVL tree with 7 nodes?
For AVL trees, max height h grows slower than normal trees.
Using AVL tree height pattern:
• For 7 nodes, maximum height = 4
Example:
markdown
CopyEdit
/\
2 6
/\ \
1 3 7
Q33. Key Differences: Internal vs External Sorting
Feature Internal Sorting External Sorting
Data size Fits in main memory Too large, uses disk/secondary
Speed Faster Slower
Examples Quick Sort, Merge Sort Multiway Merge, Polyphase Sort
Use case Small datasets Very large datasets (GBs, TBs)
Q34. Insertion in B-Tree + Applications
B-tree: Self-balancing tree for disk-based indexing
Insertion Steps:
1. Insert in correct leaf node
2. If node overflows (more than order - 1 keys), split it
3. Promote middle key to parent
4. Repeat if needed
Applications:
• File systems
• Databases (MySQL, PostgreSQL)
• Indexing large datasets
Example: Inserting into B-tree of order 3:
Insert: 10, 20, 30 → split when 3 keys → root becomes 20
markdown
CopyEdit
20
/ \
10 30
Q35. Prove: Binary Tree with n nodes has n-1 edges
Proof:
• Every node except the root has one incoming edge
• So total edges = n - 1
Example:
Tree with 3 nodes:
css
CopyEdit
/\
B C
→ 3 nodes, 2 edges
Q36. Bubble Sort Pass on: 7,6,5,3,8,2,9,1
Pass 1:
• 7 > 6 → swap → [6,7,...]
• 7 > 5 → swap → [6,5,7,...]
• 7 > 3 → swap → [6,5,3,7,...]
• 7 < 8 → OK
• 8 > 2 → swap
• 8 < 9 → OK
• 9 > 1 → swap
Result after 1 pass:
[6, 5, 3, 7, 2, 8, 1, 9]
Optimization:
• Use a flag to stop early if no swaps
• Reduce comparisons by 1 in each pass
Q37. Draw binary tree for algebraic expression
Expression: [a + (b - c)] * [(d - e) / (f + g - h)]
Expression Tree:
markdown
CopyEdit
/ \
+ /
/ \ / \
a - - -
/\ /\ /\
b cd ef +
/\
g h
Q38. BST Insertion of Keys + Deletion
Keys to insert:
16, 9, 17, 11, 3, 12, 8, 20, 22, 23, 13, 18, 14, 10, 1, 2, 24, 25, 26, 5
After insertion, BST (simplified structure):
markdown
CopyEdit
16
/ \
9 17
/\ \
3 11 20
/\ \ \
1 5 12 22
\ \ \
2 13 23
\ \
14 24
25
26
10
After deleting 11, 20, and 24, BST adjusts:
• 11 → replaced by 12
• 20 → replaced by 22
• 24 → replaced by 25
Q39. Define B-Tree + Applications + B-Tree Order 4 Insertion
B-Tree: Balanced search tree for large data blocks
Order 4: Each node can have 3 keys, 4 children
Inserting: Z, U, A, I, W, L, P, X, C, J, D, M, T, B, Q, E, H, S, K, N, R, G, Y, F, O, V
Due to complexity, only the first few steps shown:
• Insert: A, I, U → [A, I, U] (sorted)
• Add W → overflow → split → I to root
Now:
css
CopyEdit
/\
[A] [U, W]
Applications:
• Filesystems (NTFS, HFS+)
• Databases
• Indexing in storage
Q40. Discuss Prim’s Algorithm + Construct MST
Prim’s Algorithm (Greedy):
1. Start from any node
2. Select smallest weight edge connecting to a new node
3. Repeat until all nodes included
Steps:
• Initialize MST with any node (say A)
• Choose min edge connecting to A
• Add next min edge to new node
• Avoid cycles
MST Applications:
• Network cable layout
• Road or pipeline planning
• Electrical grid connection
To construct MST, please provide the graph diagram or adjacency matrix.
Q41. How is a node deleted in a Binary Search Tree?
BST Deletion Cases:
1. Leaf Node: Simply delete it.
2. One Child: Replace node with its child.
3. Two Children:
o Find inorder successor (min in right subtree) or predecessor.
o Replace node with it.
o Delete that successor.
Example:
Deleting 15 from BST:
markdown
CopyEdit
10
/ \
5 15
/\
12 20
→ Replace 15 with 20 (inorder successor), delete 20
Q42. Min and Max keys in a B-tree (t = 3)
Minimum degree (t) = 3
• Min keys (non-root): t - 1 = 2
• Max keys: 2t - 1 = 5
So, every non-root node has:
• Min: 2 keys
• Max: 5 keys
Q43. Define AVL Tree + All Operations + Construct Tree with Nodes: B, C, G, E, F, D, A
AVL Tree: A self-balancing BST, balance factor ∈ {−1, 0, 1}
Operations:
• Insertion: Insert like BST, then rotate if needed
• Deletion: Delete as BST, rebalance if unbalanced
• Rotations:
o LL → Right rotate
o RR → Left rotate
o LR → Left then right
o RL → Right then left
Constructing AVL with B, C, G, E, F, D, A
Insert order:
1. B → root
2. C → right
3. G → right → RR imbalance → rotate left
4. E → left of G
5. F → right of E → RL imbalance → rotate right-left
6. D → left of E
7. A → left of B → Tree remains balanced
Final structure (balanced):
mathematica
CopyEdit
/ \
B G
/\ /
A DF
Q44. Prim’s vs Kruskal’s Algorithm + Find MST using Kruskal
Prim’s Algorithm:
• Grow MST from one node
• Choose min edge connecting MST to outside
Kruskal’s Algorithm:
• Sort edges by weight
• Add smallest edge without forming a cycle
Steps in Kruskal:
1. Sort all edges
2. Pick smallest edge, check if it forms a cycle
3. Add if no cycle, else skip
4. Repeat till n−1 edges added
For MST construction, please share the graph or edge list.
Q45. What is Merge Sort? Sort: 38, 81, 22, 48, 13, 69, 93, 14, 45, 58, 79, 72
Merge Sort = Divide & Conquer
1. Divide array in halves
2. Sort each half
3. Merge
Given:
[38, 81, 22, 48, 13, 69, 93, 14, 45, 58, 79, 72]
Steps:
• Divide → [38,81,22,48,13,69] and [93,14,45,58,79,72]
• Subdivide → sort each
• Merge each level
Final Sorted Array:
13, 14, 22, 38, 45, 48, 58, 69, 72, 79, 81, 93
Q46. Short Notes
a. Hash Functions: Maps keys to indices
→ h(k) = k mod m
Techniques: Chaining, probing, double hashing
b. Binary Tree: At most 2 children per node
c. Complete BT: All levels full except last (left-filled)
d. Strictly Binary Tree: Every node has 0 or 2 children
Extended BT: Includes external (dummy) nodes
e. Almost Binary Tree: Nearly complete
NBT (Nearly Binary Tree): Irregular, close to binary
f. Threaded BT: Null pointers replaced by threads to inorder predecessor/successor
Q47. Hashing with Linear and Quadratic Probing
Keys: 76, 26, 37, 59, 21, 65, 88
Table size m = 11
Primary Hash: h’(k) = k mod 11
Linear Probing:
76 → 10
26 → 4
37 → 4 (occupied) → try 5
59 → 4 → 6
21 → 10 → 0
65 → 10 → 1
88 → 0 → 2
Final Table (Linear Probing):
CopyEdit
0 → 21
1 → 65
2 → 88
4 → 26
5 → 37
6 → 59
10 → 76
Quadratic Probing (c1=1, c2=3):
Use formula: (h(k) + i + 3i²) mod 11
Q48. Time Complexity of Sorting + C Code for Merge Sort
Merge Sort Time Complexity:
• Worst: O(n log n)
• Best: O(n log n)
• Space: O(n)
C Function:
CopyEdit
void merge(int arr[], int l, int m, int r) {
int i, j, k;
int n1 = m - l + 1, n2 = r - m;
int L[n1], R[n2];
for (i = 0; i < n1; i++) L[i] = arr[l + i];
for (j = 0; j < n2; j++) R[j] = arr[m + 1+ j];
i = 0; j = 0; k = l;
while (i < n1 && j < n2)
arr[k++] = (L[i] <= R[j]) ? L[i++] : R[j++];
while (i < n1) arr[k++] = L[i++];
while (j < n2) arr[k++] = R[j++];
void mergeSort(int arr[], int l, int r) {
if (l < r) {
int m = l + (r - l)/2;
mergeSort(arr, l, m);
mergeSort(arr, m+1, r);
merge(arr, l, m, r);
Q49. C Function for Insertion Sort + Worst Case Time Complexity
Worst Case Time Complexity: O(n²)
C Code:
CopyEdit
void insertionSort(int arr[], int n) {
for (int i = 1; i < n; i++) {
int key = arr[i], j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j+1] = arr[j];
j--;
arr[j+1] = key;
Q50. C Program for Binary Search
C Code:
CopyEdit
int binarySearch(int arr[], int n, int key) {
int low = 0, high = n - 1;
while (low <= high) {
int mid = (low + high)/2;
if (arr[mid] == key) return mid;
else if (arr[mid] < key) low = mid + 1;
else high = mid - 1;
return -1;
Q51. What is Selection Sort? Write a C program for it.
Selection Sort: A comparison-based algorithm that selects the smallest element and swaps
it with the current position.
Time Complexity:
• Best, Worst, Average: O(n²)
• In-place, No extra space required.
C Program:
CopyEdit
#include <stdio.h>
void selectionSort(int arr[], int n) {
int i, j, minIdx, temp;
for (i = 0; i < n - 1; i++) {
minIdx = i;
for (j = i + 1; j < n; j++)
if (arr[j] < arr[minIdx])
minIdx = j;
temp = arr[minIdx];
arr[minIdx] = arr[i];
arr[i] = temp;
int main() {
int arr[] = {64, 25, 12, 22, 11}, n = 5;
selectionSort(arr, n);
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
return 0;
Q52. Construct AVL Tree with: 35, 36, 80, 85, 67, 89, 25, 16, 10, 14, 14
AVL Tree Construction:
Insert elements one by one and rebalance using rotations.
• Insert 35, 36 → Right
• Insert 80 → RR imbalance at 35 → Left Rotate
• Insert 85 → RR at 36 → Left Rotate
• Insert 67 → RL at 80 → Right-Left Rotate
• Continue with insertions and balance after each
Final structure (balanced):
markdown
CopyEdit
36
/ \
25 80
/ / \
16 67 85
/ \ \
10 14 89
14
Note: Duplicate values like 14 may be handled as right child or ignored depending on
implementation.
Q53. Algorithm to find Second Best Minimum Spanning Tree
Second Best MST:
1. Compute the MST using Kruskal or Prim.
2. For each non-MST edge:
o Add it to MST → forms a cycle
o Remove the highest weight edge in that cycle
o Compute new total cost
3. Keep the minimum of these values (excluding original MST)
Execute on graph:
Please provide the graph structure or edge list for execution.
Q54. Quick Sort Algorithm + Sort: 2, 8, 7, 1, 3, 6, 4
Quick Sort Algorithm:
plaintext
CopyEdit
1. Choose pivot
2. Partition: elements < pivot to left, > pivot to right
3. Recursively sort subarrays
Sorting:
Initial: [2, 8, 7, 1, 3, 6, 4]
Pivot = 4 → Partition → [2, 1, 3] [4] [8, 7, 6]
Sort recursively:
• Left: [1, 2, 3]
• Right: [6, 7, 8]
Final sorted array: [1, 2, 3, 4, 6, 7, 8]
Q55. Merge Sort + Sort: 15,28,32,14,11,32,22,1
Merge Sort: Divide & Conquer
Time: O(n log n), Space: O(n)
Given Array: [15,28,32,14,11,32,22,1]
Step-by-step:
1. Divide: [15,28,32,14] & [11,32,22,1]
2. Sort subarrays:
o [15,28,32,14] → [15,28], [32,14] → [15,28], [14,32] → [14,15,28,32]
o [11,32,22,1] → [11,32], [22,1] → [11,32], [1,22] → [1,11,22,32]
3. Merge:
[14,15,28,32] + [1,11,22,32] → [1,11,14,15,22,28,32,32]
Q56. Implement BST in C (Create, Delete, Display)
BST Node Structure:
CopyEdit
struct Node {
int data;
struct Node* left;
struct Node* right;
};
a. Create BST:
CopyEdit
struct Node* insert(struct Node* root, int key) {
if (root == NULL) {
struct Node* node = malloc(sizeof(struct Node));
node->data = key; node->left = node->right = NULL;
return node;
if (key < root->data) root->left = insert(root->left, key);
else if (key > root->data) root->right = insert(root->right, key);
return root;
b. Delete Node:
CopyEdit
struct Node* minValue(struct Node* node) {
while (node->left) node = node->left;
return node;
struct Node* deleteNode(struct Node* root, int key) {
if (!root) return root;
if (key < root->data) root->left = deleteNode(root->left, key);
else if (key > root->data) root->right = deleteNode(root->right, key);
else {
if (!root->left) return root->right;
else if (!root->right) return root->left;
struct Node* temp = minValue(root->right);
root->data = temp->data;
root->right = deleteNode(root->right, temp->data);
return root;
c. Inorder Display:
CopyEdit
void inorder(struct Node* root) {
if (root) {
inorder(root->left);
printf("%d ", root->data);
inorder(root->right);
Q57. For Undirected Graph:
Please provide the graph structure or adjacency matrix/list.
a. Adjacency List: For graph:
less
CopyEdit
A—B
| |
C—D
Adjacency List:
mathematica
CopyEdit
A: B, C
B: A, D
C: A, D
D: B, C
b. Kruskal’s MST:
1. Sort edges by weight
2. Add smallest edge, avoiding cycles
3. Continue till (n−1) edges added
For exact MST, share the graph or edge weights