Skip to content

Commit

Permalink
method to return path iterator in preorder
Browse files Browse the repository at this point in the history
  • Loading branch information
sriram98v committed Oct 13, 2023
1 parent 1eedbff commit 886f175
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
18 changes: 16 additions & 2 deletions src/suffix_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<usize>{
self.get_node_path(node_id).into_iter()
}

/// Returns a postorder edge iterator of the tree
pub fn iter_edges_post(&self)->PostOrdEdges<T>{
PostOrdEdges::new(&self.root, &self.nodes, self.suffix_links.clone(), self.nodes.iter()
Expand Down Expand Up @@ -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<NodeID>{
todo!();

fn get_node_path(&self, node_id: &NodeID)->LinkedList<NodeID>{
let mut node_path: LinkedList<NodeID> = 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{
Expand Down
16 changes: 13 additions & 3 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,24 @@ fn postorder_nodes(){
assert_eq!(node_post.collect::<Vec<usize>>(), 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<char, String> = KGST::new('$');
// let item_string:Vec<char> = "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::<Vec<(usize, usize)>>());
// }

#[test]
fn postorder_edges(){
fn node_paths(){
let mut tree: KGST<char, String> = KGST::new('$');
let item_string:Vec<char> = "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::<Vec<(usize, usize)>>());
let edge_post = tree.iter_path_pre(&12);
dbg!(edge_post.collect::<Vec<usize>>());
}

// #[test]
Expand Down

0 comments on commit 886f175

Please sign in to comment.