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

@@ -86,7 +86,7 @@ int push(maxHeap *h, int val) {
}
/* 元素出堆 */
int poll(maxHeap *h) {
int pop(maxHeap *h) {
// 判空处理
if (isEmpty(h)) {
printf("heap is empty!");
@@ -162,7 +162,7 @@ int main() {
printHeap(heap->data, heap->size);
/* 堆顶元素出堆 */
int top = poll(heap);
int top = pop(heap);
printf("\n堆顶元素 %d 出堆后\n", top);
printHeap(heap->data, heap->size);

View File

@@ -13,7 +13,7 @@ void testPush(priority_queue<int> &heap, int val)
PrintUtil::printHeap(heap);
}
void testPoll(priority_queue<int> &heap)
void testPop(priority_queue<int> &heap)
{
int val = heap.top();
heap.pop();
@@ -43,11 +43,11 @@ int main()
cout << "\n堆顶元素为 " << peek << endl;
/* 堆顶元素出堆 */
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

@@ -96,7 +96,7 @@ public:
}
/* 元素出堆 */
void poll() {
void pop() {
// 判空处理
if (empty()) {
throw out_of_range("堆为空");
@@ -121,48 +121,35 @@ public:
};
void testPush(MaxHeap maxHeap, int val) {
maxHeap.push(val);
cout << "\n添加元素 " << val << "" << endl;
maxHeap.print();
}
void testPop(MaxHeap maxHeap) {
int val = maxHeap.peek();
maxHeap.poll();
cout << "\n出堆元素为 " << val << endl;
maxHeap.print();
}
/* Driver Code */
int main() {
/* 初始化大顶堆 */
vector<int> vec{9, 8, 6, 6, 7, 5, 2, 1, 4, 3, 6, 2};
MaxHeap maxheap(vec);
MaxHeap maxHeap(vec);
cout << "\n输入列表并建堆后" << endl;
maxheap.print();
maxHeap.print();
/* 获取堆顶元素 */
int peek = maxheap.peek();
int peek = maxHeap.peek();
cout << "\n堆顶元素为 " << peek << endl;
/* 元素入堆 */
int val = 7;
maxheap.push(val);
maxHeap.push(val);
cout << "\n元素 " << val << " 入堆后" << endl;
maxheap.print();
maxHeap.print();
/* 堆顶元素出堆 */
peek = maxheap.peek();
maxheap.poll();
peek = maxHeap.peek();
maxHeap.pop();
cout << "\n堆顶元素 " << peek << " 出堆后" << endl;
maxheap.print();
maxHeap.print();
/* 获取堆大小 */
int size = maxheap.size();
int size = maxHeap.size();
cout << "\n堆元素数量为 " << size << endl;
/* 判断堆是否为空 */
bool isEmpty = maxheap.empty();
bool isEmpty = maxHeap.empty();
cout << "\n堆是否为空 " << isEmpty << endl;
}

View File

@@ -18,7 +18,7 @@ public class heap
PrintUtil.printHeap(heap);
}
public void testPoll(PriorityQueue<int, int> heap)
public void testPop(PriorityQueue<int, int> heap)
{
int val = heap.Dequeue(); // 堆顶元素出堆
Console.WriteLine($"\n堆顶元素 {val} 出堆后\n");
@@ -47,11 +47,11 @@ public class heap
/* 堆顶元素出堆 */
// 出堆元素会形成一个从大到小的序列
testPoll(maxHeap);
testPoll(maxHeap);
testPoll(maxHeap);
testPoll(maxHeap);
testPoll(maxHeap);
testPop(maxHeap);
testPop(maxHeap);
testPop(maxHeap);
testPop(maxHeap);
testPop(maxHeap);
/* 获取堆大小 */
int size = maxHeap.Count;

View File

@@ -97,7 +97,7 @@ class MaxHeap
}
/* 元素出堆 */
public int poll()
public int pop()
{
// 判空处理
if (isEmpty())
@@ -168,7 +168,7 @@ public class my_heap
maxHeap.print();
/* 堆顶元素出堆 */
peek = maxHeap.poll();
peek = maxHeap.pop();
Console.WriteLine($"堆顶元素 {peek} 出堆后");
maxHeap.print();

View File

@@ -76,7 +76,7 @@ func TestMyHeap(t *testing.T) {
maxHeap.print()
/* 堆顶元素出堆 */
peek = maxHeap.poll()
peek = maxHeap.pop()
fmt.Printf("\n堆顶元素 %d 出堆后\n", peek)
maxHeap.print()

View File

@@ -94,7 +94,7 @@ func (h *maxHeap) siftUp(i int) {
}
/* 元素出堆 */
func (h *maxHeap) poll() any {
func (h *maxHeap) pop() any {
// 判空处理
if h.isEmpty() {
fmt.Println("error")

View File

@@ -18,7 +18,7 @@ func levelOrder(root *TreeNode) []int {
// 初始化一个切片,用于保存遍历序列
nums := make([]int, 0)
for queue.Len() > 0 {
// poll
// 队首元素出队
node := queue.Remove(queue.Front()).(*TreeNode)
// 保存结点值
nums = append(nums, node.Val)

View File

@@ -36,7 +36,7 @@ func ArrToTree(arr []any) *TreeNode {
queue.PushBack(root)
i := 0
for queue.Len() > 0 {
// poll
// 队首元素出队
node := queue.Remove(queue.Front()).(*TreeNode)
i++
if i < len(arr) {

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();

View File

@@ -82,7 +82,7 @@ class MaxHeap {
}
/* 元素出堆 */
poll() {
pop() {
// 判空处理
if (this.isEmpty()) throw new Error("堆为空");
// 交换根结点与最右叶结点(即交换首元素与尾元素)
@@ -119,18 +119,6 @@ class MaxHeap {
}
}
function testPush(maxHeap, val) {
maxHeap.push(val); // 元素入堆
console.log(`\n添加元素 ${val}`);
maxHeap.print();
}
function testPoll(maxHeap) {
let val = maxHeap.poll(); // 堆顶元素出堆
console.log(`\n出堆元素为 ${val}`);
maxHeap.print();
}
/* Driver Code */
/* 初始化大顶堆 */
const maxHeap = new MaxHeap([9, 8, 6, 6, 7, 5, 2, 1, 4, 3, 6, 2]);
@@ -148,7 +136,7 @@ console.log(`\n元素 ${val} 入堆后`);
maxHeap.print();
/* 堆顶元素出堆 */
peek = maxHeap.poll();
peek = maxHeap.pop();
console.log(`\n堆顶元素 ${peek} 出堆后`);
maxHeap.print();

View File

@@ -67,7 +67,7 @@ class MaxHeap:
# 循环向上堆化
i = p
def poll(self) -> int:
def pop(self) -> int:
""" 元素出堆 """
# 判空处理
assert not self.is_empty()
@@ -102,18 +102,6 @@ class MaxHeap:
print_heap(self.max_heap)
def test_push(max_heap: MaxHeap, val: int):
max_heap.push(val) # 元素入堆
print(f"\n添加元素 {val}\n")
max_heap.print()
def test_poll(max_heap: MaxHeap):
val = max_heap.poll() # 堆顶元素出堆
print(f"\n出堆元素为 {val}\n")
max_heap.print()
""" Driver Code """
if __name__ == "__main__":
# 初始化大顶堆
@@ -132,7 +120,7 @@ if __name__ == "__main__":
max_heap.print()
# 堆顶元素出堆
peek = max_heap.poll()
peek = max_heap.pop()
print(f"\n堆顶元素 {peek} 出堆后")
max_heap.print()

View File

@@ -81,7 +81,7 @@ class MaxHeap {
}
/* */
func poll() -> Int {
func pop() -> Int {
//
if isEmpty() {
fatalError("堆为空")
@@ -148,7 +148,7 @@ enum MyHeap {
maxHeap.print()
/* */
peek = maxHeap.poll()
peek = maxHeap.pop()
print("\n堆顶元素 \(peek) 出堆后")
maxHeap.print()

View File

@@ -81,7 +81,7 @@ class MaxHeap {
}
/* 元素出堆 */
public poll(): number {
public pop(): number {
// 判空处理
if (this.isEmpty()) throw new RangeError('Heap is empty.');
// 交换根结点与最右叶结点(即交换首元素与尾元素)
@@ -118,18 +118,6 @@ class MaxHeap {
}
}
function testPush(maxHeap: MaxHeap, val: number): void {
maxHeap.push(val); // 元素入堆
console.log(`\n添加元素 ${val}`);
maxHeap.print();
}
function testPoll(maxHeap: MaxHeap): void {
const val = maxHeap.poll(); // 堆顶元素出堆
console.log(`\n出堆元素为 ${val}`);
maxHeap.print();
}
/* Driver Code */
/* 初始化大顶堆 */
const maxHeap = new MaxHeap([9, 8, 6, 6, 7, 5, 2, 1, 4, 3, 6, 2]);
@@ -147,7 +135,7 @@ console.log(`\n元素 ${val} 入堆后`);
maxHeap.print();
/* 堆顶元素出堆 */
peek = maxHeap.poll();
peek = maxHeap.pop();
console.log(`\n堆顶元素 ${peek} 出堆后`);
maxHeap.print();

View File

@@ -94,7 +94,7 @@ pub fn MaxHeap(comptime T: type) type {
}
// 元素出堆
pub fn poll(self: *Self) !T {
pub fn pop(self: *Self) !T {
// 判断处理
if (self.isEmpty()) unreachable;
// 交换根结点与最右叶结点(即交换首元素与尾元素)
@@ -171,7 +171,7 @@ pub fn main() !void {
try maxHeap.print(mem_allocator);
// 堆顶元素出堆
peek = try maxHeap.poll();
peek = try maxHeap.pop();
std.debug.print("\n堆顶元素 {} 出堆后\n", .{peek});
try maxHeap.print(mem_allocator);