10 Marks Question
10 Marks Question
1. Create a Binary Search Tree (BST) for the following values: 45, 15, 79, 90, 10, 55, 12, 20, 50.
4. Insert 90: 90 is greater than 45 and greater than 79, so it goes to the right of 79.
5. Insert 10: 10 is less than 45 and less than 15, so it goes to the left of 15.
6. Insert 55: 55 is greater than 45 but less than 79, so it goes to the left of 79.
7. Insert 12: 12 is less than 45, less than 15, but greater than 10, so it goes to the right of 10.
8. Insert 20: 20 is less than 45, greater than 15, so it goes to the right of 15.
9. Insert 50: 50 is greater than 45, less than 55, so it goes to the left of 55.
Resulting BST:
45
/ \
15 79
/ \ / \
10 20 55 90
\ / /
12 50
Inorder Traversal (Left, Root, Right): 10, 12, 15, 20, 45, 50, 55, 79, 90
Preorder Traversal (Root, Left, Right): 45, 15, 10, 12, 20, 79, 55, 50, 90
Postorder Traversal (Left, Right, Root): 12, 10, 20, 15, 50, 55, 90, 79, 45
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
};
if (prev_node == NULL) {
return;
new_node->data = new_data;
new_node->next = prev_node->next;
prev_node->next = new_node;
temp = temp->next;
printf("NULL\n");
int main() {
head->data = 10;
head->next = NULL;
struct Node* second = (struct Node*)malloc(sizeof(struct Node));
second->data = 20;
second->next = NULL;
head->next = second;
printList(head);
printList(head);
return 0;
A tree is a hierarchical data structure with a set of nodes, where each node has a value and a list of
references to other nodes (children). The tree has a root node from which all other nodes descend.
Tree Traversals:
o Visit the left subtree, then the root, then the right subtree.
o Algorithm:
o Inorder(node):
o if node is NULL:
o return
o Inorder(node.left)
o visit node
o Inorder(node.right)
o Algorithm:
o Preorder(node):
o if node is NULL:
o return
o visit node
o Preorder(node.left)
o Preorder(node.right)
o Visit the left subtree, then the right subtree, then the root.
o Algorithm:
o Postorder(node):
o if node is NULL:
o return
o Postorder(node.left)
o Postorder(node.right)
o visit node
Example:
For a tree:
/\
B C
/\ \
D E F
Inorder: D, B, E, A, C, F
Preorder: A, B, D, E, C, F
Postorder: D, E, B, F, C, A
#include <stdio.h>
// Linear Search Function
if (arr[i] == key) {
return i;
return -1;
if (arr[mid] == key) {
return mid;
left = mid + 1;
} else {
right = mid - 1;
return -1;
int main() {
// Binary Search
return 0;
6. Apply Quick Sort to Sort Numbers: 22, 47, 43, 34, 10, 60, 55, 31, 20 Using '22' as Pivot Element
1. Initial Array: 22, 47, 43, 34, 10, 60, 55, 31, 20
o Pivot: 22
o Pivot: 10
o Left: []
o Right: [20]
o Pivot: 47
o Pivot: 43
o Left: [31]
o Right: [34]
o Left: [55]
o Right: []
Sorted Array: [10, 20, 22, 31, 34, 43, 47, 55, 60]
7. How Stacks Are Used in Expression Evaluation: Converting Infix to Postfix Notation
Infix expressions (like A + B * C) are typically written in a form where operators are between
operands. In contrast, postfix expressions (like A B C * +) place operators after operands, which
eliminates the need for parentheses and follows a clear order of operations.
1. Initialize an empty stack for operators and an empty list for the postfix expression.
Pop operators from the stack to the postfix list while the top of the stack has
operators with higher or equal precedence.
o If the character is a closing parenthesis ), pop all operators from the stack to the
postfix list until an open parenthesis is encountered (pop and discard the open
parenthesis).
3. After processing the entire infix expression, pop any remaining operators from the stack to
the postfix list.
Example:
Infix: A + B * C
1. Start with: A + B * C
Array-based Stack:
Array Representation: A stack can be represented by an array where we keep track of the
index of the top element.
o Increment the top pointer and insert the new element at the top position in the
array.
o Return the item at the top of the stack, then decrement the top pointer.
Advantages:
o Simple to implement.
Disadvantages:
o Wasted space if the stack size is smaller than the array size.
Linked List Representation: Each element is stored in a node, and each node points to the
next node. The top of the stack is represented by the head of the linked list.
Push Operation:
Pop Operation:
o Remove the top node, and move the top pointer to the next node.
Advantages:
o No wasted space.
Disadvantages:
1. Reverse the Infix Expression: Reverse the given expression and replace ( with ) and vice
versa.
o Q = )H + G - )T + P^N * M( * C * B + A( = Q
I'll skip detailed steps here for brevity, but the result is:
10. What is Linked List? Write and Explain the Algorithm for Create, Insertion, and Traverse
Operations in Doubly Linked List
A Linked List is a linear data structure where elements are stored in nodes, and each node points to
the next node. In a Doubly Linked List, each node contains two pointers: one to the next node and
one to the previous node.
1. Create a Node structure with three parts: data, prev, and next.
2. Initialize the list by creating a head pointer and setting it to NULL initially.
Insertion:
At the beginning:
3. Set the prev pointer of the current head to the new node.
At the end:
2. Set the next pointer of the last node to the new node.
3. Set the prev pointer of the new node to the last node.
Traversal:
Forward Traversal: Start at the head and follow the next pointers.
Backward Traversal: Start at the last node and follow the prev pointers.
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
};
new_node->data = data;
new_node->next = *head;
new_node->prev = NULL;
if (*head != NULL) {
(*head)->prev = new_node;
*head = new_node;
temp = temp->next;
}
printf("NULL\n");
int main() {
insertAtBeginning(&head, 10);
insertAtBeginning(&head, 20);
insertAtBeginning(&head, 30);
printList(head);
return 0;
11. Construct a Binary Tree from Given Pre-order and In-order Traversals
1. Pre-order Traversal: Root is always the first element. Here, the root is A.
2. In-order Traversal: A divides the nodes into left and right subtrees.
Step-by-step construction:
Root = A
o Left subtree of B: D
o Right subtree of B: E H
o Right subtree of I: C G
/ \
B I
/\ /\
D EF C
\ \
H G
2. Traverse each node, visiting its data until reaching the NULL pointer.
current = current->next;
printf("NULL\n");
Given:
Step-by-step operations:
1. Insert 50 and 60 into the queue.
F = 2, R = 0.
F = 2, R = 1.
o Deleting 10:
F = 3, R = 1.
o Deleting 20:
F = 4, R = 1.
o Inserting 70:
F = 4, R = 2.
o Inserting 80:
o Inserting 90:
F = 4, R = 4.
Expression Tree:
1. a+(b∗c)a + (b * c)
2. (d∗e+f)∗g(d * e + f) * g
/ \
/ \
+ *
/ \ / \
a * + g
/ \ / \
b cd *
o Inorder: a+b∗c+d∗e+f+ga + b * c + d * e + f + g
o Preorder: ++a∗bc∗+defg+ + a * b c * + d e f g
o Postorder: abc∗+de∗f+g∗+a b c * + d e * f + g * +
Algorithm:
1. For i=1i = 1 to n−1n-1:
o Set j=i−1j = i - 1.
Decrement jj.
Example:
Algorithm:
Example:
1. Find minimum in entire array and swap with the first element, then continue for remaining
elements.
Algorithm:
2. Perform counting sort for each digit (starting from the least significant digit).
3. Repeat this process for each digit until the most significant digit.
Example:
After sorting:
Algorithm:
Example:
1. low=0,high=5low = 0, high = 5.
2. mid=(0+5)/2=2mid = (0 + 5) / 2 = 2.
A Minimum-Cost Spanning Tree (MST) is a subset of the edges of a graph that connects all the
vertices together, without any cycles, and with the minimum possible total edge weight.
Prim's Algorithm:
2. Find the minimum-weight edge that connects the node to an unvisited node.
3. Add this edge to the MST and mark the node as visited.
Kruskal's Algorithm:
1. Sort all edges by weight.
2. Add edges to the MST, ensuring no cycles are formed (use union-find to detect cycles).
o Dequeue a node.
Example:
Example:
Start from AA, visit AA, then BB, then DD, and finally CC.