/* * File: print_util.rs * Created Time: 2023-02-05 * Author: sjinzh (sjinzh@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::tree_node::TreeNode; struct Trunk<'a, 'b> { prev: Option<&'a Trunk<'a, 'b>>, str: Cell<&'b str>, } /* Print an array */ pub fn print_array(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 a hash map */ pub fn print_hash_map(map: &HashMap) { for (key, value) in map { println!("{key} -> {value}"); } } /* Print a queue or deque */ pub fn print_queue(queue: &VecDeque) { print!("["); let iter = queue.iter(); for (i, data) in iter.enumerate() { print!("{}{}", data, if i == queue.len() - 1 {"]"} else {", "} ); } } pub fn print_tree(root: &Rc>) { _print_tree(Some(root), None, false); } fn _print_tree(root: Option<&Rc>>, prev: Option<&Trunk>, is_left: 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_left { 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()); } }