30 Lecture

CS301

Midterm & Final Term Short Notes

Inserting into a Min-Heap

Inserting a new element into a Min-Heap involves placing the new element at the bottom of the heap and then comparing it with its parent until it finds the correct position to maintain the min-heap property. To maintain the min-heap property, th


Important Mcq's
Midterm & Finalterm Prepration
Past papers included

Download PDF
  1. In a min-heap, the root node always contains the ________ element. a) Maximum b) Minimum c) Median d) Random

Answer: b) Minimum

  1. The worst-case time complexity for inserting an element in a min-heap is: a) O(1) b) O(log n) c) O(n) d) O(n log n)

Answer: b) O(log n)

  1. Which property of a min-heap ensures that the root node always contains the minimum element? a) Complete binary tree property b) Heap order property c) Both (a) and (b) d) None of the above

Answer: b) Heap order property

  1. To insert an element in a min-heap, we always add it to the: a) Leftmost position at the deepest level b) Rightmost position at the deepest level c) Leftmost position at the second deepest level d) Rightmost position at the second deepest level

Answer: a) Leftmost position at the deepest level

  1. If we insert the elements 8, 5, 3, 9, 1, 7, 6, 2 in a min-heap, what will be the root node? a) 1 b) 2 c) 3 d) 5

Answer: a) 1

  1. The height of a min-heap with n elements is: a) log n b) n/2 c) n-1 d) n

Answer: a) log n

  1. Which of the following operations is NOT supported by a min-heap? a) Insertion b) Deletion c) Search d) All of the above

Answer: c) Search

  1. To maintain the heap order property after inserting an element, we perform: a) Up-heap bubbling b) Down-heap bubbling c) Both (a) and (b) d) None of the above

Answer: a) Up-heap bubbling

  1. If we insert an element in a min-heap, the new element will always be a: a) Leaf node b) Parent node c) Child node d) Sibling node

Answer: a) Leaf node

  1. The time complexity of building a min-heap from an array of n elements is: a) O(1) b) O(n) c) O(n log n) d) O(n^2)

Answer: b) O(n)



Subjective Short Notes
Midterm & Finalterm Prepration
Past papers included

Download PDF
  1. What is a Min-Heap? A Min-Heap is a binary tree-based data structure where the value of the parent node is always less than or equal to the value of its children.

  2. What is the time complexity for inserting a new element into a Min-Heap? The time complexity for inserting a new element into a Min-Heap is O(log n).

  3. How do you insert a new element into a Min-Heap? To insert a new element into a Min-Heap, you first add the element to the end of the heap, and then you compare the element with its parent. If the parent is larger, you swap the element with its parent, and then you continue comparing the element with its new parent until you reach the root node.

  4. What is the process of maintaining the Min-Heap property after insertion? The process of maintaining the Min-Heap property after insertion involves comparing the new element with its parent and swapping it with the parent if the parent is larger. This process is repeated until the parent is smaller than the new element or until the new element becomes the root node.

  5. What happens if you try to insert a larger element into a Min-Heap? If you try to insert a larger element into a Min-Heap, the Min-Heap property will be violated, and you will need to restore the property by swapping the element with its parent and possibly its children.

  6. Can a Min-Heap have duplicate values? Yes, a Min-Heap can have duplicate values.

  7. What is the height of a Min-Heap with n elements? The height of a Min-Heap with n elements is log(n).

  8. How do you find the minimum element in a Min-Heap? The minimum element in a Min-Heap is always the root node.

  9. What is the time complexity for finding the minimum element in a Min-Heap? The time complexity for finding the minimum element in a Min-Heap is O(1).

  10. How do you delete the minimum element in a Min-Heap? To delete the minimum element in a Min-Heap, you first remove the root node and replace it with the last element in the heap. You then compare the new root with its children and swap it with the smaller child if necessary. This process is repeated until the new root is smaller than both of its children or until it becomes a leaf node.

Inserting into a Min-Heap involves adding a new element to the heap and ensuring that the heap property is maintained. The heap property of a Min-Heap requires that every node is smaller than its children. To insert a new element into a Min-Heap, the element is first placed at the next available leaf node in the bottom-most level. The new element is then compared with its parent node. If the parent node is greater than the new element, the two nodes are swapped. This process is repeated until the new element is either greater than or equal to its parent or until the root node is reached. The time complexity of inserting an element into a Min-Heap is O(log n), where n is the number of elements in the heap. This is because the worst-case scenario involves the new element being swapped with every ancestor node all the way up to the root node, which has a maximum height of log n. Inserting into a Min-Heap is a common operation in many algorithms that make use of Min-Heaps, such as Dijkstra's algorithm for finding the shortest path in a graph. By maintaining a Min-Heap of unexplored nodes in the graph, the algorithm can efficiently explore the nodes with the smallest distance from the starting node.