Part A:
-
Fill in the blanks:
a. The type of data structure that lists an element with its predecessor and successor is called Doubly Linked List.
b. The process of inserting elements in increasing or decreasing order in a Priority Queue is called Priority Queuing.
c. In a Linked List, the first node is called Head.
d. While traversing through a Stack, the element first removed is the Top.
e. The time-complexity of a binary search algorithm is O(log n).
-
Match the following:
a. AVL tree - ii. Balanced Binary Search Tree
b. Bubble Sort - iii. O(n²)
c. Hashing - i. Open Addressing
d. Dijkstra’s Algorithm - iv. Shortest Path Algorithm
-
True or False:
a. B-Trees have a variable number of keys per node as compared to BSTs. - True
b. Selection Sort is faster than Quick Sort for large data sets. - False
Part B:
-
An array is a data structure which stores elements in contiguous memory locations and has a fixed size. A linked list, on the other hand, is a data structure where elements are not stored in contiguous memory locations, but rather, each element points to the next element in the list. The size of a linked list is not fixed and can be changed dynamically.
-
A stack is a data structure where the last element added is the first element to be removed (Last-In-First-Out). An example of a stack in real life would be a pile of plates at a buffet where the top plate is the first one to be taken. A queue, on the other hand, is a data structure where the first element added is the first element to be removed (First-In-First-Out). An example of a queue in real life would be a line of people waiting to buy tickets.
-
A priority queue is a data structure where each element is assigned a priority value and the element with the highest (or lowest) priority is removed first. The types of priority queues include:
- Max Priority Queue: the element with the highest priority is removed first.
- Min Priority Queue: the element with the lowest priority is removed first.
- Indexed Priority Queue: elements have an index value assigned to them along with their priority value.
- Algorithm to convert an arithmetic expression from Infix to Postfix notation:
- Create a stack to store operators.
- Scan the infix expression from left to right.
- If the token is an operand, output it.
- If the token is an operator:
a. While the stack is not empty and the top of the stack has higher or equal precedence than the token, pop it and output it.
b. Push the token onto the stack.
- If there are remaining operators in the stack, pop and output them.
Example:
Infix expression: 2 + 3 * 4 - 6
Scan from left to right:
2 -> output
- -> push onto stack
3 -> output
- -> push onto stack
4 -> output
- -> pop * and +, output *, push - onto stack
6 -> output
Remaining operators in stack: -, output -
- Breadth-First Traversal and Depth-First Traversal are methods of traversing a graph or a tree. Breadth-First Traversal visits nodes level by level, starting from the root node. Depth-First Traversal, on the other hand, explores the graph or tree branch by branch, moving to the deepest level possible before backtracking. Examples:
Breadth-First Traversal:
A
/ \
B C
/ \ / \
D E F G
Visited in order: A, B, C, D, E, F, G
Depth-First Traversal:
A
/ \
B C
/ \ / \
D E F G
Visited in order: A, B, D, E, C, F, G
Part C:
- Algorithm for Binary Search Tree Insertion:
- If the root node is null, create a new node with the given data and make it the root node.
- If the data to be inserted is less than the data in the current node, go to the left subtree.
- If the data to be inserted is greater than the data in the current node, go to the right subtree.
- Repeat steps 2 and 3 until a null node is reached.
- Create a new node with the given data and make it the left child (if data is less than parent node) or right child (if data is greater than parent node) of the null node.
Implementation:
struct node* insert(struct node* root, int data) {
if (root == NULL) {
root = (struct node*)malloc(sizeof(struct node));
root->data = data;
root->left = NULL;
root->right = NULL;
}
else if (data < root->data) {
root->left = insert(root->left, data);
}
else if (data > root->data) {
root->right = insert(root->right, data);
}
return root;
}
- Notes on:
a. Radix Sort: Radix Sort is a non-comparison based sorting algorithm that uses the principle of sorting digits in a number. It sorts the elements by comparing the values of their digits at different positions. The elements are sorted based on the least significant digit first and then the process is repeated for the next significant digit until all the digits are compared. Radix Sort has a time complexity of O(dn) where d is the maximum number of digits in the elements. It can be used for sorting large sets of numbers and strings.
b. Red-Black Tree: Red-Black Tree is a type of balanced binary search tree where each node is colored either red or black. The tree is balanced by maintaining the property that the longest path from the root to any leaf node is no more than twice the length of the shortest path from the root to any other leaf node. Red-Black Tree operations such as insertion, deletion and search have a worst-case time complexity of O(log n).
c. Kruskal’s Algorithm: Kruskal’s Algorithm is a greedy algorithm used to find the minimum spanning tree of a connected weighted graph. It works by sorting the edges of the graph in non-decreasing order of weight and then adding the edges to the spanning tree if they do not form a cycle. Kruskal’s Algorithm has a time complexity of O(E log E) where E is the number of edges in the graph.