From 590b532606acb0f395554019c7939e26719d9552 Mon Sep 17 00:00:00 2001 From: xBLACKICEx Date: Tue, 7 Mar 2023 16:46:28 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat(rust/tree):=20add=20binary=5Ft?= =?UTF-8?q?ree=20(#398)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * ✨ feat(rust/hashing): add array_hash_map * πŸ“ƒ docs(rust/hashing): correct comments * ✨ feat(rust/include): add tree_node * ✨ feat(rust/include): add print_tree * ✨ feat(rust/tree): add binary_tree * docs(rust/tree): correct comments * πŸ“ƒ docs(rust/tree): correct comments --- codes/rust/Cargo.toml | 5 +++ codes/rust/chapter_tree/binary_tree.rs | 39 +++++++++++++++++++++ codes/rust/include/include.rs | 5 +-- codes/rust/include/print_util.rs | 48 +++++++++++++++++++++++++- codes/rust/include/tree_node.rs | 29 ++++++++++++++++ 5 files changed, 123 insertions(+), 3 deletions(-) create mode 100644 codes/rust/chapter_tree/binary_tree.rs create mode 100644 codes/rust/include/tree_node.rs diff --git a/codes/rust/Cargo.toml b/codes/rust/Cargo.toml index 4fb1198f..b203b74e 100644 --- a/codes/rust/Cargo.toml +++ b/codes/rust/Cargo.toml @@ -89,5 +89,10 @@ path = "chapter_stack_and_queue/array_stack.rs" name = "array_queue" path = "chapter_stack_and_queue/array_queue.rs" +# Run Command: cargo run --bin binary_tree +[[bin]] +name = "binary_tree" +path = "chapter_tree/binary_tree.rs" + [dependencies] rand = "0.8.5" diff --git a/codes/rust/chapter_tree/binary_tree.rs b/codes/rust/chapter_tree/binary_tree.rs new file mode 100644 index 00000000..737981f8 --- /dev/null +++ b/codes/rust/chapter_tree/binary_tree.rs @@ -0,0 +1,39 @@ +/** + * File: binary_tree.rs + * Created Time: 2023-02-27 + * Author: xBLACKICEx (xBLACKICE@outlook.com) + */ + +use std::rc::Rc; +include!("../include/include.rs"); +use tree_node::TreeNode; + +/* Driver Code */ +fn main() { + // εˆε§‹εŒ–δΊŒε‰ζ ‘ + let n1 = TreeNode::new(1); + let n2 = TreeNode::new(2); + let n3 = TreeNode::new(3); + let n4 = TreeNode::new(4); + let n5 = TreeNode::new(5); + // ζž„ε»ΊεΌ•η”¨ζŒ‡ε‘οΌˆε³ζŒ‡ι’ˆοΌ‰ + n1.borrow_mut().left = Some(Rc::clone(&n2)); + n1.borrow_mut().right = Some(Rc::clone(&n3)); + n2.borrow_mut().left = Some(Rc::clone(&n4)); + n2.borrow_mut().right = Some(Rc::clone(&n5)); + println!("\nεˆε§‹εŒ–δΊŒε‰ζ ‘\n"); + print_util::print_tree(&n1); + + // 插ε…₯θŠ‚η‚ΉδΈŽεˆ ι™€θŠ‚η‚Ή + let p = TreeNode::new(0); + // 在 n1 -> n2 中间插ε…₯η»“η‚Ή P + p.borrow_mut().left = Some(Rc::clone(&n2)); + n1.borrow_mut().left = Some(Rc::clone(&p)); + println!("\n插ε…₯η»“η‚Ή P 后\n"); + print_util::print_tree(&n1); + // εˆ ι™€η»“η‚Ή P + drop(p); + n1.borrow_mut().left = Some(Rc::clone(&n2)); + println!("\nεˆ ι™€η»“η‚Ή P 后\n"); + print_util::print_tree(&n1); +} \ No newline at end of file diff --git a/codes/rust/include/include.rs b/codes/rust/include/include.rs index 1363cb9f..90c6b0e6 100644 --- a/codes/rust/include/include.rs +++ b/codes/rust/include/include.rs @@ -1,7 +1,8 @@ /* * File: include.rs * Created Time: 2023-02-05 - * Author: sjinzh (sjinzh@gmail.com) + * Author: sjinzh (sjinzh@gmail.com), xBLACKICEx (xBLACKICE@outlook.com) */ -pub mod print_util; \ No newline at end of file +pub mod print_util; +pub mod tree_node; \ No newline at end of file diff --git a/codes/rust/include/print_util.rs b/codes/rust/include/print_util.rs index edd4affc..3511709f 100644 --- a/codes/rust/include/print_util.rs +++ b/codes/rust/include/print_util.rs @@ -4,8 +4,17 @@ * 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]) { @@ -33,4 +42,41 @@ pub fn print_queue(queue: &VecDeque) { for (i, data) in iter.enumerate() { print!("{}{}", data, if i == queue.len() - 1 {"]"} else {", "} ); } -} \ No newline at end of file +} +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()); + } +} diff --git a/codes/rust/include/tree_node.rs b/codes/rust/include/tree_node.rs new file mode 100644 index 00000000..b26d9809 --- /dev/null +++ b/codes/rust/include/tree_node.rs @@ -0,0 +1,29 @@ +/** + * File: tree_node.rs + * Created Time: 2023-02-27 + * Author: xBLACKICEx (xBLACKICE@outlook.com) + */ + +use std::cell::RefCell; +use std::rc::Rc; + +#[allow(dead_code)] +pub struct TreeNode { + pub val: i32, + pub high: i32, + pub parent: Option>>, + pub left: Option>>, + pub right: Option>>, +} + +impl TreeNode { + pub fn new(val: i32) -> Rc> { + Rc::new(RefCell::new(Self { + val, + high: 0, + parent: None, + left: None, + right: None + })) + } +} \ No newline at end of file