Skip to content

Commit

Permalink
fix(Rust): no need to clone in hash put fn (krahets#1226)
Browse files Browse the repository at this point in the history
* no need to clone in hash put fn

* fmt rust code

* make code more readable

* Change vec append to extend, more friendly to rookie rust dev

* drop comment
  • Loading branch information
rongyi authored Apr 7, 2024
1 parent bd54594 commit f901a31
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 18 deletions.
7 changes: 2 additions & 5 deletions codes/rust/chapter_hashing/hash_map_chaining.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,17 +102,14 @@ impl HashMapChaining {
// 遍历桶,若遇到指定 key ,则更新对应 val 并返回
for pair in bucket {
if pair.key == key {
pair.val = val.clone();
pair.val = val;
return;
}
}
let bucket = &mut self.buckets[index];

// 若无该 key ,则将键值对添加至尾部
let pair = Pair {
key,
val: val.clone(),
};
let pair = Pair { key, val };
bucket.push(pair);
self.size += 1;
}
Expand Down
6 changes: 3 additions & 3 deletions codes/rust/chapter_tree/binary_tree_bfs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,18 @@ use tree_node::{vec_to_tree, TreeNode};
fn level_order(root: &Rc<RefCell<TreeNode>>) -> Vec<i32> {
// 初始化队列,加入根节点
let mut que = VecDeque::new();
que.push_back(Rc::clone(&root));
que.push_back(root.clone());
// 初始化一个列表,用于保存遍历序列
let mut vec = Vec::new();

while let Some(node) = que.pop_front() {
// 队列出队
vec.push(node.borrow().val); // 保存节点值
if let Some(left) = node.borrow().left.as_ref() {
que.push_back(Rc::clone(left)); // 左子节点入队
que.push_back(left.clone()); // 左子节点入队
}
if let Some(right) = node.borrow().right.as_ref() {
que.push_back(Rc::clone(right)); // 右子节点入队
que.push_back(right.clone()); // 右子节点入队
};
}
vec
Expand Down
12 changes: 6 additions & 6 deletions codes/rust/chapter_tree/binary_tree_dfs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ fn pre_order(root: Option<&Rc<RefCell<TreeNode>>>) -> Vec<i32> {
if let Some(node) = root {
// 访问优先级:根节点 -> 左子树 -> 右子树
result.push(node.borrow().val);
result.append(&mut pre_order(node.borrow().left.as_ref()));
result.append(&mut pre_order(node.borrow().right.as_ref()));
result.extend(pre_order(node.borrow().left.as_ref()));
result.extend(pre_order(node.borrow().right.as_ref()));
}
result
}
Expand All @@ -29,9 +29,9 @@ fn in_order(root: Option<&Rc<RefCell<TreeNode>>>) -> Vec<i32> {

if let Some(node) = root {
// 访问优先级:左子树 -> 根节点 -> 右子树
result.append(&mut in_order(node.borrow().left.as_ref()));
result.extend(in_order(node.borrow().left.as_ref()));
result.push(node.borrow().val);
result.append(&mut in_order(node.borrow().right.as_ref()));
result.extend(in_order(node.borrow().right.as_ref()));
}
result
}
Expand All @@ -42,8 +42,8 @@ fn post_order(root: Option<&Rc<RefCell<TreeNode>>>) -> Vec<i32> {

if let Some(node) = root {
// 访问优先级:左子树 -> 右子树 -> 根节点
result.append(&mut post_order(node.borrow().left.as_ref()));
result.append(&mut post_order(node.borrow().right.as_ref()));
result.extend(post_order(node.borrow().left.as_ref()));
result.extend(post_order(node.borrow().right.as_ref()));
result.push(node.borrow().val);
}
result
Expand Down
9 changes: 5 additions & 4 deletions codes/rust/include/tree_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ impl TreeNode {
height: 0,
parent: None,
left: None,
right: None
right: None,
}))
}
}
Expand All @@ -34,7 +34,7 @@ impl TreeNode {
macro_rules! op_vec {
( $( $x:expr ),* ) => {
vec![
$( Option::from($x).map(|x| x) ),*
$( Option::from($x).map(|x| x) ),*
]
};
}
Expand Down Expand Up @@ -75,9 +75,10 @@ pub fn vec_to_tree(arr: Vec<Option<i32>>) -> Option<Rc<RefCell<TreeNode>>> {
fn tree_to_vec_dfs(root: Option<Rc<RefCell<TreeNode>>>, i: usize, res: &mut Vec<Option<i32>>) {
if root.is_none() {
return;
}
}
let root = root.unwrap();
while i >= res.len() {
// i + 1 is the minimum valid size to access index i
while res.len() < i + 1 {
res.push(None);
}
res[i] = Some(root.borrow().val);
Expand Down

0 comments on commit f901a31

Please sign in to comment.