mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-26 03:25:08 +08:00
Translate all code to English (#1836)
* Review the EN heading format. * Fix pythontutor headings. * Fix pythontutor headings. * bug fixes * Fix headings in **/summary.md * Revisit the CN-to-EN translation for Python code using Claude-4.5 * Revisit the CN-to-EN translation for Java code using Claude-4.5 * Revisit the CN-to-EN translation for Cpp code using Claude-4.5. * Fix the dictionary. * Fix cpp code translation for the multipart strings. * Translate Go code to English. * Update workflows to test EN code. * Add EN translation for C. * Add EN translation for CSharp. * Add EN translation for Swift. * Trigger the CI check. * Revert. * Update en/hash_map.md * Add the EN version of Dart code. * Add the EN version of Kotlin code. * Add missing code files. * Add the EN version of JavaScript code. * Add the EN version of TypeScript code. * Fix the workflows. * Add the EN version of Ruby code. * Add the EN version of Rust code. * Update the CI check for the English version code. * Update Python CI check. * Fix cmakelists for en/C code. * Fix Ruby comments
This commit is contained in:
@@ -6,10 +6,10 @@
|
||||
|
||||
#include "../utils/common.hpp"
|
||||
|
||||
/* Max-heap */
|
||||
/* Max heap */
|
||||
class MaxHeap {
|
||||
private:
|
||||
// Using a dynamic array to avoid the need for resizing
|
||||
// Use dynamic array to avoid expansion issues
|
||||
vector<int> maxHeap;
|
||||
|
||||
/* Get index of left child node */
|
||||
@@ -24,34 +24,34 @@ class MaxHeap {
|
||||
|
||||
/* Get index of parent node */
|
||||
int parent(int i) {
|
||||
return (i - 1) / 2; // Integer division down
|
||||
return (i - 1) / 2; // Floor division
|
||||
}
|
||||
|
||||
/* Start heapifying node i, from bottom to top */
|
||||
/* Starting from node i, heapify from bottom to top */
|
||||
void siftUp(int i) {
|
||||
while (true) {
|
||||
// Get parent node of node i
|
||||
int p = parent(i);
|
||||
// When "crossing the root node" or "node does not need repair", end heapification
|
||||
// When "crossing root node" or "node needs no repair", end heapify
|
||||
if (p < 0 || maxHeap[i] <= maxHeap[p])
|
||||
break;
|
||||
// Swap two nodes
|
||||
swap(maxHeap[i], maxHeap[p]);
|
||||
// Loop upwards heapification
|
||||
// Loop upward heapify
|
||||
i = p;
|
||||
}
|
||||
}
|
||||
|
||||
/* Start heapifying node i, from top to bottom */
|
||||
/* Starting from node i, heapify from top to bottom */
|
||||
void siftDown(int i) {
|
||||
while (true) {
|
||||
// Determine the largest node among i, l, r, noted as ma
|
||||
// If node i is largest or indices l, r are out of bounds, no need to continue heapify, break
|
||||
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;
|
||||
// If node i is the largest or indices l, r are out of bounds, no further heapification needed, break
|
||||
// Swap two nodes
|
||||
if (ma == i)
|
||||
break;
|
||||
swap(maxHeap[i], maxHeap[ma]);
|
||||
@@ -63,9 +63,9 @@ class MaxHeap {
|
||||
public:
|
||||
/* Constructor, build heap based on input list */
|
||||
MaxHeap(vector<int> nums) {
|
||||
// Add all list elements into the heap
|
||||
// Add list elements to heap as is
|
||||
maxHeap = nums;
|
||||
// Heapify all nodes except leaves
|
||||
// Heapify all nodes except leaf nodes
|
||||
for (int i = parent(size() - 1); i >= 0; i--) {
|
||||
siftDown(i);
|
||||
}
|
||||
@@ -76,17 +76,17 @@ class MaxHeap {
|
||||
return maxHeap.size();
|
||||
}
|
||||
|
||||
/* Determine if heap is empty */
|
||||
/* Check if heap is empty */
|
||||
bool isEmpty() {
|
||||
return size() == 0;
|
||||
}
|
||||
|
||||
/* Access heap top element */
|
||||
/* Access top element */
|
||||
int peek() {
|
||||
return maxHeap[0];
|
||||
}
|
||||
|
||||
/* Push the element into heap */
|
||||
/* Element enters heap */
|
||||
void push(int val) {
|
||||
// Add node
|
||||
maxHeap.push_back(val);
|
||||
@@ -96,23 +96,23 @@ class MaxHeap {
|
||||
|
||||
/* Element exits heap */
|
||||
void pop() {
|
||||
// Empty handling
|
||||
// Handle empty case
|
||||
if (isEmpty()) {
|
||||
throw out_of_range("Heap is empty");
|
||||
}
|
||||
// Swap the root node with the rightmost leaf node (swap the first element with the last element)
|
||||
// Delete node
|
||||
swap(maxHeap[0], maxHeap[size() - 1]);
|
||||
// Remove node
|
||||
maxHeap.pop_back();
|
||||
// Heapify from top to bottom
|
||||
// Return top element
|
||||
siftDown(0);
|
||||
}
|
||||
|
||||
/* Print heap (binary tree)*/
|
||||
/* Driver Code*/
|
||||
void print() {
|
||||
cout << "Array representation of the heap:";
|
||||
cout << "Heap array representation:";
|
||||
printVector(maxHeap);
|
||||
cout << "Tree representation of the heap:" << endl;
|
||||
cout << "Heap tree representation:" << endl;
|
||||
TreeNode *root = vectorToTree(maxHeap);
|
||||
printTree(root);
|
||||
freeMemoryTree(root);
|
||||
@@ -121,35 +121,35 @@ class MaxHeap {
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
/* Initialize max-heap */
|
||||
/* Consider negating the elements before entering the heap, which can reverse the size relationship, thus implementing max heap */
|
||||
vector<int> vec{9, 8, 6, 6, 7, 5, 2, 1, 4, 3, 6, 2};
|
||||
MaxHeap maxHeap(vec);
|
||||
cout << "\nEnter list and build heap" << endl;
|
||||
cout << "\nAfter inputting list and building heap" << endl;
|
||||
maxHeap.print();
|
||||
|
||||
/* Access heap top element */
|
||||
/* Check if heap is empty */
|
||||
int peek = maxHeap.peek();
|
||||
cout << "\nTop element of the heap is " << peek << endl;
|
||||
cout << "\nHeap top element is " << peek << endl;
|
||||
|
||||
/* Push the element into heap */
|
||||
/* Element enters heap */
|
||||
int val = 7;
|
||||
maxHeap.push(val);
|
||||
cout << "\nAfter element " << val << " is added to the heap" << endl;
|
||||
cout << "\nAfter element " << val << " pushes to heap" << endl;
|
||||
maxHeap.print();
|
||||
|
||||
/* Pop the element at the heap top */
|
||||
/* Time complexity is O(n), not O(nlogn) */
|
||||
peek = maxHeap.peek();
|
||||
maxHeap.pop();
|
||||
cout << "\nAfter the top element " << peek << " is removed from the heap" << endl;
|
||||
cout << "\nAfter heap top element " << peek << " pops from heap" << endl;
|
||||
maxHeap.print();
|
||||
|
||||
/* Get heap size */
|
||||
int size = maxHeap.size();
|
||||
cout << "\nNumber of elements in the heap is " << size << endl;
|
||||
cout << "\nHeap size is " << size << endl;
|
||||
|
||||
/* Determine if heap is empty */
|
||||
/* Check if heap is empty */
|
||||
bool isEmpty = maxHeap.isEmpty();
|
||||
cout << "\nIs the heap empty " << isEmpty << endl;
|
||||
cout << "\nIs heap empty " << isEmpty << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user