Implementing a Min Heap in JavaScript
Min heaps are fundamental data structures used in various algorithms, including priority queues and heap sort. This challenge asks you to implement a min heap data structure in JavaScript, allowing you to efficiently manage and retrieve the smallest element. A min heap ensures that the root node always holds the smallest value, making it ideal for scenarios where you need to repeatedly access the minimum element.
Problem Description
You are tasked with creating a MinHeap class in JavaScript. This class should support the following operations:
insert(value): Inserts a new value into the heap, maintaining the min-heap property.extractMin(): Removes and returns the minimum value (root) from the heap, re-heapifying to maintain the min-heap property. Returnsnullif the heap is empty.peek(): Returns the minimum value (root) without removing it. Returnsnullif the heap is empty.size(): Returns the number of elements in the heap.isEmpty(): Returnstrueif the heap is empty,falseotherwise.
The min-heap property dictates that for every node i in the heap, the value of the node is less than or equal to the value of its children. You should use an array to represent the heap internally.
Examples
Example 1:
Input:
Initial Heap: []
insert(5)
insert(3)
insert(8)
extractMin()
insert(1)
extractMin()
extractMin()
extractMin()
extractMin()
Output:
5
3
1
8
null
Explanation: The heap starts empty. 5, 3, and 8 are inserted, resulting in a heap structure. extractMin() removes 3. 1 is inserted, and then 3, 1, 5, and 8 are extracted in that order, leaving the heap empty.
Example 2:
Input:
Initial Heap: []
peek()
insert(10)
peek()
insert(5)
peek()
extractMin()
peek()
Output:
null
10
5
5
null
Explanation: peek() on an empty heap returns null. 10 is inserted, peek() returns 10. 5 is inserted, peek() returns 5. extractMin() removes 5, and peek() returns 10.
Example 3: (Edge Case - Empty Heap)
Input:
Initial Heap: []
size()
isEmpty()
extractMin()
Output:
0
true
null
Explanation: The heap is initially empty. size() returns 0, isEmpty() returns true, and extractMin() returns null.
Constraints
- The heap will contain numbers only.
- The number of elements inserted will be between 0 and 1000 (inclusive).
- The values inserted will be integers between -1000 and 1000 (inclusive).
- The
extractMin()andpeek()operations should be efficient, ideally with a time complexity of O(log n), where n is the number of elements in the heap.
Notes
- Consider using an array to represent the heap. The index of a node's children can be calculated as
2i + 1(left child) and2i + 2(right child), whereiis the index of the parent node. - The
insertoperation involves adding the new element to the end of the array and then "heapifying up" by swapping the element with its parent until the min-heap property is satisfied. - The
extractMinoperation involves replacing the root with the last element in the array, removing the last element, and then "heapifying down" by swapping the root with the smaller of its children until the min-heap property is satisfied. - Pay close attention to edge cases, such as inserting into an empty heap or extracting from an empty heap.