1- const minHeapCode = `
1+ const minHeapJSCode = `
22class MinHeap {
33
44 constructor () {
55 /* Initialing the array heap and adding a dummy element at index 0 */
66 this.heap = [null]
77 }
88
9- getMin () {
9+ peek () {
1010 /* Accessing the min element at index 1 in the heap array */
1111 return this.heap[1]
1212 }
1313
14- insert (node) {
14+ push (node) {
1515
1616 /* Inserting the new node at the end of the heap array */
1717 this.heap.push(node)
@@ -31,7 +31,7 @@ class MinHeap {
3131 }
3232 }
3333
34- remove () {
34+ pop () {
3535 /* Smallest element is at the index 1 in the heap array */
3636 let smallest = this.heap[1]
3737
@@ -83,25 +83,206 @@ class MinHeap {
8383 /**
8484 * Your MinHeap object will be instantiated and called as such:
8585 * var obj = new MinHeap()
86- * obj.insert (1)
87- * obj.insert (2)
88- * obj.getMin () // will return 1
89- * obj.remove () // remove 1
90- * obj.getMin () // will return 2
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
9191 */
9292}
9393` ;
9494
95+ const minHeapPythonCode = `
96+ class min_heap:
97+ def __init__(self):
98+ self.h = [0]
99+
100+ def shift_up(self, i):
101+ while i // 2 > 0:
102+ if self.h[i] < self.h[i // 2]:
103+ self.h[i], self.h[i//2] = self.h[i//2], self.h[i]
104+ i = i // 2
105+
106+ def shift_down(self, i):
107+ while (i * 2) <= len(self.h)-1:
108+ mc = self.minChild(i)
109+ if self.h[i] > self.h[mc]:
110+ self.h[i], self.h[mc] = self.h[mc], self.h[i]
111+ i = mc
112+
113+ def minChild(self, i):
114+ if i * 2 + 1 > len(self.h)-1:
115+ return i * 2
116+ if self.h[i*2] < self.h[i*2+1]:
117+ return i * 2
118+ else:
119+ return i * 2 + 1
120+
121+ def heappop(self):
122+ if len(self.h) == 1:
123+ return None
124+ ans = self.h[1]
125+ self.h[1] = self.h[len(self.h)-1]
126+ self.h.pop()
127+ self.shift_down(1)
128+ return ans
129+
130+ def heappush(self, a):
131+ self.h.append(a)
132+ self.shift_up(len(self.h)-1)
133+
134+ def build_heap(self, A):
135+ self.h = [0] + A
136+ i = 1
137+ while (i < len(self.h)):
138+ self.shift_down(i)
139+ i = i + 1
140+
141+ # 使用:
142+
143+ h = min_heap()
144+ h.build_heap([5, 6, 2, 3])
145+
146+ h.heappush(1)
147+ h.heappop() # 1
148+ h.heappop() # 2
149+ h.heappush(1)
150+ h.heappop() # 1
151+ h.heappop() # 3
152+ ` ;
153+
154+ const minHeapJavaCode = `
155+ // by @CaptainZ
156+ import java.util.Arrays;
157+ import java.util.Comparator;
158+
159+ /**
160+ * 用完全二叉树来构建 堆
161+ * 前置条件 起点为 1
162+ * 那么 子节点为 i <<1 和 i<<1 + 1
163+ * 核心方法为
164+ * shiftdown 交换下沉
165+ * shiftup 交换上浮
166+ * <p>
167+ * build 构建堆
168+ */
169+
170+ public class MinHeap {
171+
172+ int size = 0;
173+ int queue[];
174+
175+ public Heap(int initialCapacity) {
176+ if (initialCapacity < 1)
177+ throw new IllegalArgumentException();
178+ this.queue = new int[initialCapacity];
179+ }
180+
181+ public Heap(int[] arr) {
182+ size = arr.length;
183+ queue = new int[arr.length + 1];
184+ int i = 1;
185+ for (int val : arr) {
186+ queue[i++] = val;
187+ }
188+ }
189+
190+ public void shiftDown(int i) {
191+
192+ int temp = queue[i];
193+
194+ while ((i << 1) <= size) {
195+ int child = i << 1;
196+ // child!=size 判断当前元素是否包含右节点
197+ if (child != size && queue[child + 1] < queue[child]) {
198+ child++;
199+ }
200+ if (temp > queue[child]) {
201+ queue[i] = queue[child];
202+ i = child;
203+ } else {
204+ break;
205+ }
206+ }
207+ queue[i] = temp;
208+ }
209+
210+
211+ public void shiftUp(int i) {
212+ int temp = queue[i];
213+ while ((i >> 1) > 0) {
214+ if (temp < queue[i >> 1]) {
215+ queue[i] = queue[i >> 1];
216+ i >>= 1;
217+ } else {
218+ break;
219+ }
220+ }
221+ queue[i] = temp;
222+ }
223+
224+ public int peek() {
225+
226+ int res = queue[1];
227+ return res;
228+ }
229+
230+ public int pop() {
231+
232+ int res = queue[1];
233+
234+ queue[1] = queue[size--];
235+ shiftDown(1);
236+ return res;
237+ }
238+
239+ public void push(int val) {
240+ if (size == queue.length - 1) {
241+ queue = Arrays.copyOf(queue, size << 1+1);
242+ }
243+ queue[++size] = val;
244+ shiftUp(size);
245+ }
246+
247+ public void buildHeap() {
248+ for (int i = size >> 1; i >= 0; i--) {
249+ shiftDown(i);
250+ }
251+ }
252+
253+ public static void main(String[] args) {
254+ int arr[] = new int[]{2,7,4,1,8,1};
255+ Heap heap = new Heap(arr);
256+ heap.buildHeap();
257+ System.out.println(heap.peek());
258+ heap.push(5);
259+ while (heap.size > 0) {
260+ int num = heap.pop();
261+ System.out.printf(num + "");
262+ }
263+ }
264+ }
265+
266+
267+ ` ;
95268module . exports = {
96269 title : "堆" ,
97270 list : [
98271 {
99- text : "标准堆 " ,
272+ text : "小顶堆 " ,
100273 problems : [ ] ,
101274 codes : [
102275 {
103276 language : "js" ,
104- text : minHeapCode ,
277+ text : minHeapJSCode ,
278+ } ,
279+ {
280+ language : "py" ,
281+ text : minHeapPythonCode ,
282+ } ,
283+ {
284+ language : "java" ,
285+ text : minHeapJavaCode ,
105286 } ,
106287 ] ,
107288 } ,
0 commit comments