Skip to content

Commit b1b820c

Browse files
committed
Minor AdjacencyGraph Changes
1 parent a2b94ae commit b1b820c

File tree

3 files changed

+37
-9
lines changed

3 files changed

+37
-9
lines changed

src/adjacency_graph.rs

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,22 @@ use std::collections::HashSet;
33

44
#[derive(Clone, Debug, PartialEq)]
55
pub struct AdjacencyGraph {
6+
n: usize,
67
neighbors: HashMap<i32, HashSet<i32>>,
78
}
89

910
impl AdjacencyGraph {
1011

11-
pub fn new() -> Self {
12-
Self { neighbors: HashMap::new() }
12+
pub fn new(n: usize) -> Self {
13+
Self { n, neighbors: HashMap::new() }
1314
}
1415

15-
pub fn from_neighbors(neighbors: &HashMap<i32, HashSet<i32>>) -> Self {
16-
Self { neighbors: neighbors.clone() }
16+
pub fn from_neighbors(n: usize, neighbors: &HashMap<i32, HashSet<i32>>) -> Self {
17+
Self { n, neighbors: neighbors.clone() }
1718
}
1819

19-
pub fn from_vec(edges: &Vec<Vec<i32>>) -> Self {
20-
let mut result = Self::new();
20+
pub fn from_vec(n: usize, edges: &Vec<Vec<i32>>) -> Self {
21+
let mut result = Self::new(n);
2122
for edge in edges {
2223
result.add_edge(edge[0], edge[1]);
2324
}
@@ -69,7 +70,9 @@ impl AdjacencyGraph {
6970
}
7071

7172
pub fn find_centroids(&self) -> Vec<i32> {
72-
if self.neighbors.len() <= 2 {
73+
if self.neighbors.len() == 0 {
74+
(0..self.n).into_iter().map(|i| i as i32).collect()
75+
} else if self.neighbors.len() <= 2 {
7376
self.neighbors.keys().copied().collect()
7477
} else {
7578
let mut cloned = self.clone();
@@ -93,4 +96,19 @@ impl AdjacencyGraph {
9396
}
9497
}
9598

99+
pub fn vertices_len(&self) -> usize {
100+
self.n
101+
}
102+
103+
pub fn edges_len(&self) -> usize {
104+
self.neighbors.len()
105+
}
106+
107+
pub fn adj(&self, x: i32) -> HashSet<i32> {
108+
self.neighbors
109+
.get(&x)
110+
.cloned()
111+
.unwrap_or(HashSet::new())
112+
}
113+
96114
}

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ pub mod binary_tree_right_side_view; // 199
9292
pub mod number_of_islands; // 200
9393

9494
pub mod reverse_linked_list; // 206
95+
pub mod course_schedule; // 207
9596

9697
pub mod contains_duplicate; // 217
9798

src/minimum_height_trees.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ struct Solution;
2424

2525
impl Solution {
2626

27-
pub fn find_min_height_tree(_n: i32, edges: Vec<Vec<i32>>) -> Vec<i32> {
28-
let graph = AdjacencyGraph::from_vec(&edges);
27+
pub fn find_min_height_tree(n: i32, edges: Vec<Vec<i32>>) -> Vec<i32> {
28+
let n = n as usize;
29+
let graph = AdjacencyGraph::from_vec(n, &edges);
2930
graph.find_centroids()
3031
}
3132

@@ -52,4 +53,12 @@ mod tests {
5253
assert_eq!(result, vec![3,4]);
5354
}
5455

56+
#[test]
57+
fn no_edges() {
58+
let n = 1;
59+
let edges = vec![];
60+
let result = Solution::find_min_height_tree(n, edges);
61+
assert_eq!(result, vec![0]);
62+
}
63+
5564
}

0 commit comments

Comments
 (0)