1
1
const minHeapJSCode = `
2
- class MinHeap {
3
-
4
- constructor () {
5
- /* Initialing the array heap and adding a dummy element at index 0 */
6
- this.heap = [null]
2
+ function minHeap(A = []) {
3
+ this.heapify(A);
4
+ }
5
+
6
+ minHeap.prototype._shiftUp = function(i) {
7
+ let parent_i = (i / 2) >>> 0;
8
+ while (parent_i > 0) {
9
+ if (this.h[i] < this.h[parent_i]) {
10
+ [this.h[i], this.h[parent_i]] = [this.h[parent_i], this.h[i]];
11
+ }
12
+ parent_i = (parent_i / 2) >>> 0;
7
13
}
8
-
9
- peek() {
10
- /* Accessing the min element at index 1 in the heap array */
11
- return this.heap[1]
14
+ };
15
+
16
+ minHeap.prototype._shiftDown = function(i) {
17
+ while (i * 2 <= this.h.length - 1) {
18
+ const mc = this._minChild(i);
19
+ if (this.h[i] > this.h[mc]) {
20
+ [this.h[i], this.h[mc]] = [this.h[mc], this.h[i]];
21
+ }
22
+ i = mc;
12
23
}
13
-
14
- push (node) {
15
-
16
- /* Inserting the new node at the end of the heap array */
17
- this.heap.push(node)
18
-
19
- /* Finding the correct position for the new node */
20
-
21
- if (this.heap.length > 1) {
22
- let current = this.heap.length - 1
23
-
24
- /* Traversing up the parent node until the current node (current) is greater than the parent (current/2)*/
25
- while (current > 1 && this.heap[Math.floor(current/2)] > this.heap[current]) {
26
-
27
- /* Swapping the two nodes by using the ES6 destructuring syntax*/
28
- [this.heap[Math.floor(current/2)], this.heap[current]] = [this.heap[current], this.heap[Math.floor(current/2)]]
29
- current = Math.floor(current/2)
30
- }
31
- }
32
- }
33
-
34
- pop() {
35
- /* Smallest element is at the index 1 in the heap array */
36
- let smallest = this.heap[1]
37
-
38
- /* When there are more than two elements in the array, we put the right most element at the first position
39
- and start comparing nodes with the child nodes
40
- */
41
- if (this.heap.length > 2) {
42
- this.heap[1] = this.heap[this.heap.length-1]
43
- this.heap.splice(this.heap.length - 1)
44
-
45
- if (this.heap.length === 3) {
46
- if (this.heap[1] > this.heap[2]) {
47
- [this.heap[1], this.heap[2]] = [this.heap[2], this.heap[1]]
48
- }
49
- return smallest
50
- }
51
-
52
- let current = 1
53
- let leftChildIndex = current * 2
54
- let rightChildIndex = current * 2 + 1
55
-
56
- while (this.heap[leftChildIndex] &&
57
- this.heap[rightChildIndex] &&
58
- (this.heap[current] > this.heap[leftChildIndex] ||
59
- this.heap[current] > this.heap[rightChildIndex])) {
60
- if (this.heap[leftChildIndex] < this.heap[rightChildIndex]) {
61
- [this.heap[current], this.heap[leftChildIndex]] = [this.heap[leftChildIndex], this.heap[current]]
62
- current = leftChildIndex
63
- } else {
64
- [this.heap[current], this.heap[rightChildIndex]] = [this.heap[rightChildIndex], this.heap[current]]
65
- current = rightChildIndex
66
- }
67
-
68
- leftChildIndex = current * 2
69
- rightChildIndex = current * 2 + 1
70
- }
71
- }
72
-
73
- /* If there are only two elements in the array, we directly splice out the first element */
74
-
75
- else if (this.heap.length === 2) {
76
- this.heap.splice(1, 1)
77
- } else {
78
- return null
79
- }
80
-
81
- return smallest
24
+ };
25
+
26
+ minHeap.prototype._minChild = function(i) {
27
+ if (i * 2 + 1 > this.h.length - 1) return i * 2;
28
+ if (this.h[i * 2] < this.h[i * 2 + 1]) return i * 2;
29
+ return i * 2 + 1;
30
+ };
31
+
32
+ minHeap.prototype.pop = function() {
33
+ if (this.h.length === 1) throw new Error('空了就别弹了吧?');
34
+ const ans = this.h[1];
35
+ this.h[1] = this.h[this.h.length - 1];
36
+ this.h.pop();
37
+ this._shiftDown(1);
38
+ return ans;
39
+ };
40
+
41
+ minHeap.prototype.push = function(a) {
42
+ this.h.push(a);
43
+ this._shiftUp(this.h.length - 1);
44
+ console.log(this.h);
45
+ };
46
+
47
+ minHeap.prototype.heapify = function(A) {
48
+ this.h = [0].concat(A);
49
+ i = 1;
50
+ while (i < this.h.length) {
51
+ this._shiftDown(i);
52
+ i++;
82
53
}
83
- /**
84
- * Your MinHeap object will be instantiated and called as such:
85
- * var obj = new MinHeap()
86
- * obj.push(1)
87
- * obj .push(2)
88
- * obj.peek() // will return 1
89
- * obj .pop() // remove 1
90
- * obj.peek() // will return 2
91
- */
54
+ };
55
+
56
+ // test:
57
+ h = new minHeap([1, 2, 3]);
58
+ h .push(4);
59
+ h.push(5);
60
+ h .pop();
61
+ h.pop();
62
+ console.log(h.h); // inspect internal value
92
63
}
93
64
` ;
94
65
95
66
const minHeapPythonCode = `
96
67
class min_heap:
97
- def __init__(self):
98
- self.h = [0]
68
+ def __init__(self, A=[] ):
69
+ self.heapify(A)
99
70
100
71
def shift_up(self, i):
101
72
while i // 2 > 0:
@@ -131,7 +102,7 @@ class min_heap:
131
102
self.h.append(a)
132
103
self.shift_up(len(self.h)-1)
133
104
134
- def build_heap (self, A):
105
+ def heapify (self, A):
135
106
self.h = [0] + A
136
107
i = 1
137
108
while (i < len(self.h)):
@@ -140,8 +111,7 @@ class min_heap:
140
111
141
112
# 使用:
142
113
143
- h = min_heap()
144
- h.build_heap([5, 6, 2, 3])
114
+ h = min_heap([5, 6, 2, 3])
145
115
146
116
h.heappush(1)
147
117
h.heappop() # 1
@@ -293,6 +263,5 @@ module.exports = {
293
263
] ,
294
264
} ,
295
265
] ,
296
- link :
297
- "https://leetcode-solution.cn/solutionDetail?url=https%3A%2F%2Fapi.github.com%2Frepos%2Fazl397985856%2Fleetcode%2Fcontents%2Fthinkings%2Fheap.md&type=1" ,
266
+ link : "https://leetcode-solution.cn/solutionDetail?url=https%3A%2F%2Fapi.github.com%2Frepos%2Fazl397985856%2Fleetcode%2Fcontents%2Fthinkings%2Fheap.md&type=1" ,
298
267
} ;
0 commit comments