read_tree and tree to string work
This commit is contained in:
82
src/node.rs
82
src/node.rs
@@ -1,5 +1,7 @@
|
||||
use std::collections::VecDeque;
|
||||
|
||||
pub struct Node {
|
||||
pub val: Option<Box<char>>,
|
||||
pub val: Option<char>,
|
||||
pub l: Option<Box<Node>>,
|
||||
pub r: Option<Box<Node>>,
|
||||
pub n: i32,
|
||||
@@ -16,7 +18,7 @@ impl Node {
|
||||
}
|
||||
|
||||
pub fn set_char(&mut self, v: char) {
|
||||
self.val = Some(Box::new(v));
|
||||
self.val = Some(v);
|
||||
}
|
||||
|
||||
pub fn set_counter(&mut self, n: i32) {
|
||||
@@ -42,28 +44,62 @@ 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());
|
||||
pub fn tree_to_string(root: &mut Node) -> (String, Vec<char>) {
|
||||
let mut tree = String::new();
|
||||
let mut chars: Vec<char> = Vec::new();
|
||||
let mut to_do: VecDeque<&mut Node> = VecDeque::new();
|
||||
to_do.push_back(root);
|
||||
|
||||
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());
|
||||
while let Some(n) = to_do.pop_front() {
|
||||
match n.val {
|
||||
Some(c) => {
|
||||
tree.push('0');
|
||||
chars.push(c);
|
||||
}
|
||||
None => {
|
||||
tree.push('1');
|
||||
if let Some(l) = n.l.as_mut() {
|
||||
to_do.push_back(l);
|
||||
}
|
||||
if let Some(r) = n.r.as_mut() {
|
||||
to_do.push_back(r);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
*root
|
||||
|
||||
(tree, chars)
|
||||
}
|
||||
|
||||
pub fn read_tree(binary: &str, chars: &Vec<char>) -> Node {
|
||||
let mut stack_bin = String::from(binary);
|
||||
let mut stack_chars = chars.clone();
|
||||
let mut to_do: VecDeque<&mut Node> = VecDeque::new();
|
||||
let mut root = Node::new();
|
||||
|
||||
if stack_bin.remove(0) == '1' {
|
||||
to_do.push_back(&mut root);
|
||||
} else {
|
||||
root.val = Some(stack_chars.remove(0));
|
||||
return root;
|
||||
}
|
||||
|
||||
while let Some(int_root) = to_do.pop_front() {
|
||||
int_root.insert(Node::new(), Node::new());
|
||||
if let Some(left) = int_root.l.as_mut() {
|
||||
if stack_bin.remove(0) == '1' {
|
||||
to_do.push_back(left);
|
||||
} else {
|
||||
left.val = Some(stack_chars.remove(0));
|
||||
}
|
||||
}
|
||||
if let Some(right) = int_root.r.as_mut() {
|
||||
if stack_bin.remove(0) == '1' {
|
||||
to_do.push_front(right);
|
||||
} else {
|
||||
right.val = Some(stack_chars.remove(0));
|
||||
}
|
||||
}
|
||||
}
|
||||
root
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user