mirror of
https://github.com/krahets/hello-algo.git
synced 2026-09-02 05:07:13 +00: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:
@@ -0,0 +1,71 @@
|
||||
/*
|
||||
* File: heap.rs
|
||||
* Created Time: 2023-07-16
|
||||
* Author: night-cruise (2586447362@qq.com)
|
||||
*/
|
||||
|
||||
use hello_algo_rust::include::print_util;
|
||||
|
||||
use std::{cmp::Reverse, collections::BinaryHeap};
|
||||
|
||||
fn test_push_max(heap: &mut BinaryHeap<i32>, val: i32) {
|
||||
heap.push(val); // Element enters heap
|
||||
println!("\nAfter element {} pushes to heap", val);
|
||||
print_util::print_heap(heap.iter().map(|&val| val).collect());
|
||||
}
|
||||
|
||||
fn test_pop_max(heap: &mut BinaryHeap<i32>) {
|
||||
let val = heap.pop().unwrap();
|
||||
println!("\nAfter heap top element {} pops from heap", val);
|
||||
print_util::print_heap(heap.iter().map(|&val| val).collect());
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
fn main() {
|
||||
/* Initialize heap */
|
||||
// Python's heapq module implements min heap by default
|
||||
#[allow(unused_assignments)]
|
||||
let mut min_heap = BinaryHeap::new();
|
||||
// Rust's BinaryHeap is a max heap, min heap typically wraps elements with Reverse
|
||||
// Consider negating the elements before entering the heap, which can reverse the size relationship, thus implementing max heap
|
||||
let mut max_heap = BinaryHeap::new();
|
||||
|
||||
println!("\nThe following test cases are for max heap");
|
||||
|
||||
/* Element enters heap */
|
||||
test_push_max(&mut max_heap, 1);
|
||||
test_push_max(&mut max_heap, 3);
|
||||
test_push_max(&mut max_heap, 2);
|
||||
test_push_max(&mut max_heap, 5);
|
||||
test_push_max(&mut max_heap, 4);
|
||||
|
||||
/* Check if heap is empty */
|
||||
let peek = max_heap.peek().unwrap();
|
||||
println!("\nHeap top element is {}", peek);
|
||||
|
||||
/* Time complexity is O(n), not O(nlogn) */
|
||||
test_pop_max(&mut max_heap);
|
||||
test_pop_max(&mut max_heap);
|
||||
test_pop_max(&mut max_heap);
|
||||
test_pop_max(&mut max_heap);
|
||||
test_pop_max(&mut max_heap);
|
||||
|
||||
/* Get heap size */
|
||||
let size = max_heap.len();
|
||||
println!("\nHeap size is {}", size);
|
||||
|
||||
/* Check if heap is empty */
|
||||
let is_empty = max_heap.is_empty();
|
||||
println!("\nIs heap empty {}", is_empty);
|
||||
|
||||
/* Input list and build heap */
|
||||
// Time complexity is O(n), not O(nlogn)
|
||||
min_heap = BinaryHeap::from(
|
||||
vec![1, 3, 2, 5, 4]
|
||||
.into_iter()
|
||||
.map(|val| Reverse(val))
|
||||
.collect::<Vec<Reverse<i32>>>(),
|
||||
);
|
||||
println!("\nAfter inputting list and building min heap");
|
||||
print_util::print_heap(min_heap.iter().map(|&val| val.0).collect());
|
||||
}
|
||||
@@ -0,0 +1,165 @@
|
||||
/*
|
||||
* File: my_heap.rs
|
||||
* Created Time: 2023-07-16
|
||||
* Author: night-cruise (2586447362@qq.com)
|
||||
*/
|
||||
|
||||
use hello_algo_rust::include::print_util;
|
||||
|
||||
/* Max heap */
|
||||
struct MaxHeap {
|
||||
// Use vector instead of array to avoid capacity concerns
|
||||
max_heap: Vec<i32>,
|
||||
}
|
||||
|
||||
impl MaxHeap {
|
||||
/* Constructor, build heap based on input list */
|
||||
fn new(nums: Vec<i32>) -> Self {
|
||||
// Add list elements to heap as is
|
||||
let mut heap = MaxHeap { max_heap: nums };
|
||||
// Heapify all nodes except leaf nodes
|
||||
for i in (0..=Self::parent(heap.size() - 1)).rev() {
|
||||
heap.sift_down(i);
|
||||
}
|
||||
heap
|
||||
}
|
||||
|
||||
/* Get index of left child node */
|
||||
fn left(i: usize) -> usize {
|
||||
2 * i + 1
|
||||
}
|
||||
|
||||
/* Get index of right child node */
|
||||
fn right(i: usize) -> usize {
|
||||
2 * i + 2
|
||||
}
|
||||
|
||||
/* Get index of parent node */
|
||||
fn parent(i: usize) -> usize {
|
||||
(i - 1) / 2 // Floor division
|
||||
}
|
||||
|
||||
/* Swap elements */
|
||||
fn swap(&mut self, i: usize, j: usize) {
|
||||
self.max_heap.swap(i, j);
|
||||
}
|
||||
|
||||
/* Get heap size */
|
||||
fn size(&self) -> usize {
|
||||
self.max_heap.len()
|
||||
}
|
||||
|
||||
/* Check if heap is empty */
|
||||
fn is_empty(&self) -> bool {
|
||||
self.max_heap.is_empty()
|
||||
}
|
||||
|
||||
/* Access top element */
|
||||
fn peek(&self) -> Option<i32> {
|
||||
self.max_heap.first().copied()
|
||||
}
|
||||
|
||||
/* Element enters heap */
|
||||
fn push(&mut self, val: i32) {
|
||||
// Add node
|
||||
self.max_heap.push(val);
|
||||
// Heapify from bottom to top
|
||||
self.sift_up(self.size() - 1);
|
||||
}
|
||||
|
||||
/* Starting from node i, heapify from bottom to top */
|
||||
fn sift_up(&mut self, mut i: usize) {
|
||||
loop {
|
||||
// Node i is already the heap root, end heapification
|
||||
if i == 0 {
|
||||
break;
|
||||
}
|
||||
// Get parent node of node i
|
||||
let p = Self::parent(i);
|
||||
// When "node needs no repair", end heapification
|
||||
if self.max_heap[i] <= self.max_heap[p] {
|
||||
break;
|
||||
}
|
||||
// Swap two nodes
|
||||
self.swap(i, p);
|
||||
// Loop upward heapify
|
||||
i = p;
|
||||
}
|
||||
}
|
||||
|
||||
/* Element exits heap */
|
||||
fn pop(&mut self) -> i32 {
|
||||
// Handle empty case
|
||||
if self.is_empty() {
|
||||
panic!("index out of bounds");
|
||||
}
|
||||
// Delete node
|
||||
self.swap(0, self.size() - 1);
|
||||
// Remove node
|
||||
let val = self.max_heap.pop().unwrap();
|
||||
// Return top element
|
||||
self.sift_down(0);
|
||||
// Return heap top element
|
||||
val
|
||||
}
|
||||
|
||||
/* Starting from node i, heapify from top to bottom */
|
||||
fn sift_down(&mut self, mut i: usize) {
|
||||
loop {
|
||||
// If node i is largest or indices l, r are out of bounds, no need to continue heapify, break
|
||||
let (l, r, mut ma) = (Self::left(i), Self::right(i), i);
|
||||
if l < self.size() && self.max_heap[l] > self.max_heap[ma] {
|
||||
ma = l;
|
||||
}
|
||||
if r < self.size() && self.max_heap[r] > self.max_heap[ma] {
|
||||
ma = r;
|
||||
}
|
||||
// Swap two nodes
|
||||
if ma == i {
|
||||
break;
|
||||
}
|
||||
// Swap two nodes
|
||||
self.swap(i, ma);
|
||||
// Loop downwards heapification
|
||||
i = ma;
|
||||
}
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
fn print(&self) {
|
||||
print_util::print_heap(self.max_heap.clone());
|
||||
}
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
fn main() {
|
||||
/* Consider negating the elements before entering the heap, which can reverse the size relationship, thus implementing max heap */
|
||||
let mut max_heap = MaxHeap::new(vec![9, 8, 6, 6, 7, 5, 2, 1, 4, 3, 6, 2]);
|
||||
println!("\nAfter inputting list and building heap");
|
||||
max_heap.print();
|
||||
|
||||
/* Check if heap is empty */
|
||||
let peek = max_heap.peek();
|
||||
if let Some(peek) = peek {
|
||||
println!("\nHeap top element is {}", peek);
|
||||
}
|
||||
|
||||
/* Element enters heap */
|
||||
let val = 7;
|
||||
max_heap.push(val);
|
||||
println!("\nAfter element {} pushes to heap", val);
|
||||
max_heap.print();
|
||||
|
||||
/* Time complexity is O(n), not O(nlogn) */
|
||||
let peek = max_heap.pop();
|
||||
println!("\nAfter heap top element {} pops from heap", peek);
|
||||
max_heap.print();
|
||||
|
||||
/* Get heap size */
|
||||
let size = max_heap.size();
|
||||
println!("\nHeap size is {}", size);
|
||||
|
||||
/* Check if heap is empty */
|
||||
let is_empty = max_heap.is_empty();
|
||||
println!("\nIs heap empty {}", is_empty);
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* File: top_k.rs
|
||||
* Created Time: 2023-07-16
|
||||
* Author: night-cruise (2586447362@qq.com)
|
||||
*/
|
||||
|
||||
use hello_algo_rust::include::print_util;
|
||||
|
||||
use std::cmp::Reverse;
|
||||
use std::collections::BinaryHeap;
|
||||
|
||||
/* Find the largest k elements in array based on heap */
|
||||
fn top_k_heap(nums: Vec<i32>, k: usize) -> BinaryHeap<Reverse<i32>> {
|
||||
// BinaryHeap is a max heap, use Reverse to negate elements to implement min heap
|
||||
let mut heap = BinaryHeap::<Reverse<i32>>::new();
|
||||
// Enter the first k elements of array into heap
|
||||
for &num in nums.iter().take(k) {
|
||||
heap.push(Reverse(num));
|
||||
}
|
||||
// Starting from the (k+1)th element, maintain heap length as k
|
||||
for &num in nums.iter().skip(k) {
|
||||
// If current element is greater than top element, top element exits heap, current element enters heap
|
||||
if num > heap.peek().unwrap().0 {
|
||||
heap.pop();
|
||||
heap.push(Reverse(num));
|
||||
}
|
||||
}
|
||||
heap
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
fn main() {
|
||||
let nums = vec![1, 7, 6, 3, 2];
|
||||
let k = 3;
|
||||
|
||||
let res = top_k_heap(nums, k);
|
||||
println!("The largest {} elements are", k);
|
||||
print_util::print_heap(res.into_iter().map(|item| item.0).collect());
|
||||
}
|
||||
Reference in New Issue
Block a user