binary to tree
This commit is contained in:
26
src/node.rs
26
src/node.rs
@@ -41,3 +41,29 @@ pub fn create_tree(mut nodes: Vec<Node>) -> Node {
|
||||
}
|
||||
nodes.pop().unwrap()
|
||||
}
|
||||
|
||||
pub fn read_tree(binary: &str) -> Node {
|
||||
let mut stack = String::from(binary);
|
||||
let mut to_do: Vec<&mut Box<Node>> = Vec::new();
|
||||
let mut root = Box::new(Node::new());
|
||||
|
||||
if stack.remove(0) == '1' {
|
||||
to_do.push(&mut root);
|
||||
} else {
|
||||
return *root;
|
||||
}
|
||||
|
||||
while !to_do.is_empty() {
|
||||
let mut int_root = to_do.remove(0);
|
||||
let mut l = Box::new(Node::new());
|
||||
let mut r = Box::new(Node::new());
|
||||
if stack.remove(0) == '1' {
|
||||
int_root.l = Some(l);
|
||||
to_do.push(int_root.l.as_mut().unwrap());
|
||||
}
|
||||
if stack.remove(0) == '1' {
|
||||
to_do.push(int_root.r.as_mut().unwrap());
|
||||
}
|
||||
}
|
||||
*root
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user