read_tree and tree to string work

This commit is contained in:
2025-04-08 19:26:27 +02:00
parent 4c00c14258
commit d7e13bf84f
2 changed files with 72 additions and 28 deletions

View File

@@ -7,7 +7,7 @@ mod byte_writer;
mod node;
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<()> {
let args: Vec<String> = env::args().collect();
@@ -19,12 +19,19 @@ fn main() -> std::io::Result<()> {
}
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 vec = nodes_at_vec(map);
let root = node::create_tree(vec);
eprintln!("root.n = {:#?}", root.n);
let mut root = node::create_tree(vec);
//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";
@@ -38,7 +45,7 @@ fn main() -> std::io::Result<()> {
println!("101100001");
file2.write_all(&len_bytes)?;
file2.write_all(&v)?;*/
/*
let mut buffer = [0u8; 1];
file2.read_exact(&mut buffer)?;
eprintln!("buffer = {:?}", buffer);
@@ -51,6 +58,7 @@ fn main() -> std::io::Result<()> {
//let current = read[4..].to_vec();
let string = binary_to_string(read, len);
eprintln!("string = {:?}", string);
*/
//eprintln!("v = {:#?}", v);
//eprintln!("buffer = {:#?}", buffer);

View File

@@ -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 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);
}
}
}
}
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
(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
}