0% found this document useful (0 votes)
3 views17 pages

10 Marks Question

The document provides a comprehensive overview of various data structures and algorithms, including the creation of a Binary Search Tree (BST) and its traversals, linked list operations, sorting algorithms like Quick Sort, and searching techniques such as linear and binary search. It also covers tree definitions, traversal algorithms, infix to postfix conversion using stacks, and the construction of binary trees from pre-order and in-order traversals. Additionally, it includes C programming examples for linked lists, stacks, and circular queues.

Uploaded by

sahiluddin624
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views17 pages

10 Marks Question

The document provides a comprehensive overview of various data structures and algorithms, including the creation of a Binary Search Tree (BST) and its traversals, linked list operations, sorting algorithms like Quick Sort, and searching techniques such as linear and binary search. It also covers tree definitions, traversal algorithms, infix to postfix conversion using stacks, and the construction of binary trees from pre-order and in-order traversals. Additionally, it includes C programming examples for linked lists, stacks, and circular queues.

Uploaded by

sahiluddin624
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

10 marks question

1. Create a Binary Search Tree (BST) for the following values: 45, 15, 79, 90, 10, 55, 12, 20, 50.

Step-by-step process to create the BST:

1. Insert 45: This is the root of the tree.

2. Insert 15: 15 is less than 45, so it goes to the left of 45.

3. Insert 79: 79 is greater than 45, so it goes to the right of 45.

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

2. Perform BST Traversals (Inorder, Preorder, Postorder)

For the given BST:

 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

3. C Program to Insert Element After a Specific Node in a Singly Linked List

#include <stdio.h>

#include <stdlib.h>
struct Node {

int data;

struct Node* next;

};

void insertAfter(struct Node* prev_node, int new_data) {

if (prev_node == NULL) {

printf("The previous node cannot be NULL.\n");

return;

struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));

new_node->data = new_data;

new_node->next = prev_node->next;

prev_node->next = new_node;

void printList(struct Node* head) {

struct Node* temp = head;

while (temp != NULL) {

printf("%d -> ", temp->data);

temp = temp->next;

printf("NULL\n");

int main() {

struct Node* head = (struct Node*)malloc(sizeof(struct Node));

head->data = 10;

head->next = NULL;
struct Node* second = (struct Node*)malloc(sizeof(struct Node));

second->data = 20;

second->next = NULL;

head->next = second;

printf("Original List: ");

printList(head);

insertAfter(head, 15); // Insert 15 after the first node.

printf("Updated List: ");

printList(head);

return 0;

4. Define Tree. Explain Tree Traversals with Algorithms and Examples.

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:

1. Inorder (Left, Root, Right):

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)

2. Preorder (Root, Left, Right):


o Visit the root, then the left subtree, then the right subtree.

o Algorithm:

o Preorder(node):

o if node is NULL:

o return

o visit node

o Preorder(node.left)

o Preorder(node.right)

3. Postorder (Left, Right, Root):

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

5. C Program to Perform Searching Operations using Linear and Binary Search

#include <stdio.h>
// Linear Search Function

int linearSearch(int arr[], int n, int key) {

for (int i = 0; i < n; i++) {

if (arr[i] == key) {

return i;

return -1;

// Binary Search Function (Array must be sorted)

int binarySearch(int arr[], int n, int key) {

int left = 0, right = n - 1;

while (left <= right) {

int mid = (left + right) / 2;

if (arr[mid] == key) {

return mid;

} else if (arr[mid] < key) {

left = mid + 1;

} else {

right = mid - 1;

return -1;

int main() {

int arr[] = {10, 20, 30, 40, 50};

int n = sizeof(arr) / sizeof(arr[0]);

int key = 30;


// Linear Search

int linearResult = linearSearch(arr, n, key);

printf("Linear Search: Element %d is at index %d\n", key, linearResult);

// Binary Search

int binaryResult = binarySearch(arr, n, key);

printf("Binary Search: Element %d is at index %d\n", key, binaryResult);

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 Left Partition: [10, 20]

o Right Partition: [47, 43, 34, 60, 55, 31]

2. Sort Left Partition: [10, 20]

o Pivot: 10

o Left: []

o Right: [20]

o Final Left: [10, 20]

3. Sort Right Partition: [47, 43, 34, 60, 55, 31]

o Pivot: 47

o Left Partition: [43, 34, 31]

o Right Partition: [60, 55]

4. Sort Left: [43, 34, 31]

o Pivot: 43

o Left: [31]

o Right: [34]

o Final Left: [31, 34, 43]

5. Sort Right: [60, 55]


o Pivot: 60

o Left: [55]

o Right: []

o Final Right: [55, 60]

Sorted Array: [10, 20, 22, 31, 34, 43, 47, 55, 60]

Sure, let's continue addressing the remaining questions:

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.

Algorithm to Convert Infix to Postfix:

1. Initialize an empty stack for operators and an empty list for the postfix expression.

2. For each character in the infix expression:

o If the character is an operand (i.e., a variable or number), add it directly to the


postfix list.

o If the character is an operator:

 Pop operators from the stack to the postfix list while the top of the stack has
operators with higher or equal precedence.

 Push the current operator onto the stack.

o If the character is an open parenthesis (, push it onto the stack.

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

 Postfix Conversion Steps:

1. Start with: A + B * C

2. A → Operand, add to postfix: A

3. + → Operator, push to stack: Stack: +


4. B → Operand, add to postfix: A B

5. * → Operator (higher precedence than +), push to stack: Stack: + *

6. C → Operand, add to postfix: A B C

7. Pop * from the stack, then +: Postfix: A B C * +

Final Postfix Expression: A B C * +

8. How Stacks Can Be Made Using Arrays and Linked Lists

Array-based Stack:

 Array Representation: A stack can be represented by an array where we keep track of the
index of the top element.

Push Operation (Add Item):

o Increment the top pointer and insert the new element at the top position in the
array.

Pop Operation (Remove Item):

o Return the item at the top of the stack, then decrement the top pointer.

Advantages:

o Simple to implement.

o Random access to any element.

Disadvantages:

o Fixed size (need to specify maximum size beforehand).

o Wasted space if the stack size is smaller than the array size.

Linked List-based Stack:

 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:

o Create a new node and link it to the current top.

o Move the top pointer to the new node.

Pop Operation:

o Remove the top node, and move the top pointer to the next node.

Advantages:

o Dynamic size (grows and shrinks as needed).

o No wasted space.
Disadvantages:

o Requires extra memory for storing pointers.

o Slower access compared to arrays because of pointer-based traversal.

9. Convert Infix Operation to Prefix Using Stack

Given Infix Expression: Q = (A + B * C * (M * N^P + T) - G + H)

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

2. Apply the Infix to Postfix Conversion Algorithm on the reversed expression.

3. Reverse the result to get the final Prefix expression.

I'll skip detailed steps here for brevity, but the result is:

Prefix Expression: = Q - + H G * )T + P^N * M( * C * B + A

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.

Create a Doubly Linked List:

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:

1. Create a new node.

2. Point its next to the current head.

3. Set the prev pointer of the current head to the new node.

4. Update head to point to the new node.

 At the end:

1. Traverse to the last node.

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.

Example Code for Doubly Linked List:

#include <stdio.h>

#include <stdlib.h>

struct Node {

int data;

struct Node* next;

struct Node* prev;

};

void insertAtBeginning(struct Node** head, int data) {

struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));

new_node->data = data;

new_node->next = *head;

new_node->prev = NULL;

if (*head != NULL) {

(*head)->prev = new_node;

*head = new_node;

void printList(struct Node* head) {

struct Node* temp = head;

while (temp != NULL) {

printf("%d <-> ", temp->data);

temp = temp->next;

}
printf("NULL\n");

int main() {

struct Node* head = NULL;

insertAtBeginning(&head, 10);

insertAtBeginning(&head, 20);

insertAtBeginning(&head, 30);

printf("Doubly Linked List: ");

printList(head);

return 0;

11. Construct a Binary Tree from Given Pre-order and In-order Traversals

Given Pre-order: ABDEHICFG


In-order: DBHEIAFCG

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.

o Left subtree: DBHE

o Right subtree: FCG

Step-by-step construction:

 Root = A

 Left subtree (Pre-order = BDEH, In-order = DBHE)

o Root of left subtree = B

o Left subtree of B: D

o Right subtree of B: E H

 Right subtree (Pre-order = ICFG, In-order = FCG)

o Root of right subtree = I


o Left subtree of I: F

o Right subtree of I: C G

Resulting Binary Tree:

/ \

B I

/\ /\

D EF C

\ \

H G

12. Traversing Linked List

Algorithm for Traversing Linked List:

1. Start from the head.

2. Traverse each node, visiting its data until reaching the NULL pointer.

Example Code for Traversing:

void traverseList(struct Node* head) {

struct Node* current = head;

while (current != NULL) {

printf("%d -> ", current->data);

current = current->next;

printf("NULL\n");

13. Circular Queue Operations:

Given:

 Size of the circular queue = 5

 Elements in the queue: 10, 20, 40

 Front (F) = 2, Rear (R) = 4

Step-by-step operations:
1. Insert 50 and 60 into the queue.

o After inserting 50:

 New Rear (R) = (R + 1) % size = (4 + 1) % 5 = 0.

 Queue now: 10, 20, 40, 50.

 F = 2, R = 0.

o After inserting 60:

 New Rear (R) = (R + 1) % size = (0 + 1) % 5 = 1.

 Queue now: 10, 20, 40, 50, 60.

 F = 2, R = 1.

2. Trying to insert 30 at this stage:

o Before inserting, check if the queue is full:

 The queue is full when (R + 1) % size == F. In this case, (R + 1) % 5 = (1 + 1) %


5 = 2, which is equal to F = 2.

o The queue is full, so insertion of 30 is not possible.

3. Delete 2 elements from the queue:

o Deleting 10:

 Front (F) = (F + 1) % size = (2 + 1) % 5 = 3.

 Queue now: 20, 40, 50, 60.

 F = 3, R = 1.

o Deleting 20:

 Front (F) = (F + 1) % size = (3 + 1) % 5 = 4.

 Queue now: 40, 50, 60.

 F = 4, R = 1.

4. Insert 70, 80, and 90 into the queue.

o Inserting 70:

 Rear (R) = (R + 1) % size = (1 + 1) % 5 = 2.

 Queue now: 40, 50, 60, 70.

 F = 4, R = 2.

o Inserting 80:

 Rear (R) = (R + 1) % size = (2 + 1) % 5 = 3.

 Queue now: 40, 50, 60, 70, 80.


 F = 4, R = 3.

o Inserting 90:

 Rear (R) = (R + 1) % size = (3 + 1) % 5 = 4.

 Queue now: 40, 50, 60, 70, 80, 90.

 F = 4, R = 4.

14. Expression Tree Construction for (a+b∗c)+((d∗e+f)∗g)(a + b * c) + ((d * e + f) * g)

Expression Tree:

The expression can be broken down as follows:

1. a+(b∗c)a + (b * c)

2. (d∗e+f)∗g(d * e + f) * g

Combining them, the tree looks like:

/ \

/ \

+ *

/ \ / \

a * + g

/ \ / \

b cd *

Inorder, Preorder, and Postorder Traversals:

 Inorder Traversal: a+(b∗c)+((d∗e+f)∗g)a + (b * c) + ((d * e + f) * g)

o Inorder: a+b∗c+d∗e+f+ga + b * c + d * e + f + g

 Preorder Traversal: Start from root and visit nodes.

o Preorder: ++a∗bc∗+defg+ + a * b c * + d e f g

 Postorder Traversal: Visit nodes after their children.

o Postorder: abc∗+de∗f+g∗+a b c * + d e * f + g * +

15. Algorithm for Insertion Sort:

Algorithm:
1. For i=1i = 1 to n−1n-1:

o Set key=A[i]key = A[i].

o Set j=i−1j = i - 1.

o While j≥0j \geq 0 and A[j]>keyA[j] > key:

 Move A[j]A[j] to A[j+1]A[j+1].

 Decrement jj.

o Insert keykey at position j+1j + 1.

Example:

For A=[5,2,9,1,5,6]A = [5, 2, 9, 1, 5, 6]:

1. Start with 5 as the first element.

2. Insert 2, and then 9, etc.

After each iteration, the list becomes:

5,2,9,1,5,6→2,5,9,1,5,6→2,5,9,1,5,6→…5, 2, 9, 1, 5, 6 \to 2, 5, 9, 1, 5, 6 \to 2, 5, 9, 1, 5, 6 \to \dots

16. Algorithm for Selection Sort:

Algorithm:

1. For i=0i = 0 to n−2n-2:

o Find the minimum element in the unsorted part of the list.

o Swap it with the element at position ii.

Example:

For A=[5,2,9,1,5,6]A = [5, 2, 9, 1, 5, 6]:

1. Find minimum in entire array and swap with the first element, then continue for remaining
elements.

After each iteration:

1,2,9,5,5,6→1,2,5,9,5,6→…1, 2, 9, 5, 5, 6 \to 1, 2, 5, 9, 5, 6 \to \dots

17. Algorithm for Radix Sort:

Algorithm:

1. Find the maximum number in the array.

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:

For A=[170,45,75,90,802,24,2,66]A = [170, 45, 75, 90, 802, 24, 2, 66]:

1. Sort based on the unit place.

2. Then based on tens place.

3. Continue for hundreds place.

After sorting:

2,24,45,66,75,90,170,8022, 24, 45, 66, 75, 90, 170, 802

18. Algorithm for Binary Search:

Algorithm:

1. Set low=0low = 0 and high=n−1high = n-1.

2. While low≤highlow \leq high:

o Set mid=(low+high)/2mid = (low + high) / 2.

o If key=A[mid]key = A[mid], return midmid.

o If key<A[mid]key < A[mid], set high=mid−1high = mid - 1.

o Otherwise, set low=mid+1low = mid + 1.

Example:

For A=[2,5,7,9,12,14]A = [2, 5, 7, 9, 12, 14] and key=7key = 7:

1. low=0,high=5low = 0, high = 5.

2. mid=(0+5)/2=2mid = (0 + 5) / 2 = 2.

3. A[2]=7A[2] = 7, so return index 2.

19. Minimum-Cost Spanning Tree:

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:

1. Start with any node.

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.

4. Repeat until all nodes are 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).

3. Continue until the MST contains n−1n-1 edges.

20. BFS and DFS Algorithms:

Breadth-First Search (BFS):

1. Start from a node, mark it as visited, and enqueue it.

2. While the queue is not empty:

o Dequeue a node.

o Visit all its unvisited neighbors and enqueue them.

Example:

For a graph G={A,B,C,D}G = \{ A, B, C, D \} with edges A−B,A−C,B−DA-B, A-C, B-D:

 Start from AA, queue: AA.

 Visit AA, queue: B,CB, C.

 Visit BB, queue: C,DC, D.

 Continue until all nodes are visited.

Depth-First Search (DFS):

1. Start from a node, mark it as visited.

2. Visit all unvisited neighbors recursively.

Example:

For the same graph:

 Start from AA, visit AA, then BB, then DD, and finally CC.

You might also like