Crates.io | huffman-compression |
lib.rs | huffman-compression |
version | 0.1.2 |
source | src |
created_at | 2024-03-29 23:12:58.469822 |
updated_at | 2024-03-30 09:19:35.442914 |
description | Package to encode and decode Huffman Strings |
homepage | |
repository | https://github.com/humblewolfstudio/huffman-compression |
max_upload_size | |
id | 1190622 |
size | 13,299 |
This is my crude implementation of the Huffman Tree Algorithm.
fn main() {
let text = "Hello world!".to_string();
let huffman_tree = HuffmanTree::new(text.clone());
let encoded_message = huffman_tree.encode(&text);
let decoded_message = huffman_tree.decode(&encoded_message);
println!("Compressed: {}", encoded_message);
println!("Uncompressed: {}", decoded_message);
println!("Huffman Codes: {:#?}", huffman_tree.get_code_table());
}
fn main() {
let huffman_table = HashMap::from([
('o', String::from("100")),
('l', String::from("01")),
('e', String::from("111")),
(' ', String::from("0000")),
('!', String::from("110")),
('w', String::from("101")),
('d', String::from("0011")),
('H', String::from("0010")),
('r', String::from("0001")),
]);
let huffman_tree = HuffmanTree::new_decoder(huffman_table);
let encoded_message = huffman_tree.encode("Hello world!");
let decoded_message = huffman_tree.decode("0010111010110000001011000001010011110");
println!("Compressed: {}", encoded_message);
println!("Uncompressed: {}", decoded_message);
}