Heaps
We have a list of points on the plane. Find the K closest points to the origin (0, 0).
(1; 1)
(3; 2)
(-0.3; -0.4)
(-2; -1)
Data = [[3, 2], [1,1], [-0.3, -0.4], [-2, -1]], k = 2
x
y
Res = [ [-0.3, -0.4], [1,1]], k = 2
Kth Largest Element in an Array
Given [3,2,1,5,6,4] and k = 2, return 5.
Heap is a special case of balanced binary tree data structure where the root-node key is compared with its children and arranged accordingly.
The maximum number of children of a node in a heap depends on the type of heap. However, in the more commonly-used heap type, there are at most 2 children of a node and it's known as a Binary heap
Max-Heap: In a Max-Heap the key present at the root node must be greatest among the keys present at all of it’s children. The same property must be recursively true for all sub-trees in that Binary Tree.
Min-Heap: In a Min-Heap the key present at the root node must be minimum among the keys present at all of it’s children. The same property must be recursively true for all sub-trees in that Binary Tree.
A heap can be implemented using objects links (like we did for BST) and the Array!
<-- Figure out this!
Parent = (index-1)/2
Left child index*2 + 1
Right child index*2 + 2
class Heap {
constructor() {
this.data = [];
this.size = 0;
}
bubbleUp() { /* ... */ }
put(val) { /* ... */ }
print() { console.log(this.data) }
}
const heap = new Heap();
heap.push(20);
heap.push(10);
heap.push(30);
heap.push(1);
heap.push(100);
heap.print(); // [ 100, 30, 20, 1, 10 ]
Implement .push(val) method
}
Max-Heapify
Implement .pop() method
class Heap {
constructor() {
this.data = [];
this.size = 0;
}
bubbleUp() { /* ... */ }
put(val) { /* ... */ }
print() { console.log(this.data) }
maxHeapify() { /* ... */ }
pop() { /* ... */ }
}
Implement .buildMaxHeap(arr) method
class Heap {
/* ... */
buildMaxHeap(arr = []) { /* ... */ }
}
const heap = new Heap();
heap.buildMaxHeap([1,20,10,100,30]);
heap.print(); // [ 100, 30, 20, 1, 10 ]
Applications of Priority Queue
Refactor a Heap class, so you can use it as a priority queue
Heap sort is a comparison based sorting technique based on Binary Heap data structure. It is similar to selection sort where we first find the maximum element and place the maximum element at the end. We repeat the same process for remaining element.
Time Complexity: Time complexity of heapify is O(Log n). Overall time complexity of Heap Sort is O(n Log n).
Use Heap to sort an array