read_tree and tree to string work
This commit is contained in:
18
src/main.rs
18
src/main.rs
@@ -7,7 +7,7 @@ mod byte_writer;
|
|||||||
mod node;
|
mod node;
|
||||||
|
|
||||||
use byte_writer::{binary_to_string, string_to_binary};
|
use byte_writer::{binary_to_string, string_to_binary};
|
||||||
use node::Node;
|
use node::{Node, read_tree, tree_to_string};
|
||||||
|
|
||||||
fn main() -> std::io::Result<()> {
|
fn main() -> std::io::Result<()> {
|
||||||
let args: Vec<String> = env::args().collect();
|
let args: Vec<String> = env::args().collect();
|
||||||
@@ -19,12 +19,19 @@ fn main() -> std::io::Result<()> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let file = File::open("test.txt")?;
|
let file = File::open("test.txt")?;
|
||||||
let mut file2 = File::open("fo.txt")?;
|
//let mut file2 = File::open("fo.txt")?;
|
||||||
|
|
||||||
let map = get_map_file(file).expect("Failed to load!");
|
let map = get_map_file(file).expect("Failed to load!");
|
||||||
let vec = nodes_at_vec(map);
|
let vec = nodes_at_vec(map);
|
||||||
let root = node::create_tree(vec);
|
let mut root = node::create_tree(vec);
|
||||||
eprintln!("root.n = {:#?}", root.n);
|
//eprintln!("root.n = {:#?}", root.l.unwrap().n);
|
||||||
|
let txt = tree_to_string(&mut root);
|
||||||
|
//eprintln!("txt = {:?}", txt);
|
||||||
|
let mut root2 = read_tree(&txt.0, &txt.1);
|
||||||
|
eprintln!(
|
||||||
|
"root2.l.unwrap().l.unwrap().val = {:?}",
|
||||||
|
root2.l.unwrap().l.unwrap().val
|
||||||
|
);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
let binary = "101100001";
|
let binary = "101100001";
|
||||||
@@ -38,7 +45,7 @@ fn main() -> std::io::Result<()> {
|
|||||||
println!("101100001");
|
println!("101100001");
|
||||||
file2.write_all(&len_bytes)?;
|
file2.write_all(&len_bytes)?;
|
||||||
file2.write_all(&v)?;*/
|
file2.write_all(&v)?;*/
|
||||||
|
/*
|
||||||
let mut buffer = [0u8; 1];
|
let mut buffer = [0u8; 1];
|
||||||
file2.read_exact(&mut buffer)?;
|
file2.read_exact(&mut buffer)?;
|
||||||
eprintln!("buffer = {:?}", buffer);
|
eprintln!("buffer = {:?}", buffer);
|
||||||
@@ -51,6 +58,7 @@ fn main() -> std::io::Result<()> {
|
|||||||
//let current = read[4..].to_vec();
|
//let current = read[4..].to_vec();
|
||||||
let string = binary_to_string(read, len);
|
let string = binary_to_string(read, len);
|
||||||
eprintln!("string = {:?}", string);
|
eprintln!("string = {:?}", string);
|
||||||
|
*/
|
||||||
|
|
||||||
//eprintln!("v = {:#?}", v);
|
//eprintln!("v = {:#?}", v);
|
||||||
//eprintln!("buffer = {:#?}", buffer);
|
//eprintln!("buffer = {:#?}", buffer);
|
||||||
|
|||||||
82
src/node.rs
82
src/node.rs
@@ -1,5 +1,7 @@
|
|||||||
|
use std::collections::VecDeque;
|
||||||
|
|
||||||
pub struct Node {
|
pub struct Node {
|
||||||
pub val: Option<Box<char>>,
|
pub val: Option<char>,
|
||||||
pub l: Option<Box<Node>>,
|
pub l: Option<Box<Node>>,
|
||||||
pub r: Option<Box<Node>>,
|
pub r: Option<Box<Node>>,
|
||||||
pub n: i32,
|
pub n: i32,
|
||||||
@@ -16,7 +18,7 @@ impl Node {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_char(&mut self, v: char) {
|
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) {
|
pub fn set_counter(&mut self, n: i32) {
|
||||||
@@ -42,28 +44,62 @@ pub fn create_tree(mut nodes: Vec<Node>) -> Node {
|
|||||||
nodes.pop().unwrap()
|
nodes.pop().unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn read_tree(binary: &str) -> Node {
|
pub fn tree_to_string(root: &mut Node) -> (String, Vec<char>) {
|
||||||
let mut stack = String::from(binary);
|
let mut tree = String::new();
|
||||||
let mut to_do: Vec<&mut Box<Node>> = Vec::new();
|
let mut chars: Vec<char> = Vec::new();
|
||||||
let mut root = Box::new(Node::new());
|
let mut to_do: VecDeque<&mut Node> = VecDeque::new();
|
||||||
|
to_do.push_back(root);
|
||||||
|
|
||||||
if stack.remove(0) == '1' {
|
while let Some(n) = to_do.pop_front() {
|
||||||
to_do.push(&mut root);
|
match n.val {
|
||||||
} else {
|
Some(c) => {
|
||||||
return *root;
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
while !to_do.is_empty() {
|
(tree, chars)
|
||||||
let mut int_root = to_do.remove(0);
|
}
|
||||||
let mut l = Box::new(Node::new());
|
|
||||||
let mut r = Box::new(Node::new());
|
pub fn read_tree(binary: &str, chars: &Vec<char>) -> Node {
|
||||||
if stack.remove(0) == '1' {
|
let mut stack_bin = String::from(binary);
|
||||||
int_root.l = Some(l);
|
let mut stack_chars = chars.clone();
|
||||||
to_do.push(int_root.l.as_mut().unwrap());
|
let mut to_do: VecDeque<&mut Node> = VecDeque::new();
|
||||||
}
|
let mut root = Node::new();
|
||||||
if stack.remove(0) == '1' {
|
|
||||||
to_do.push(int_root.r.as_mut().unwrap());
|
if stack_bin.remove(0) == '1' {
|
||||||
}
|
to_do.push_back(&mut root);
|
||||||
}
|
} else {
|
||||||
*root
|
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