File tree Expand file tree Collapse file tree 1 file changed +18
-3
lines changed
Expand file tree Collapse file tree 1 file changed +18
-3
lines changed Original file line number Diff line number Diff line change 11/*
22 graph
3- This problem requires you to implement a basic graph functio
3+ This problem requires you to implement a basic graph function
44*/
5- // I AM NOT DONE
65
76use std:: collections:: { HashMap , HashSet } ;
87use std:: fmt;
@@ -30,6 +29,17 @@ impl Graph for UndirectedGraph {
3029 }
3130 fn add_edge ( & mut self , edge : ( & str , & str , i32 ) ) {
3231 //TODO
32+ let ( left_node, right_node, weight) = edge;
33+
34+ self . add_node ( left_node) ;
35+ self . add_node ( right_node) ;
36+
37+ let table = self . adjacency_table_mutable ( ) ;
38+
39+ table. get_mut ( left_node) . unwrap ( ) . push ( ( right_node. to_string ( ) , weight) ) ;
40+ table. get_mut ( right_node) . unwrap ( ) . push ( ( left_node. to_string ( ) , weight) ) ;
41+
42+
3343 }
3444}
3545pub trait Graph {
@@ -38,7 +48,12 @@ pub trait Graph {
3848 fn adjacency_table ( & self ) -> & HashMap < String , Vec < ( String , i32 ) > > ;
3949 fn add_node ( & mut self , node : & str ) -> bool {
4050 //TODO
41- true
51+ if self . contains ( node) {
52+ false
53+ } else {
54+ self . adjacency_table_mutable ( ) . insert ( node. to_string ( ) , Vec :: new ( ) ) ;
55+ true
56+ }
4257 }
4358 fn add_edge ( & mut self , edge : ( & str , & str , i32 ) ) {
4459 //TODO
You can’t perform that action at this time.
0 commit comments