binary to tree

This commit is contained in:
2025-04-08 17:33:52 +02:00
parent 7f3a0b20d1
commit 4c00c14258
2 changed files with 32 additions and 8 deletions

View File

@@ -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
}