From 886f1755eceb75f41c1a07cbcfc7ea1b0cc715f7 Mon Sep 17 00:00:00 2001 From: sriram98v Date: Fri, 13 Oct 2023 11:45:51 -0500 Subject: [PATCH] method to return path iterator in preorder --- src/suffix_tree.rs | 18 ++++++++++++++++-- tests/tests.rs | 16 +++++++++++++--- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/src/suffix_tree.rs b/src/suffix_tree.rs index e13aca5..4a3c1b5 100644 --- a/src/suffix_tree.rs +++ b/src/suffix_tree.rs @@ -444,6 +444,10 @@ where PostOrdNodes::new(&self.root, &self.nodes) } + pub fn iter_path_pre(&self, node_id: &NodeID)->std::collections::linked_list::IntoIter{ + self.get_node_path(node_id).into_iter() + } + /// Returns a postorder edge iterator of the tree pub fn iter_edges_post(&self)->PostOrdEdges{ PostOrdEdges::new(&self.root, &self.nodes, self.suffix_links.clone(), self.nodes.iter() @@ -485,8 +489,18 @@ where fn get_node_path_label(&self, _node_id: &NodeID)->&[T]{ todo!(); } - fn get_node_path(&self, _node_id: &NodeID)->LinkedList{ - todo!(); + + fn get_node_path(&self, node_id: &NodeID)->LinkedList{ + let mut node_path: LinkedList = LinkedList::new(); + let mut curr_node_id: usize = node_id.clone(); + while self.get_node_parent(&curr_node_id).expect("Invalid NodeID! Path is broken")!=&0{ + node_path.push_front(curr_node_id.clone()); + curr_node_id = self.get_node_parent(&curr_node_id).cloned().expect("Invalid NodeID! Path is broken"); + } + node_path.push_front(curr_node_id); + node_path.push_front(0); + node_path + } fn is_suffix(&self, s:&[T])->bool{ diff --git a/tests/tests.rs b/tests/tests.rs index a039591..e01a46f 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -67,14 +67,24 @@ fn postorder_nodes(){ assert_eq!(node_post.collect::>(), vec![16, 15, 14, 3, 13, 8, 12, 2, 11, 7, 6, 10, 1, 9, 5, 4, 0]); } +// #[test] +// fn postorder_edges(){ +// let mut tree: KGST = KGST::new('$'); +// let item_string:Vec = "abcabxabcd".chars().collect(); +// let item_id:String = "World".to_string(); +// tree.insert(item_id.clone(), item_string.clone(), &0); +// let edge_post = tree.iter_edges_post(); +// dbg!(edge_post.collect::>()); +// } + #[test] -fn postorder_edges(){ +fn node_paths(){ let mut tree: KGST = KGST::new('$'); let item_string:Vec = "abcabxabcd".chars().collect(); let item_id:String = "World".to_string(); tree.insert(item_id.clone(), item_string.clone(), &0); - let edge_post = tree.iter_edges_post(); - dbg!(edge_post.collect::>()); + let edge_post = tree.iter_path_pre(&12); + dbg!(edge_post.collect::>()); } // #[test]