mirror of
https://github.com/krahets/hello-algo.git
synced 2026-08-12 20:00:58 +00:00
2778a6f9c7
* 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
104 lines
2.8 KiB
Rust
104 lines
2.8 KiB
Rust
/*
|
|
* File: print_util.rs
|
|
* Created Time: 2023-02-05
|
|
* Author: codingonion (coderonion@gmail.com), xBLACKICEx (xBLACKICEx@outlook.com)
|
|
*/
|
|
|
|
use std::cell::{Cell, RefCell};
|
|
use std::fmt::Display;
|
|
use std::collections::{HashMap, VecDeque};
|
|
use std::rc::Rc;
|
|
|
|
use super::list_node::ListNode;
|
|
use super::tree_node::{TreeNode, vec_to_tree};
|
|
|
|
struct Trunk<'a, 'b> {
|
|
prev: Option<&'a Trunk<'a, 'b>>,
|
|
str: Cell<&'b str>,
|
|
}
|
|
|
|
/* Print array */
|
|
pub fn print_array<T: Display>(nums: &[T]) {
|
|
print!("[");
|
|
if nums.len() > 0 {
|
|
for (i, num) in nums.iter().enumerate() {
|
|
print!("{}{}", num, if i == nums.len() - 1 {"]"} else {", "} );
|
|
}
|
|
} else {
|
|
print!("]");
|
|
}
|
|
}
|
|
|
|
/* Print hash table */
|
|
pub fn print_hash_map<TKey: Display, TValue: Display>(map: &HashMap<TKey, TValue>) {
|
|
for (key, value) in map {
|
|
println!("{key} -> {value}");
|
|
}
|
|
}
|
|
|
|
/* Print queue (deque) */
|
|
pub fn print_queue<T: Display>(queue: &VecDeque<T>) {
|
|
print!("[");
|
|
let iter = queue.iter();
|
|
for (i, data) in iter.enumerate() {
|
|
print!("{}{}", data, if i == queue.len() - 1 {"]"} else {", "} );
|
|
}
|
|
}
|
|
|
|
/* Print linked list */
|
|
pub fn print_linked_list<T: Display>(head: &Rc<RefCell<ListNode<T>>>) {
|
|
print!("{}{}", head.borrow().val, if head.borrow().next.is_none() {"\n"} else {" -> "});
|
|
if let Some(node) = &head.borrow().next {
|
|
return print_linked_list(node);
|
|
}
|
|
}
|
|
|
|
/* Print binary tree */
|
|
pub fn print_tree(root: &Rc<RefCell<TreeNode>>) {
|
|
_print_tree(Some(root), None, false);
|
|
}
|
|
|
|
/* Print binary tree */
|
|
fn _print_tree(root: Option<&Rc<RefCell<TreeNode>>>, prev: Option<&Trunk>, is_right: bool) {
|
|
if let Some(node) = root {
|
|
let mut prev_str = " ";
|
|
let trunk = Trunk { prev, str: Cell::new(prev_str) };
|
|
_print_tree(node.borrow().right.as_ref(), Some(&trunk), true);
|
|
|
|
if prev.is_none() {
|
|
trunk.str.set("———");
|
|
} else if is_right {
|
|
trunk.str.set("/———");
|
|
prev_str = " |";
|
|
} else {
|
|
trunk.str.set("\\———");
|
|
prev.as_ref().unwrap().str.set(prev_str);
|
|
}
|
|
|
|
show_trunks(Some(&trunk));
|
|
println!(" {}", node.borrow().val);
|
|
if let Some(prev) = prev {
|
|
prev.str.set(prev_str);
|
|
}
|
|
trunk.str.set(" |");
|
|
|
|
_print_tree(node.borrow().left.as_ref(), Some(&trunk), false);
|
|
}
|
|
}
|
|
|
|
fn show_trunks(trunk: Option<&Trunk>) {
|
|
if let Some(trunk) = trunk {
|
|
show_trunks(trunk.prev);
|
|
print!("{}", trunk.str.get());
|
|
}
|
|
}
|
|
|
|
/* Print heap */
|
|
pub fn print_heap(heap: Vec<i32>) {
|
|
println!("Array representation of heap: {:?}", heap);
|
|
println!("Heap tree representation:");
|
|
if let Some(root) = vec_to_tree(heap.into_iter().map(|val| Some(val)).collect()) {
|
|
print_tree(&root);
|
|
}
|
|
}
|