Retranslate the complete English edition from Simplified Chinese (#2010)

* Retranslate the complete English edition from Simplified Chinese

* Polish three English explanations after reader-focused review
This commit is contained in:
Yudong Jin
2026-10-31 03:35:51 +08:00
committed by GitHub
parent ef46e2ed95
commit 14c314c08c
1502 changed files with 29459 additions and 23517 deletions

View File

@@ -7,59 +7,59 @@
#include "../utils/common.hpp"
void testPush(priority_queue<int> &heap, int val) {
heap.push(val); // Element enters heap
cout << "\nAfter element " << val << " pushes to heap" << endl;
heap.push(val); // Push an element onto the heap
cout << "\nAfter pushing element " << val << " onto the heap:" << endl;
printHeap(heap);
}
void testPop(priority_queue<int> &heap) {
int val = heap.top();
heap.pop();
cout << "\nAfter heap top element " << val << " pops from heap" << endl;
cout << "\nAfter popping top element " << val << " from the heap:" << endl;
printHeap(heap);
}
/* Driver Code */
/* Driver code */
int main() {
/* Initialize heap */
// Python's heapq module implements min heap by default
/* Initialize the heap */
// Initialize a min heap
// priority_queue<int, vector<int>, greater<int>> minHeap;
// Consider negating the elements before entering the heap, which can reverse the size relationship, thus implementing max heap
// Initialize a max heap
priority_queue<int, vector<int>, less<int>> maxHeap;
cout << "\nThe following test cases are for max heap" << endl;
cout << "\nThe following example uses a max heap" << endl;
/* Element enters heap */
/* Push an element onto the heap */
testPush(maxHeap, 1);
testPush(maxHeap, 3);
testPush(maxHeap, 2);
testPush(maxHeap, 5);
testPush(maxHeap, 4);
/* Check if heap is empty */
/* Get the top element of the heap */
int peek = maxHeap.top();
cout << "\nHeap top element is " << peek << endl;
cout << "\nTop element of the heap: " << peek << endl;
/* Time complexity is O(n), not O(nlogn) */
/* Pop the top element from the heap */
testPop(maxHeap);
testPop(maxHeap);
testPop(maxHeap);
testPop(maxHeap);
testPop(maxHeap);
/* Get heap size */
/* Get the heap size */
int size = maxHeap.size();
cout << "\nHeap size is " << size << endl;
cout << "\nNumber of elements in the heap: " << size << endl;
/* Check if heap is empty */
/* Check whether the heap is empty */
bool isEmpty = maxHeap.empty();
cout << "\nIs heap empty " << isEmpty << endl;
cout << "\nIs the heap empty: " << isEmpty << endl;
/* Input list and build heap */
// Time complexity is O(n), not O(nlogn)
/* Build a heap from an input list */
// The time complexity is O(n), not O(nlogn)
vector<int> input{1, 3, 2, 5, 4};
priority_queue<int, vector<int>, greater<int>> minHeap(input.begin(), input.end());
cout << "After input list and building min heap" << endl;
cout << "After building a min heap from the input list:" << endl;
printHeap(minHeap);
return 0;