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

This commit is contained in:
rongyi 2024-10-31 21:24:56 +08:00 committed by GitHub
parent 7d708b4fce
commit 68a61f23d5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 38 additions and 29 deletions

View file

@ -49,18 +49,11 @@ impl ArrayBinaryTree {
/* 层序遍历 */ /* 层序遍历 */
fn level_order(&self) -> Vec<i32> { fn level_order(&self) -> Vec<i32> {
let mut res = vec![]; self.tree.iter().filter_map(|&x| x).collect()
// 直接遍历数组
for i in 0..self.size() {
if let Some(val) = self.val(i) {
res.push(val)
}
}
res
} }
/* 深度优先遍历 */ /* 深度优先遍历 */
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() { if self.val(i).is_none() {
return; return;
} }

View file

@ -126,7 +126,7 @@ impl BinarySearchTree {
// 删除节点 cur // 删除节点 cur
if !Rc::ptr_eq(&cur, self.root.as_ref().unwrap()) { if !Rc::ptr_eq(&cur, self.root.as_ref().unwrap()) {
let left = pre.borrow().left.clone(); 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; pre.borrow_mut().left = child;
} else { } else {
pre.borrow_mut().right = child; pre.borrow_mut().right = child;
@ -147,11 +147,11 @@ impl BinarySearchTree {
break; break;
} }
} }
let tmpval = tmp.unwrap().borrow().val; let tmp_val = tmp.unwrap().borrow().val;
// 递归删除节点 tmp // 递归删除节点 tmp
self.remove(tmpval); self.remove(tmp_val);
// 用 tmp 覆盖 cur // 用 tmp 覆盖 cur
cur.borrow_mut().val = tmpval; cur.borrow_mut().val = tmp_val;
} }
} }
} }

View file

@ -4,7 +4,7 @@
* Author: xBLACKICEx (xBLACKICE@outlook.com) * 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 hello_algo_rust::op_vec;
use std::cell::RefCell; use std::cell::RefCell;
@ -14,12 +14,17 @@ use std::rc::Rc;
fn pre_order(root: Option<&Rc<RefCell<TreeNode>>>) -> Vec<i32> { fn pre_order(root: Option<&Rc<RefCell<TreeNode>>>) -> Vec<i32> {
let mut result = vec![]; let mut result = vec![];
if let Some(node) = root { fn dfs(root: Option<&Rc<RefCell<TreeNode>>>, res: &mut Vec<i32>) {
// 访问优先级:根节点 -> 左子树 -> 右子树 if let Some(node) = root {
result.push(node.borrow().val); // 访问优先级:根节点 -> 左子树 -> 右子树
result.extend(pre_order(node.borrow().left.as_ref())); let node = node.borrow();
result.extend(pre_order(node.borrow().right.as_ref())); res.push(node.val);
dfs(node.left.as_ref(), res);
dfs(node.right.as_ref(), res);
}
} }
dfs(root, &mut result);
result result
} }
@ -27,12 +32,17 @@ fn pre_order(root: Option<&Rc<RefCell<TreeNode>>>) -> Vec<i32> {
fn in_order(root: Option<&Rc<RefCell<TreeNode>>>) -> Vec<i32> { fn in_order(root: Option<&Rc<RefCell<TreeNode>>>) -> Vec<i32> {
let mut result = vec![]; let mut result = vec![];
if let Some(node) = root { fn dfs(root: Option<&Rc<RefCell<TreeNode>>>, res: &mut Vec<i32>) {
// 访问优先级:左子树 -> 根节点 -> 右子树 if let Some(node) = root {
result.extend(in_order(node.borrow().left.as_ref())); // 访问优先级:左子树 -> 根节点 -> 右子树
result.push(node.borrow().val); let node = node.borrow();
result.extend(in_order(node.borrow().right.as_ref())); dfs(node.left.as_ref(), res);
res.push(node.val);
dfs(node.right.as_ref(), res);
}
} }
dfs(root, &mut result);
result result
} }
@ -40,12 +50,18 @@ fn in_order(root: Option<&Rc<RefCell<TreeNode>>>) -> Vec<i32> {
fn post_order(root: Option<&Rc<RefCell<TreeNode>>>) -> Vec<i32> { fn post_order(root: Option<&Rc<RefCell<TreeNode>>>) -> Vec<i32> {
let mut result = vec![]; let mut result = vec![];
if let Some(node) = root { fn dfs(root: Option<&Rc<RefCell<TreeNode>>>, res: &mut Vec<i32>) {
// 访问优先级:左子树 -> 右子树 -> 根节点 if let Some(node) = root {
result.extend(post_order(node.borrow().left.as_ref())); // 访问优先级:左子树 -> 右子树 -> 根节点
result.extend(post_order(node.borrow().right.as_ref())); let node = node.borrow();
result.push(node.borrow().val); dfs(node.left.as_ref(), res);
dfs(node.right.as_ref(), res);
res.push(node.val);
}
} }
dfs(root, &mut result);
result result
} }