|
| 1 | +/* |
| 2 | + Implement a Min-Heap class that supports |
| 3 | +
|
| 4 | + Building a Min Heap from an input array of integers. |
| 5 | + Inserting integers in the heap. |
| 6 | + Removing the heap's minimum / root value. |
| 7 | + Peeking at the heap's minimum / root value. |
| 8 | + Sifting integers up and down the heap, which is to be used when inserting and removing values. |
| 9 | +
|
| 10 | + Note that the heap should be represented in the form of an array. |
| 11 | +
|
| 12 | + Explanation: |
| 13 | +
|
| 14 | + The code snippet implements a MinHeap data structure in Go. |
| 15 | +
|
| 16 | + - `NewMinHeap`: This function creates a new MinHeap from an input array and returns a pointer to the MinHeap object. |
| 17 | + It calls the `BuildHeap` method to construct the heap structure. |
| 18 | + - `BuildHeap`: This method constructs the heap by iteratively calling `siftDown` on each parent node starting from the |
| 19 | + last non-leaf node. |
| 20 | + - `siftDown`: This method corrects the heap property by moving an element down the heap until it reaches its correct position. It compares the element with its children and swaps it with the smaller child if necessary. |
| 21 | + - `siftUp`: This method corrects the heap property by moving an element up the heap until it reaches its correct position. |
| 22 | + It compares the element with its parent and swaps it if necessary. |
| 23 | + - `Peek`: This method returns the minimum element in the heap (the root of the heap) without removing it. |
| 24 | + - `Remove`: This method removes and returns the minimum element in the heap. It swaps the root with the last element, |
| 25 | + removes the last element from the heap, and then calls `siftDown` to maintain the heap property. |
| 26 | + - `Insert`: This method inserts a new element into the heap. It appends the element to the end of the heap and then |
| 27 | + calls `siftUp` to maintain the heap property. |
| 28 | + - `swap`: This method swaps two elements in the heap given their indices. |
| 29 | + - `length`: This method returns the number of elements in the heap. |
| 30 | +
|
| 31 | + Overall, this code provides a basic implementation of a MinHeap data structure, allowing for efficient insertion, removal, |
| 32 | + and retrieval of the minimum element. |
| 33 | +
|
| 34 | + BuildHeap: O(n) time | O(1) space - where n is the length of the input array |
| 35 | + SiftDown: O(log(n)) time | O(1) space - where n is the length of the heap |
| 36 | + SiftUp: O(log(n)) time | O(1) space - where n is the length of the heap |
| 37 | + Peek: O(1) time | O(1) space |
| 38 | + Remove: O(log(n)) time | O(1) space - where n is the length of the heap |
| 39 | + Insert: O(log(n)) time | O(1) space - where n is the length of the heap |
| 40 | +
|
| 41 | +*/ |
| 42 | +#include <vector> |
| 43 | + |
| 44 | +class MinHeap { |
| 45 | +public: |
| 46 | + std::vector<int> heap; // The heap represented as a vector |
| 47 | + |
| 48 | + void buildHeap(std::vector<int>& array) { |
| 49 | + int first = (array.size() - 2) / 2; // Start from the last parent node |
| 50 | + for (int currentIdx = first; currentIdx >= 0; currentIdx--) { |
| 51 | + siftDown(currentIdx, array.size() - 1); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + void siftDown(int currentIndex, int endIndex) { |
| 56 | + int childOneIdx = currentIndex * 2 + 1; // Calculate the index of the first child |
| 57 | + while (childOneIdx <= endIndex) { |
| 58 | + int childTwoIdx = -1; // Initialize the index of the second child |
| 59 | + if (currentIndex * 2 + 2 <= endIndex) { |
| 60 | + childTwoIdx = currentIndex * 2 + 2; // Calculate the index of the second child if it exists |
| 61 | + } |
| 62 | + int indexToSwap = childOneIdx; // Assume the first child is the one to swap with |
| 63 | + if (childTwoIdx > -1 && heap[childOneIdx] > heap[childTwoIdx]) { |
| 64 | + // If the second child exists and is smaller, update the index to swap with |
| 65 | + indexToSwap = childTwoIdx; |
| 66 | + } |
| 67 | + if (heap[currentIndex] > heap[indexToSwap]) { |
| 68 | + // If the current element is greater than the one to swap with, perform the swap |
| 69 | + swap(currentIndex, indexToSwap); |
| 70 | + currentIndex = indexToSwap; |
| 71 | + childOneIdx = currentIndex * 2 + 1; // Update the index of the first child |
| 72 | + } else { |
| 73 | + return; |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + void siftUp() { |
| 79 | + int currentIdx = heap.size() - 1; // Start from the last element |
| 80 | + int parentIdx = (currentIdx - 1) / 2; // Calculate the index of the parent |
| 81 | + while (currentIdx > 0) { |
| 82 | + int current = heap[currentIdx]; |
| 83 | + int parent = heap[parentIdx]; |
| 84 | + if (current < parent) { |
| 85 | + // If the current element is smaller than the parent, perform the swap |
| 86 | + swap(currentIdx, parentIdx); |
| 87 | + currentIdx = parentIdx; |
| 88 | + parentIdx = (currentIdx - 1) / 2; // Update the index of the parent |
| 89 | + } else { |
| 90 | + return; |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + int peek() { |
| 96 | + if (heap.empty()) { |
| 97 | + return -1; |
| 98 | + } |
| 99 | + return heap[0]; // Return the minimum element at the top of the heap |
| 100 | + } |
| 101 | + |
| 102 | + int remove() { |
| 103 | + int l = heap.size(); |
| 104 | + swap(0, l - 1); // Swap the root with the last element |
| 105 | + int peeked = heap[l - 1]; // Remove the last element (minimum) and store it |
| 106 | + heap.pop_back(); |
| 107 | + siftDown(0, l - 2); // Sift down the new root element |
| 108 | + return peeked; |
| 109 | + } |
| 110 | + |
| 111 | + void insert(int value) { |
| 112 | + heap.push_back(value); // Append the new element to the end of the heap |
| 113 | + siftUp(); // Sift up the new element to its correct position |
| 114 | + } |
| 115 | + |
| 116 | + void swap(int i, int j) { |
| 117 | + int temp = heap[i]; |
| 118 | + heap[i] = heap[j]; |
| 119 | + heap[j] = temp; // Swap elements at indices i and j |
| 120 | + } |
| 121 | + |
| 122 | + int length() { |
| 123 | + return heap.size(); // Return the number of elements in the heap |
| 124 | + } |
| 125 | +}; |
| 126 | + |
| 127 | +MinHeap* newMinHeap(std::vector<int>& array) { |
| 128 | + MinHeap* heap = new MinHeap(); // Create a new MinHeap object |
| 129 | + heap->buildHeap(array); // Build the heap using the given array |
| 130 | + return heap; |
| 131 | +} |
0 commit comments