mirror of
https://github.com/krahets/hello-algo.git
synced 2026-11-30 00:04:44 +08:00
Cite demo-source regions for heap operations and the linked-list node classes (#2036)
Batch 3a of the runnable-snippets plan. heap_operations now cites the heap demos in Python, C++, Java, C#, Go, Swift, Rust and Kotlin; the demos call the library directly instead of push/pop helpers and print each popped value. list_node_class cites each language's shared ListNode (Swift stays literal) and doubly_list_node_class cites linkedlist_deque (Go stays literal). Node comments adopt reviewed source wording, Rust and Swift get import regions, the Ruby typo 前躯 becomes 前驱, and the TypeScript doubly node fields become nullable. en, ja, ko and zh-hant are regenerated; ru is synced by hand. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -6,22 +6,13 @@
|
||||
|
||||
use hello_algo_rust::include::print_util;
|
||||
|
||||
// [region]{import}
|
||||
use std::{cmp::Reverse, collections::BinaryHeap};
|
||||
|
||||
fn test_push_max(heap: &mut BinaryHeap<i32>, val: i32) {
|
||||
heap.push(val); // 元素入堆
|
||||
println!("\n元素 {} 入堆后", 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!("\n堆顶元素 {} 出堆后", val);
|
||||
print_util::print_heap(heap.iter().map(|&val| val).collect());
|
||||
}
|
||||
// [endregion]{import}
|
||||
|
||||
/* Driver Code */
|
||||
fn main() {
|
||||
// [region]{init}
|
||||
/* 初始化堆 */
|
||||
// 初始化小顶堆
|
||||
#[allow(unused_assignments)]
|
||||
@@ -29,43 +20,66 @@ fn main() {
|
||||
// Rust 的 BinaryHeap 是大顶堆,小顶堆一般会“套上”Reverse
|
||||
// 初始化大顶堆
|
||||
let mut max_heap = BinaryHeap::new();
|
||||
// [endregion]{init}
|
||||
|
||||
println!("\n以下测试样例为大顶堆");
|
||||
|
||||
// [region]{push}
|
||||
/* 元素入堆 */
|
||||
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);
|
||||
max_heap.push(1);
|
||||
max_heap.push(3);
|
||||
max_heap.push(2);
|
||||
max_heap.push(5);
|
||||
max_heap.push(4);
|
||||
// [endregion]{push}
|
||||
print_util::print_heap(max_heap.iter().map(|&val| val).collect());
|
||||
|
||||
// [region]{peek}
|
||||
/* 获取堆顶元素 */
|
||||
let peek = max_heap.peek().unwrap();
|
||||
let peek = max_heap.peek().unwrap(); // 5
|
||||
// [endregion]{peek}
|
||||
println!("\n堆顶元素为 {}", peek);
|
||||
|
||||
// [region]{pop_1}
|
||||
/* 堆顶元素出堆 */
|
||||
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);
|
||||
// 出堆元素会形成一个从大到小的序列
|
||||
let peek = max_heap.pop().unwrap(); // 5
|
||||
// [endregion]{pop_1}
|
||||
println!("{}", peek);
|
||||
// [region]{pop_2}
|
||||
let peek = max_heap.pop().unwrap(); // 4
|
||||
// [endregion]{pop_2}
|
||||
println!("{}", peek);
|
||||
// [region]{pop_3}
|
||||
let peek = max_heap.pop().unwrap(); // 3
|
||||
// [endregion]{pop_3}
|
||||
println!("{}", peek);
|
||||
// [region]{pop_4}
|
||||
let peek = max_heap.pop().unwrap(); // 2
|
||||
// [endregion]{pop_4}
|
||||
println!("{}", peek);
|
||||
// [region]{pop_5}
|
||||
let peek = max_heap.pop().unwrap(); // 1
|
||||
// [endregion]{pop_5}
|
||||
println!("{}", peek);
|
||||
|
||||
// [region]{size}
|
||||
/* 获取堆大小 */
|
||||
let size = max_heap.len();
|
||||
// [endregion]{size}
|
||||
println!("\n堆元素数量为 {}", size);
|
||||
|
||||
// [region]{is_empty}
|
||||
/* 判断堆是否为空 */
|
||||
let is_empty = max_heap.is_empty();
|
||||
// [endregion]{is_empty}
|
||||
println!("\n堆是否为空 {}", is_empty);
|
||||
|
||||
// [region]{heapify}
|
||||
/* 输入列表并建堆 */
|
||||
// 时间复杂度为 O(n) ,而非 O(nlogn)
|
||||
min_heap = BinaryHeap::from(
|
||||
vec![1, 3, 2, 5, 4]
|
||||
.into_iter()
|
||||
.map(|val| Reverse(val))
|
||||
.collect::<Vec<Reverse<i32>>>(),
|
||||
);
|
||||
min_heap = BinaryHeap::from(vec![Reverse(1), Reverse(3), Reverse(2), Reverse(5), Reverse(4)]);
|
||||
// [endregion]{heapify}
|
||||
println!("\n输入列表并建立小顶堆后");
|
||||
print_util::print_heap(min_heap.iter().map(|&val| val.0).collect());
|
||||
}
|
||||
|
||||
@@ -6,9 +6,12 @@
|
||||
|
||||
use hello_algo_rust::include::print_util;
|
||||
|
||||
// [region]{import}
|
||||
use std::cell::RefCell;
|
||||
use std::rc::Rc;
|
||||
// [endregion]{import}
|
||||
|
||||
// [region]{doubly_list_node}
|
||||
/* 双向链表节点 */
|
||||
pub struct ListNode<T> {
|
||||
pub val: T, // 节点值
|
||||
@@ -25,6 +28,7 @@ impl<T> ListNode<T> {
|
||||
}))
|
||||
}
|
||||
}
|
||||
// [endregion]{doubly_list_node}
|
||||
|
||||
/* 基于双向链表实现的双向队列 */
|
||||
#[allow(dead_code)]
|
||||
|
||||
@@ -4,20 +4,26 @@
|
||||
* Author: codingonion (coderonion@gmail.com), rongyi (hiarongyi@gmail.com)
|
||||
*/
|
||||
|
||||
// [region]{import_cell}
|
||||
use std::cell::RefCell;
|
||||
// [endregion]{import_cell}
|
||||
use std::collections::HashMap;
|
||||
// [region]{import_rc}
|
||||
use std::rc::Rc;
|
||||
// [endregion]{import_rc}
|
||||
|
||||
// [region]{list_node}
|
||||
#[derive(Debug)]
|
||||
pub struct ListNode<T> {
|
||||
pub val: T,
|
||||
pub next: Option<Rc<RefCell<ListNode<T>>>>,
|
||||
pub val: T, // 节点值
|
||||
pub next: Option<Rc<RefCell<ListNode<T>>>>, // 后继节点指针
|
||||
}
|
||||
|
||||
impl<T> ListNode<T> {
|
||||
pub fn new(val: T) -> Rc<RefCell<ListNode<T>>> {
|
||||
Rc::new(RefCell::new(ListNode { val, next: None }))
|
||||
}
|
||||
// [endregion]{list_node}
|
||||
|
||||
/* 将数组反序列化为链表 */
|
||||
pub fn arr_to_linked_list(array: &[T]) -> Option<Rc<RefCell<ListNode<T>>>>
|
||||
@@ -54,4 +60,6 @@ impl<T> ListNode<T> {
|
||||
|
||||
hashmap
|
||||
}
|
||||
// [region]{list_node_end}
|
||||
}
|
||||
// [endregion]{list_node_end}
|
||||
|
||||
Reference in New Issue
Block a user