paper -ds
paper -ds
🌀
a) iv) Merge sort
Best suited for linked lists due to non-contiguous memory and stable O(n log n) time.
🔁
b) ii) 2
A stack can be implemented using 2 queues.
c) iv) Dequeue
↔️ Double-ended queue allows insertion/deletion from both ends.
d) Find no. of binary tree with 3 nodes which when traversed in post
order gives the sequence A, B, C is _______.
i) 3 ii) 9
iii) 7 iv) 5
🌳
d) i) 3
3 distinct binary trees give postorder A, B, C.
c) What is Pivot?
c) Pivot:
An element chosen during Quick Sort to partition the array.
● Insertion
● Deletion
● Search
📈
e) Critical path:
The longest path in a project network diagram determining the shortest project duration.
—------------------------------------------------------------------------------------------------------------------------
Q2) Answer the following (Any Five) : [5 × 3 = 15]
Advantages:
int factorial(int n) {
if (n == 0) return 1;
int matrix[2][3] = {
{1, 2, 3},
{4, 5, 6}
};
● matrix[0][0] = 1, matrix[1][2] = 6
Uses:
● Matrix operations
A Sparse Matrix is a matrix in which most of the elements are zero. Storing all zero values
wastes memory.
P/Q – R * S + T.
List: 2, 6, 8, 2, 3, 9, 1, 4, 9
1. Divide:
[2, 6, 8, 2, 3, 9, 1, 4, 9]
-> [2] [6] [8] [2] [3] [9] [1] [4] [9]
2. Merge:
-> [1,2,2,3,4,6,8,9,9]
✅ Sorted List: 1, 2, 2, 3, 4, 6, 8, 9, 9
b) C Function to Search Element in Singly Linked List
struct Node {
int data;
};
if (head->data == key)
return 1; // Found
head = head->next;
1. Linear Search:
Definition: Linear Search is a straightforward search algorithm that checks each element in
a list sequentially until the desired element is found or the list ends.
Linear Search is simple and works on any list but is inefficient for large datasets.
Algorithm Steps:
4. If the end of the list is reached without finding the target, return a signal that indicates the
element is not present.
2. Binary Search:
Definition: Binary Search is an efficient searching algorithm that works on sorted arrays. It
divides the search interval in half repeatedly, checking the middle element each time.
Binary Search is much faster but requires the data to be sorted. When applicable, it is the
preferred method for searching due to its logarithmic time complexity.
Algorithm Steps:
1. Start with two pointers, low and high, representing the bounds of the search space.
o If the middle element equals the target, return the middle index.
o If the middle element is less than the target, narrow the search to the upper half by setting
low = mid + 1.
o If the middle element is greater than the target, narrow the search to the lower half by
setting high = mid - 1.
struct Node {
int data;
};
if (root == NULL) {
newNode->data = key;
return newNode;
else
return root;
}
e) What is Graph? Explain Its Types 📊
Definition:
A graph is a non-linear data structure consisting of nodes (vertices) and edges that
connect pairs of nodes.
Types of Graphs:
Array Representation:
#define SIZE 5
int queue[SIZE], front = -1, rear = -1;
if (rear == SIZE - 1)
printf("Queue Full\n");
else {
queue[++rear] = val;
void dequeue() {
printf("Queue Empty\n");
else
c) Write 'C' function to delete node from beginning and end in Doubly linked
list.
markdown
CopyEdit
11
/ \
7 15
/ / \
5 12 25
18
\
20
Primitive Operations:
● Push:
Inserts an element onto the top of the stack.
➤ Example: push(10)
● Pop:
Removes the element from the top of the stack.
➤ Example: pop() removes the most recently added item.
● Peek / Top:
Returns the top element of the stack without removing it.
➤ Example: peek() returns the current top value.
● isEmpty():
Checks if the stack is empty.
➤ Returns true if no elements are present.
● isFull():
Checks if the stack is full (in case of array implementation).
➤ Useful for avoiding overflow errors.
int isEmpty() {
}
// Function to check if stack is full
int isFull() {
// Push operation
if (isFull()) {
} else {
top++;
stack[top] = value;
// Pop operation
void pop() {
if (isEmpty()) {
} else {
// Peek operation
void peek() {
if (isEmpty()) {
printf("Stack is empty.\n");
} else {
struct Node {
int data;
};
free(temp);
temp = temp->next;
if (temp->prev != NULL)
temp->prev->next = NULL;
else
*head = NULL;
free(temp);
#define SIZE 5
printf("Queue Overflow\n");
else {
cq[rear] = val;
void delete() {
if (front == -1)
printf("Queue Underflow\n");
else {
if (front == rear)
else
}
f) What is Tree? Explain Methods of Tree Traversal 🌳
✅ Definition:
A tree is a non-linear hierarchical data structure consisting of nodes connected by
edges.
● Example Use: Retrieve sorted data from a Binary Search Tree (BST)
● Example Use: Printing a tree level-wise or finding the shortest path in a tree