mirror of
https://github.com/krahets/hello-algo.git
synced 2026-11-28 23:34:43 +08:00
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:
@@ -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;
|
||||
|
||||
@@ -9,147 +9,147 @@
|
||||
/* Max heap */
|
||||
class MaxHeap {
|
||||
private:
|
||||
// Use dynamic array to avoid expansion issues
|
||||
// Use a dynamic array so capacity growth is handled automatically
|
||||
vector<int> maxHeap;
|
||||
|
||||
/* Get index of left child node */
|
||||
/* Get the left child index */
|
||||
int left(int i) {
|
||||
return 2 * i + 1;
|
||||
}
|
||||
|
||||
/* Get index of right child node */
|
||||
/* Get the right child index */
|
||||
int right(int i) {
|
||||
return 2 * i + 2;
|
||||
}
|
||||
|
||||
/* Get index of parent node */
|
||||
/* Get the parent index */
|
||||
int parent(int i) {
|
||||
return (i - 1) / 2; // Floor division
|
||||
return (i - 1) / 2; // Integer division truncates toward zero
|
||||
}
|
||||
|
||||
/* Starting from node i, heapify from bottom to top */
|
||||
/* Sift up starting from node i */
|
||||
void siftUp(int i) {
|
||||
while (true) {
|
||||
// Get parent node of node i
|
||||
// Get the parent of node i
|
||||
int p = parent(i);
|
||||
// When "crossing root node" or "node needs no repair", end heapify
|
||||
// Stop sifting when the root has been passed or the node needs no repair
|
||||
if (p < 0 || maxHeap[i] <= maxHeap[p])
|
||||
break;
|
||||
// Swap two nodes
|
||||
// Swap the two nodes
|
||||
swap(maxHeap[i], maxHeap[p]);
|
||||
// Loop upward heapify
|
||||
// Continue sifting up
|
||||
i = p;
|
||||
}
|
||||
}
|
||||
|
||||
/* Starting from node i, heapify from top to bottom */
|
||||
/* Sift down starting from node i */
|
||||
void siftDown(int i) {
|
||||
while (true) {
|
||||
// If node i is largest or indices l, r are out of bounds, no need to continue heapify, break
|
||||
// Find the node with the largest value among i, l, and r, and store its index in ma
|
||||
int l = left(i), r = right(i), ma = i;
|
||||
if (l < size() && maxHeap[l] > maxHeap[ma])
|
||||
ma = l;
|
||||
if (r < size() && maxHeap[r] > maxHeap[ma])
|
||||
ma = r;
|
||||
// Swap two nodes
|
||||
// Stop if node i is largest or both child indices l and r are out of bounds
|
||||
if (ma == i)
|
||||
break;
|
||||
swap(maxHeap[i], maxHeap[ma]);
|
||||
// Loop downwards heapification
|
||||
// Continue sifting down
|
||||
i = ma;
|
||||
}
|
||||
}
|
||||
|
||||
public:
|
||||
/* Constructor, build heap based on input list */
|
||||
/* Constructor: build a heap from the input list */
|
||||
MaxHeap(vector<int> nums) {
|
||||
// Add list elements to heap as is
|
||||
// Place the list elements in the heap without changing their order
|
||||
maxHeap = nums;
|
||||
// Heapify all nodes except leaf nodes
|
||||
// Sift down from every non-leaf node
|
||||
for (int i = parent(size() - 1); i >= 0; i--) {
|
||||
siftDown(i);
|
||||
}
|
||||
}
|
||||
|
||||
/* Get heap size */
|
||||
/* Get the heap size */
|
||||
int size() {
|
||||
return maxHeap.size();
|
||||
}
|
||||
|
||||
/* Check if heap is empty */
|
||||
/* Check whether the heap is empty */
|
||||
bool isEmpty() {
|
||||
return size() == 0;
|
||||
}
|
||||
|
||||
/* Access top element */
|
||||
/* Access the top element of the heap */
|
||||
int peek() {
|
||||
return maxHeap[0];
|
||||
}
|
||||
|
||||
/* Element enters heap */
|
||||
/* Push an element onto the heap */
|
||||
void push(int val) {
|
||||
// Add node
|
||||
// Add a node
|
||||
maxHeap.push_back(val);
|
||||
// Heapify from bottom to top
|
||||
// Sift up
|
||||
siftUp(size() - 1);
|
||||
}
|
||||
|
||||
/* Element exits heap */
|
||||
/* Pop an element from the heap */
|
||||
void pop() {
|
||||
// Handle empty case
|
||||
// Handle an empty heap
|
||||
if (isEmpty()) {
|
||||
throw out_of_range("Heap is empty");
|
||||
}
|
||||
// Delete node
|
||||
// Swap the root with the rightmost leaf on the last level (swap the first and last elements)
|
||||
swap(maxHeap[0], maxHeap[size() - 1]);
|
||||
// Remove node
|
||||
// Remove a node
|
||||
maxHeap.pop_back();
|
||||
// Return top element
|
||||
// Sift down
|
||||
siftDown(0);
|
||||
}
|
||||
|
||||
/* Driver Code*/
|
||||
/* Print the heap (binary tree)*/
|
||||
void print() {
|
||||
cout << "Heap array representation:";
|
||||
cout << "Array representation of the heap:";
|
||||
printVector(maxHeap);
|
||||
cout << "Heap tree representation:" << endl;
|
||||
cout << "Tree representation of the heap:" << endl;
|
||||
TreeNode *root = vectorToTree(maxHeap);
|
||||
printTree(root);
|
||||
freeMemoryTree(root);
|
||||
}
|
||||
};
|
||||
|
||||
/* Driver Code */
|
||||
/* Driver code */
|
||||
int main() {
|
||||
/* Consider negating the elements before entering the heap, which can reverse the size relationship, thus implementing max heap */
|
||||
/* Initialize a max heap */
|
||||
vector<int> vec{9, 8, 6, 6, 7, 5, 2, 1, 4, 3, 6, 2};
|
||||
MaxHeap maxHeap(vec);
|
||||
cout << "\nAfter inputting list and building heap" << endl;
|
||||
cout << "\nAfter building a heap from the input list:" << endl;
|
||||
maxHeap.print();
|
||||
|
||||
/* Check if heap is empty */
|
||||
/* Get the top element of the heap */
|
||||
int peek = maxHeap.peek();
|
||||
cout << "\nHeap top element is " << peek << endl;
|
||||
cout << "\nTop element of the heap: " << peek << endl;
|
||||
|
||||
/* Element enters heap */
|
||||
/* Push an element onto the heap */
|
||||
int val = 7;
|
||||
maxHeap.push(val);
|
||||
cout << "\nAfter element " << val << " pushes to heap" << endl;
|
||||
cout << "\nAfter pushing element " << val << " onto the heap:" << endl;
|
||||
maxHeap.print();
|
||||
|
||||
/* Time complexity is O(n), not O(nlogn) */
|
||||
/* Pop the top element from the heap */
|
||||
peek = maxHeap.peek();
|
||||
maxHeap.pop();
|
||||
cout << "\nAfter heap top element " << peek << " pops from heap" << endl;
|
||||
cout << "\nAfter popping top element " << peek << " from the heap:" << endl;
|
||||
maxHeap.print();
|
||||
|
||||
/* 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.isEmpty();
|
||||
cout << "\nIs heap empty " << isEmpty << endl;
|
||||
cout << "\nIs the heap empty: " << isEmpty << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -6,17 +6,17 @@
|
||||
|
||||
#include "../utils/common.hpp"
|
||||
|
||||
/* Find the largest k elements in array based on heap */
|
||||
/* Use a heap to find the k largest elements in an array */
|
||||
priority_queue<int, vector<int>, greater<int>> topKHeap(vector<int> &nums, int k) {
|
||||
// Python's heapq module implements min heap by default
|
||||
// Initialize a min heap
|
||||
priority_queue<int, vector<int>, greater<int>> heap;
|
||||
// Enter the first k elements of array into heap
|
||||
// Push the first k array elements onto the heap
|
||||
for (int i = 0; i < k; i++) {
|
||||
heap.push(nums[i]);
|
||||
}
|
||||
// Starting from the (k+1)th element, maintain heap length as k
|
||||
// Starting with element k+1, keep the heap size at k
|
||||
for (int i = k; i < nums.size(); i++) {
|
||||
// If current element is greater than top element, top element exits heap, current element enters heap
|
||||
// If the current element is larger than the heap top, pop the top element and push the current element
|
||||
if (nums[i] > heap.top()) {
|
||||
heap.pop();
|
||||
heap.push(nums[i]);
|
||||
@@ -25,7 +25,7 @@ priority_queue<int, vector<int>, greater<int>> topKHeap(vector<int> &nums, int k
|
||||
return heap;
|
||||
}
|
||||
|
||||
// Driver Code
|
||||
// Driver code
|
||||
int main() {
|
||||
vector<int> nums = {1, 7, 6, 3, 2};
|
||||
int k = 3;
|
||||
|
||||
Reference in New Issue
Block a user