Commit bafd9542 authored by tgerstel's avatar tgerstel
Browse files

obtain sequence from bintree

parent 31dbdca4
......@@ -119,6 +119,31 @@ impl<V> Pool<Node<V>> {
}
}
// assume that u is the root of its binary tree
#[cfg(test)]
fn sequence(&self, u: Label<Node<V>>) -> Vec<Label<Node<V>>> {
let mut queue = vec![u];
let mut seq = vec![u];
while let Some(label) = queue.pop() {
if let Parent::BinTree(parent) = self[label].parent {
let index = seq.iter().position(|&label| label == parent).unwrap();
if self.side(label) {
seq.insert(index + 1, label);
} else {
seq.insert(index, label);
}
}
let Children(a_opt, b_opt) = self[label].children;
if let Some(a) = a_opt {
queue.push(a);
}
if let Some(b) = b_opt {
queue.push(b);
}
}
seq
}
pub fn value_mut(&mut self, u: Label<Node<V>>) -> &mut V {
&mut self[u].value
}
......@@ -231,7 +256,6 @@ impl<V> Pool<Node<V>> {
false
}
} else {
//dbg!(&self[u]);
unreachable!();
}
}
......@@ -484,4 +508,32 @@ mod tests {
dbg!(&lct);
assert_eq!(lct.find_root(g), o);
}
#[test]
fn basic_sequence() {
let mut lct = Pool::with_capacity(7);
// define nodes
let o = lct.add_node("node");
let p = lct.add_node("parent");
let g = lct.add_node("grandparent");
let a = lct.add_node("a");
let b = lct.add_node("b");
let c = lct.add_node("c");
let d = lct.add_node("d");
// link nodes
lct[o].children = Children(Some(a), Some(b));
lct[p].children = Children(Some(o), Some(c));
lct[g].children = Children(Some(p), Some(d));
lct[o].parent = Parent::BinTree(p);
lct[p].parent = Parent::BinTree(g);
lct[a].parent = Parent::BinTree(o);
lct[b].parent = Parent::BinTree(o);
lct[c].parent = Parent::BinTree(p);
lct[d].parent = Parent::BinTree(g);
dbg!(lct.sequence(g));
assert!(false);
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment