mirror of
				https://github.com/krahets/hello-algo.git
				synced 2025-11-04 22:28:40 +08:00 
			
		
		
		
	✨ feat(rust/tree): add binary_tree (#398)
* ✨ 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
This commit is contained in:
		@ -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"
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										39
									
								
								codes/rust/chapter_tree/binary_tree.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										39
									
								
								codes/rust/chapter_tree/binary_tree.rs
									
									
									
									
									
										Normal file
									
								
							@ -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);
 | 
			
		||||
}
 | 
			
		||||
@ -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;
 | 
			
		||||
pub mod tree_node;
 | 
			
		||||
@ -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<T: Display>(nums: &[T]) {
 | 
			
		||||
@ -34,3 +43,40 @@ pub fn print_queue<T: Display>(queue: &VecDeque<T>) {
 | 
			
		||||
        print!("{}{}", data, if i == queue.len() - 1 {"]"} else {", "} );
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
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_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());
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										29
									
								
								codes/rust/include/tree_node.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										29
									
								
								codes/rust/include/tree_node.rs
									
									
									
									
									
										Normal file
									
								
							@ -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<Rc<RefCell<TreeNode>>>,
 | 
			
		||||
    pub left: Option<Rc<RefCell<TreeNode>>>,
 | 
			
		||||
    pub right: Option<Rc<RefCell<TreeNode>>>,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl TreeNode {
 | 
			
		||||
    pub fn new(val: i32) -> Rc<RefCell<Self>> {
 | 
			
		||||
        Rc::new(RefCell::new(Self {
 | 
			
		||||
            val,
 | 
			
		||||
            high: 0,
 | 
			
		||||
            parent: None,
 | 
			
		||||
            left: None,
 | 
			
		||||
            right: None
 | 
			
		||||
        }))
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user