From 68a61f23d54fc73158261fb22bac35ad0dc68f2d Mon Sep 17 00:00:00 2001 From: rongyi Date: Thu, 31 Oct 2024 21:24:56 +0800 Subject: [PATCH] make dfs same as c/c++ and other small improvement (#1543) --- codes/rust/chapter_tree/array_binary_tree.rs | 11 +---- codes/rust/chapter_tree/binary_search_tree.rs | 8 ++-- codes/rust/chapter_tree/binary_tree_dfs.rs | 48 ++++++++++++------- 3 files changed, 38 insertions(+), 29 deletions(-) diff --git a/codes/rust/chapter_tree/array_binary_tree.rs b/codes/rust/chapter_tree/array_binary_tree.rs index 98504159b8..296f247df9 100644 --- a/codes/rust/chapter_tree/array_binary_tree.rs +++ b/codes/rust/chapter_tree/array_binary_tree.rs @@ -49,18 +49,11 @@ impl ArrayBinaryTree { /* 层序遍历 */ fn level_order(&self) -> Vec { - let mut res = vec![]; - // 直接遍历数组 - for i in 0..self.size() { - if let Some(val) = self.val(i) { - res.push(val) - } - } - res + self.tree.iter().filter_map(|&x| x).collect() } /* 深度优先遍历 */ - fn dfs(&self, i: i32, order: &str, res: &mut Vec) { + fn dfs(&self, i: i32, order: &'static str, res: &mut Vec) { if self.val(i).is_none() { return; } diff --git a/codes/rust/chapter_tree/binary_search_tree.rs b/codes/rust/chapter_tree/binary_search_tree.rs index 21daad9f73..720d9c642b 100644 --- a/codes/rust/chapter_tree/binary_search_tree.rs +++ b/codes/rust/chapter_tree/binary_search_tree.rs @@ -126,7 +126,7 @@ impl BinarySearchTree { // 删除节点 cur if !Rc::ptr_eq(&cur, self.root.as_ref().unwrap()) { let left = pre.borrow().left.clone(); - if left.is_some() && Rc::ptr_eq(&left.as_ref().unwrap(), &cur) { + if left.is_some() && Rc::ptr_eq(left.as_ref().unwrap(), &cur) { pre.borrow_mut().left = child; } else { pre.borrow_mut().right = child; @@ -147,11 +147,11 @@ impl BinarySearchTree { break; } } - let tmpval = tmp.unwrap().borrow().val; + let tmp_val = tmp.unwrap().borrow().val; // 递归删除节点 tmp - self.remove(tmpval); + self.remove(tmp_val); // 用 tmp 覆盖 cur - cur.borrow_mut().val = tmpval; + cur.borrow_mut().val = tmp_val; } } } diff --git a/codes/rust/chapter_tree/binary_tree_dfs.rs b/codes/rust/chapter_tree/binary_tree_dfs.rs index 56c68614b0..77533d52f9 100644 --- a/codes/rust/chapter_tree/binary_tree_dfs.rs +++ b/codes/rust/chapter_tree/binary_tree_dfs.rs @@ -4,7 +4,7 @@ * Author: xBLACKICEx (xBLACKICE@outlook.com) */ -use hello_algo_rust::include::{vec_to_tree, TreeNode, print_util}; +use hello_algo_rust::include::{print_util, vec_to_tree, TreeNode}; use hello_algo_rust::op_vec; use std::cell::RefCell; @@ -14,12 +14,17 @@ use std::rc::Rc; fn pre_order(root: Option<&Rc>>) -> Vec { let mut result = vec![]; - if let Some(node) = root { - // 访问优先级:根节点 -> 左子树 -> 右子树 - result.push(node.borrow().val); - result.extend(pre_order(node.borrow().left.as_ref())); - result.extend(pre_order(node.borrow().right.as_ref())); + fn dfs(root: Option<&Rc>>, res: &mut Vec) { + if let Some(node) = root { + // 访问优先级:根节点 -> 左子树 -> 右子树 + let node = node.borrow(); + res.push(node.val); + dfs(node.left.as_ref(), res); + dfs(node.right.as_ref(), res); + } } + dfs(root, &mut result); + result } @@ -27,12 +32,17 @@ fn pre_order(root: Option<&Rc>>) -> Vec { fn in_order(root: Option<&Rc>>) -> Vec { let mut result = vec![]; - if let Some(node) = root { - // 访问优先级:左子树 -> 根节点 -> 右子树 - result.extend(in_order(node.borrow().left.as_ref())); - result.push(node.borrow().val); - result.extend(in_order(node.borrow().right.as_ref())); + fn dfs(root: Option<&Rc>>, res: &mut Vec) { + if let Some(node) = root { + // 访问优先级:左子树 -> 根节点 -> 右子树 + let node = node.borrow(); + dfs(node.left.as_ref(), res); + res.push(node.val); + dfs(node.right.as_ref(), res); + } } + dfs(root, &mut result); + result } @@ -40,12 +50,18 @@ fn in_order(root: Option<&Rc>>) -> Vec { fn post_order(root: Option<&Rc>>) -> Vec { let mut result = vec![]; - if let Some(node) = root { - // 访问优先级:左子树 -> 右子树 -> 根节点 - result.extend(post_order(node.borrow().left.as_ref())); - result.extend(post_order(node.borrow().right.as_ref())); - result.push(node.borrow().val); + fn dfs(root: Option<&Rc>>, res: &mut Vec) { + if let Some(node) = root { + // 访问优先级:左子树 -> 右子树 -> 根节点 + let node = node.borrow(); + dfs(node.left.as_ref(), res); + dfs(node.right.as_ref(), res); + res.push(node.val); + } } + + dfs(root, &mut result); + result }