|
| 1 | + |
| 2 | +/* Minimum Priority Queue |
| 3 | +* It is a part of heap data structure |
| 4 | +* A heap is a specific tree based data structure |
| 5 | +* in which all the nodes of tree are in a specific order. |
| 6 | +* that is the children are arranged in some |
| 7 | +* respect of their parents, can either be greater |
| 8 | +* or less than the parent. This makes it a min priority queue |
| 9 | +* or max priority queue. |
| 10 | +*/ |
| 11 | + |
| 12 | +// Functions: insert, delete, peek, isEmpty, print, heapSort, sink |
| 13 | + |
| 14 | +class MinPriorityQueue { |
| 15 | + |
| 16 | + // calss the constructor and initializes the capacity |
| 17 | + constructor(c) { |
| 18 | + this.heap = []; |
| 19 | + this.capacity = c; |
| 20 | + this.size = 0; |
| 21 | + } |
| 22 | + |
| 23 | + // inserts the key at the end and rearranges it |
| 24 | + // so that the binary heap is in appropriate order |
| 25 | + insert(key) { |
| 26 | + if (this.isFull()) return; |
| 27 | + this.heap[this.size + 1] = key; |
| 28 | + let k = this.size + 1; |
| 29 | + while (k > 1) { |
| 30 | + if (this.heap[k] < this.heap[Math.floor(k / 2)]) { |
| 31 | + let temp = this.heap[k]; |
| 32 | + this.heap[k] = this.heap[Math.floor(k / 2)]; |
| 33 | + this.heap[Math.floor(k / 2)] = temp; |
| 34 | + } |
| 35 | + k = Math.floor(k / 2); |
| 36 | + } |
| 37 | + this.size++; |
| 38 | + } |
| 39 | + |
| 40 | + // returns the highest priority value |
| 41 | + peek() { |
| 42 | + return this.heap[1]; |
| 43 | + } |
| 44 | + |
| 45 | + // returns boolean value whether the heap is empty or not |
| 46 | + isEmpty() { |
| 47 | + if (0 == this.size) return true; |
| 48 | + return false; |
| 49 | + } |
| 50 | + |
| 51 | + // returns boolean value whether the heap is full or not |
| 52 | + isFull() { |
| 53 | + if (this.size == this.capacity) return true; |
| 54 | + return false; |
| 55 | + } |
| 56 | + |
| 57 | + // prints the heap |
| 58 | + print() { |
| 59 | + console.log(this.heap.slice(1)); |
| 60 | + } |
| 61 | + |
| 62 | + // heap sorting can be done by performing |
| 63 | + // delete function to the number of times of the size of the heap |
| 64 | + // it returns reverse sort because it is a min priority queue |
| 65 | + heapSort() { |
| 66 | + for (let i = 1; i < this.capacity; i++) { |
| 67 | + this.delete(); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + // this function reorders the heap after every delete function |
| 72 | + sink() { |
| 73 | + let k = 1; |
| 74 | + while (2 * k <= this.size || 2 * k + 1 <= this.size) { |
| 75 | + let minIndex; |
| 76 | + if (this.heap[2 * k] >= this.heap[k]) { |
| 77 | + if (2 * k + 1 <= this.size && this.heap[2*k+1] >= this.heap[k]) { |
| 78 | + break; |
| 79 | + } |
| 80 | + else if(2*k+1 > this.size){ |
| 81 | + break; |
| 82 | + } |
| 83 | + } |
| 84 | + if (2 * k + 1 > this.size) { |
| 85 | + minIndex = this.heap[2 * k] < this.heap[k] ? 2 * k : k; |
| 86 | + } else { |
| 87 | + if ( |
| 88 | + this.heap[k] > this.heap[2 * k] || |
| 89 | + this.heap[k] > this.heap[2 * k + 1] |
| 90 | + ) { |
| 91 | + minIndex = |
| 92 | + this.heap[2 * k] < this.heap[2 * k + 1] ? 2 * k : 2 * k + 1; |
| 93 | + } else { |
| 94 | + minIndex = k; |
| 95 | + } |
| 96 | + } |
| 97 | + let temp = this.heap[k]; |
| 98 | + this.heap[k] = this.heap[minIndex]; |
| 99 | + this.heap[minIndex] = temp; |
| 100 | + k = minIndex; |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + // deletes the highest priority value from the heap |
| 105 | + delete() { |
| 106 | + let min = this.heap[1]; |
| 107 | + this.heap[1] = this.heap[this.size]; |
| 108 | + this.heap[this.size] = min; |
| 109 | + this.size--; |
| 110 | + this.sink(); |
| 111 | + return min; |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +// testing |
| 116 | +q = new MinPriorityQueue(8); |
| 117 | + |
| 118 | +q.insert(5); |
| 119 | +q.insert(2); |
| 120 | +q.insert(4); |
| 121 | +q.insert(1); |
| 122 | +q.insert(7); |
| 123 | +q.insert(6); |
| 124 | +q.insert(3); |
| 125 | +q.insert(8); |
| 126 | +q.print(); // [ 1, 2, 3, 5, 7, 6, 4, 8 ] |
| 127 | +q.heapSort(); |
| 128 | +q.print(); // [ 8, 7, 6, 5, 4, 3, 2, 1 ] |
0 commit comments