Skip to content

Commit

Permalink
make dfs same as c/c++ and other small improvement (#1543)
Browse files Browse the repository at this point in the history
  • Loading branch information
rongyi authored Oct 31, 2024
1 parent 7d708b4 commit 68a61f2
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 29 deletions.
11 changes: 2 additions & 9 deletions codes/rust/chapter_tree/array_binary_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,11 @@ impl ArrayBinaryTree {

/* 层序遍历 */
fn level_order(&self) -> Vec<i32> {
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<i32>) {
fn dfs(&self, i: i32, order: &'static str, res: &mut Vec<i32>) {
if self.val(i).is_none() {
return;
}
Expand Down
8 changes: 4 additions & 4 deletions codes/rust/chapter_tree/binary_search_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
}
}
Expand Down
48 changes: 32 additions & 16 deletions codes/rust/chapter_tree/binary_tree_dfs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Author: xBLACKICEx ([email protected])
*/

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;
Expand All @@ -14,38 +14,54 @@ use std::rc::Rc;
fn pre_order(root: Option<&Rc<RefCell<TreeNode>>>) -> Vec<i32> {
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<RefCell<TreeNode>>>, res: &mut Vec<i32>) {
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
}

/* 中序遍历 */
fn in_order(root: Option<&Rc<RefCell<TreeNode>>>) -> Vec<i32> {
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<RefCell<TreeNode>>>, res: &mut Vec<i32>) {
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
}

/* 后序遍历 */
fn post_order(root: Option<&Rc<RefCell<TreeNode>>>) -> Vec<i32> {
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<RefCell<TreeNode>>>, res: &mut Vec<i32>) {
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
}

Expand Down

0 comments on commit 68a61f2

Please sign in to comment.