refactor: Replace 'poll' with 'pop' in Heap (#416)

This commit is contained in:
Yudong Jin
2023-03-13 22:31:05 +08:00
committed by GitHub
parent 8aebbaad21
commit 28aacccf44
27 changed files with 91 additions and 152 deletions

View File

@ -12,12 +12,12 @@ import java.util.*;
public class heap {
public static void testPush(Queue<Integer> heap, int val) {
heap.add(val); // 元素入堆
heap.offer(val); // 元素入堆
System.out.format("\n元素 %d 入堆后\n", val);
PrintUtil.printHeap(heap);
}
public static void testPoll(Queue<Integer> heap) {
public static void testPop(Queue<Integer> heap) {
int val = heap.poll(); // 堆顶元素出堆
System.out.format("\n堆顶元素 %d 出堆后\n", val);
PrintUtil.printHeap(heap);
@ -44,11 +44,11 @@ public class heap {
System.out.format("\n堆顶元素为 %d\n", peek);
/* 堆顶元素出堆 */
testPoll(maxHeap);
testPoll(maxHeap);
testPoll(maxHeap);
testPoll(maxHeap);
testPoll(maxHeap);
testPop(maxHeap);
testPop(maxHeap);
testPop(maxHeap);
testPop(maxHeap);
testPop(maxHeap);
/* 获取堆大小 */
int size = maxHeap.size();

View File

@ -87,7 +87,7 @@ class MaxHeap {
}
/* 元素出堆 */
public int poll() {
public int pop() {
// 判空处理
if (isEmpty())
throw new EmptyStackException();
@ -129,18 +129,6 @@ class MaxHeap {
public class my_heap {
public static void testPush(MaxHeap maxHeap, int val) {
maxHeap.push(val); // 元素入堆
System.out.format("\n添加元素 %d 后\n", val);
maxHeap.print();
}
public static void testPoll(MaxHeap maxHeap) {
int val = maxHeap.poll(); // 堆顶元素出堆
System.out.format("\n出堆元素为 %d\n", val);
maxHeap.print();
}
public static void main(String[] args) {
/* 初始化大顶堆 */
MaxHeap maxHeap = new MaxHeap(Arrays.asList(9, 8, 6, 6, 7, 5, 2, 1, 4, 3, 6, 2));
@ -158,7 +146,7 @@ public class my_heap {
maxHeap.print();
/* 堆顶元素出堆 */
peek = maxHeap.poll();
peek = maxHeap.pop();
System.out.format("\n堆顶元素 %d 出堆后\n", peek);
maxHeap.print();