Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make dfs same as c/c++ and other small improvement #1543

Merged
merged 1 commit into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
make dfs same as c/c++ and other small improvement
  • Loading branch information
rongyi committed Oct 31, 2024
commit 89bb146eb6949c20a98b029d9973f4fae946c671
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 (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;
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