Skip to content

Commit 51fcb69

Browse files
author
Lee Johnson
committed
opencamp-cn#1. [Header部分只有一行,包括三个字段]:type(必需)、scope(必需)和subject(必需):
opencamp-cn#1.1 <type:commit类型>: 必填项, 指定commit类型:feature/fix/refactor/style(如格式化)/test/build/docs/revert(执行git revert) opencamp-cn#1.2 <scope1:(JIRA号)>: 对应的Epic/Story/Task等JIRA号, 没有则填"None" opencamp-cn#1.3 <scope2:[功能模块]>: 必填项, 填写本次改动的功能模块名, 如微服务中module名, 多级module需要填全路径,如paip-common/paip-common-core opencamp-cn#1.4 <subject:简述>: 必填项, 简述本次提交内容, 如实现了功能名/模块名, 可直接拷贝对应JIRA号的标题 opencamp-cn#2. [Body:详细描述]: 填写详细描述, 主要描述改动之前的情况及修改动机, 对于小的修改不作要求, 但是重大需求更新等必须添加 opencamp-cn#3. [Footer:BREAKING CHANGE]: 当前代码与上一个版本不兼容, 以BREAKING CHANGE开头, 后面是对变动的描述变动理由和迁移方法 fix: 检查代码错误并修复 [详细描述] #[BREAKING CHANGE]:
1 parent 056e377 commit 51fcb69

2 files changed

Lines changed: 54 additions & 34 deletions

File tree

exercises/easy/algorithm10.rs

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -28,37 +28,51 @@ impl Graph for UndirectedGraph {
2828
fn adjacency_table(&self) -> &HashMap<String, Vec<(String, i32)>> {
2929
&self.adjacency_table
3030
}
31-
fn add_edge(&mut self, edge: (&str, &str, i32)) {
32-
let (from, to, weight) = edge;
33-
34-
// 确保两个节点都存在
35-
self.add_node(from);
36-
self.add_node(to);
37-
38-
// 因为是无向图,需要添加双向边
39-
self.adjacency_table_mutable()
40-
.get_mut(from)
41-
.unwrap()
42-
.push((to.to_string(), weight));
43-
44-
self.adjacency_table_mutable()
45-
.get_mut(to)
46-
.unwrap()
47-
.push((from.to_string(), weight));
31+
}
32+
33+
impl UndirectedGraph {
34+
// 添加一些辅助方法
35+
pub fn get_neighbors(&self, node: &str) -> Option<&Vec<(String, i32)>> {
36+
self.adjacency_table.get(node)
37+
}
38+
39+
pub fn node_count(&self) -> usize {
40+
self.adjacency_table.len()
41+
}
42+
43+
pub fn edge_count(&self) -> usize {
44+
self.edges().len() / 2 // 因为是无向图,每条边都被计算了两次
4845
}
4946
}
47+
5048
pub trait Graph {
5149
fn new() -> Self;
5250
fn adjacency_table_mutable(&mut self) -> &mut HashMap<String, Vec<(String, i32)>>;
5351
fn adjacency_table(&self) -> &HashMap<String, Vec<(String, i32)>>;
5452
fn add_node(&mut self, node: &str) -> bool {
55-
if self.contains(node) {
56-
return false;
53+
if self.adjacency_table_mutable().contains_key(node) {
54+
return true;
5755
}
58-
self.adjacency_table_mutable().insert(node.to_string(), Vec::new());
56+
self.adjacency_table_mutable().insert(String::from(node), vec![]);
5957
true
6058
}
61-
fn add_edge(&mut self, edge: (&str, &str, i32));
59+
fn add_edge(&mut self, edge: (&str, &str, i32)) {
60+
let (from, to, weight) = edge;
61+
if !self.contains(from) {
62+
self.add_node(from);
63+
}
64+
if !self.contains(to) {
65+
self.add_node(to);
66+
}
67+
self.adjacency_table_mutable()
68+
.get_mut(from)
69+
.unwrap()
70+
.push((String::from(to), weight));
71+
self.adjacency_table_mutable()
72+
.get_mut(to)
73+
.unwrap()
74+
.push((String::from(from), weight));
75+
}
6276
fn contains(&self, node: &str) -> bool {
6377
self.adjacency_table().get(node).is_some()
6478
}
@@ -97,4 +111,4 @@ mod test_undirected_graph {
97111
assert_eq!(graph.edges().contains(edge), true);
98112
}
99113
}
100-
}
114+
}

exercises/easy/algorithm4.rs

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,14 @@ where
5858

5959
// Search for a value in the BST
6060
fn search(&self, value: T) -> bool {
61-
fn search_internal<T: Ord>(node: &Option<Box<TreeNode<T>>>, value: &T) -> bool {
61+
fn search_internal<T: Ord>(node: &Option<Box<TreeNode<T>>>, target: &T) -> bool {
6262
match node {
6363
None => false,
6464
Some(node) => {
65-
match value.cmp(&node.value) {
65+
match target.cmp(&node.value) {
6666
Ordering::Equal => true,
67-
Ordering::Less => search_internal(&node.left, value),
68-
Ordering::Greater => search_internal(&node.right, value),
67+
Ordering::Less => search_internal(&node.left, target),
68+
Ordering::Greater => search_internal(&node.right, target),
6969
}
7070
}
7171
}
@@ -81,19 +81,23 @@ where
8181
// Insert a node into the tree
8282
fn insert(&mut self, value: T) {
8383
match value.cmp(&self.value) {
84+
Ordering::Equal => return, // 重复值不插入,直接返回
8485
Ordering::Less => {
85-
match &mut self.left {
86-
None => self.left = Some(Box::new(TreeNode::new(value))),
87-
Some(node) => node.insert(value),
86+
// 插入到左子树
87+
if self.left.is_none() {
88+
self.left = Some(Box::new(TreeNode::new(value)));
89+
} else {
90+
self.left.as_mut().unwrap().insert(value);
8891
}
8992
}
9093
Ordering::Greater => {
91-
match &mut self.right {
92-
None => self.right = Some(Box::new(TreeNode::new(value))),
93-
Some(node) => node.insert(value),
94+
// 插入到右子树
95+
if self.right.is_none() {
96+
self.right = Some(Box::new(TreeNode::new(value)));
97+
} else {
98+
self.right.as_mut().unwrap().insert(value);
9499
}
95100
}
96-
Ordering::Equal => {} // 如果值相等,不做任何操作(避免重复)
97101
}
98102
}
99103
}
@@ -149,4 +153,6 @@ mod tests {
149153
None => panic!("Root should not be None after insertion"),
150154
}
151155
}
152-
}
156+
}
157+
158+

0 commit comments

Comments
 (0)