mirror of
				https://github.com/krahets/hello-algo.git
				synced 2025-11-04 14:18:20 +08:00 
			
		
		
		
	* First commit * Update mkdocs.yml * Translate all the docs to traditional Chinese * Translate the code files. * Translate the docker file * Fix mkdocs.yml * Translate all the figures from SC to TC * 二叉搜尋樹 -> 二元搜尋樹 * Update terminology. * Update terminology * 构造函数/构造方法 -> 建構子 异或 -> 互斥或 * 擴充套件 -> 擴展 * constant - 常量 - 常數 * 類 -> 類別 * AVL -> AVL 樹 * 數組 -> 陣列 * 係統 -> 系統 斐波那契數列 -> 費波那契數列 運算元量 -> 運算量 引數 -> 參數 * 聯絡 -> 關聯 * 麵試 -> 面試 * 面向物件 -> 物件導向 歸併排序 -> 合併排序 范式 -> 範式 * Fix 算法 -> 演算法 * 錶示 -> 表示 反碼 -> 一補數 補碼 -> 二補數 列列尾部 -> 佇列尾部 區域性性 -> 區域性 一摞 -> 一疊 * Synchronize with main branch * 賬號 -> 帳號 推匯 -> 推導 * Sync with main branch * First commit * Update mkdocs.yml * Translate all the docs to traditional Chinese * Translate the code files. * Translate the docker file * Fix mkdocs.yml * Translate all the figures from SC to TC * 二叉搜尋樹 -> 二元搜尋樹 * Update terminology * 构造函数/构造方法 -> 建構子 异或 -> 互斥或 * 擴充套件 -> 擴展 * constant - 常量 - 常數 * 類 -> 類別 * AVL -> AVL 樹 * 數組 -> 陣列 * 係統 -> 系統 斐波那契數列 -> 費波那契數列 運算元量 -> 運算量 引數 -> 參數 * 聯絡 -> 關聯 * 麵試 -> 面試 * 面向物件 -> 物件導向 歸併排序 -> 合併排序 范式 -> 範式 * Fix 算法 -> 演算法 * 錶示 -> 表示 反碼 -> 一補數 補碼 -> 二補數 列列尾部 -> 佇列尾部 區域性性 -> 區域性 一摞 -> 一疊 * Synchronize with main branch * 賬號 -> 帳號 推匯 -> 推導 * Sync with main branch * Update terminology.md * 操作数量(num. of operations)-> 操作數量 * 字首和->前綴和 * Update figures * 歸 -> 迴 記憶體洩漏 -> 記憶體流失 * Fix the bug of the file filter * 支援 -> 支持 Add zh-Hant/README.md * Add the zh-Hant chapter covers. Bug fixes. * 外掛 -> 擴充功能 * Add the landing page for zh-Hant version * Unify the font of the chapter covers for the zh, en, and zh-Hant version * Move zh-Hant/ to zh-hant/ * Translate terminology.md to traditional Chinese
		
			
				
	
	
		
			103 lines
		
	
	
		
			2.8 KiB
		
	
	
	
		
			Rust
		
	
	
	
	
	
			
		
		
	
	
			103 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 crate::list_node::ListNode;
 | 
						|
use crate::tree_node::{TreeNode, vec_to_tree};
 | 
						|
 | 
						|
struct Trunk<'a, 'b> {
 | 
						|
    prev: Option<&'a Trunk<'a, 'b>>,
 | 
						|
    str: Cell<&'b str>,
 | 
						|
}
 | 
						|
 | 
						|
/* 列印陣列 */
 | 
						|
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!("]");
 | 
						|
    }
 | 
						|
}
 | 
						|
 | 
						|
/* 列印雜湊表 */
 | 
						|
pub fn print_hash_map<TKey: Display, TValue: Display>(map: &HashMap<TKey, TValue>) {
 | 
						|
    for (key, value) in map {
 | 
						|
        println!("{key} -> {value}");
 | 
						|
    }
 | 
						|
}
 | 
						|
 | 
						|
/* 列印佇列(雙向佇列) */
 | 
						|
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 {", "} );
 | 
						|
    }
 | 
						|
}
 | 
						|
 | 
						|
/* 列印鏈結串列 */
 | 
						|
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);
 | 
						|
    }
 | 
						|
}
 | 
						|
 | 
						|
/* 列印二元樹 */
 | 
						|
pub fn print_tree(root: &Rc<RefCell<TreeNode>>) {
 | 
						|
    _print_tree(Some(root), None, false);
 | 
						|
}
 | 
						|
 | 
						|
/* 列印二元樹 */
 | 
						|
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());
 | 
						|
    }
 | 
						|
}
 | 
						|
 | 
						|
/* 列印堆積 */
 | 
						|
pub fn print_heap(heap: Vec<i32>) {
 | 
						|
    println!("堆積的陣列表示:{:?}", heap);
 | 
						|
    println!("堆積的樹狀表示:");
 | 
						|
    if let Some(root) = vec_to_tree(heap.into_iter().map(|val| Some(val)).collect()) {
 | 
						|
        print_tree(&root);
 | 
						|
    } 
 | 
						|
} |