fn build_huffman_tree(frequency_map: &HashMap<u8, i64>) -> Node {
    //Create a Vector of Nodes containing each u8 and their frequency
    let mut freq_list: Vec<Node> = Vec::new();
    for (data, freq) in frequency_map {
        freq_list.push(Node{left: None, right: None,value: Some(*data),freq: *freq});
    }
    //Sort the Vector
    freq_list.sort_by(|a, b| b.value.cmp(&a.value));
    freq_list.sort_by(|a, b| b.freq.cmp(&a.freq));
    
    while freq_list.len() != 1 {
        let  left_node = freq_list.pop().unwrap();
        let  right_node = freq_list.pop().unwrap();
        let  new_node_freq = left_node.freq + right_node.freq;
        let  new_node = Node{left: Some(Box::new(left_node)), right: Some(Box::new(right_node)),value: None,freq: new_node_freq};
        freq_list.push(new_node);
        freq_list.sort_by(|a, b| b.freq.cmp(&a.freq));
    }
    return freq_list.pop().unwrap();
}
//Creates a a Huffman Coding Tree with given Frequency Map
// We sort the frequency list alphabetically then we sort it by frequency to give us consitancy in the tree we generate
Creates a a Huffman Coding Tree with given Frequency Map
We sort the frequency list alphabetically then we sort it by frequency to give us consitancy in the tree we generate
Creates a a Huffman Coding Tree with given Frequency Map We sort the frequency list alphabetically then we sort it by frequency to give us consitancy in the tree we generate
fn build_huffman_tree(frequency_map: &HashMap<u8, i64>) -> Node {
    //Create a Vector of Nodes containing each u8 and their frequency
    let mut freq_list: Vec<Node> = Vec::new();
    for (data, freq) in frequency_map {
        freq_list.push(Node{left: None, right: None,value: Some(*data),freq: *freq});
    }
    //Sort the Vector
    freq_list.sort_by(|a, b| b.value.cmp(&a.value));
    freq_list.sort_by(|a, b| b.freq.cmp(&a.freq));
    while freq_list.len() != 1 {
        let  left_node = freq_list.pop().unwrap();
        let  right_node = freq_list.pop().unwrap();
        let  new_node_freq = left_node.freq + right_node.freq;
        let  new_node = Node{left: Some(Box::new(left_node)), right: Some(Box::new(right_node)),value: None,freq: new_node_freq};
        freq_list.push(new_node);
        freq_list.sort_by(|a, b| b.freq.cmp(&a.freq));
    }
    return freq_list.pop().unwrap();
}
fn build_huffman_tree ( frequency_map : & HashMap < u8 , i64 > ) -> Node { //Create a Vector of Nodes containing each u8 and their frequency let mut freq_list : Vec < Node > = Vec :: new ( ) ; for ( data , freq ) in frequency_map { freq_list . push ( Node { left : None , right : None , value : Some ( * data ) , freq : * freq } ) ; } //Sort the Vector freq_list . sort_by ( | a , b | b . value . cmp ( & a . value ) ) ; freq_list . sort_by ( | a , b | b . freq . cmp ( & a . freq ) ) ; while freq_list . len ( ) != 1 { let left_node = freq_list . pop ( ) . unwrap ( ) ; let right_node = freq_list . pop ( ) . unwrap ( ) ; let new_node_freq = left_node . freq + right_node . freq ; let new_node = Node { left : Some ( Box :: new ( left_node ) ) , right : Some ( Box :: new ( right_node ) ) , value : None , freq : new_node_freq } ; freq_list . push ( new_node ) ; freq_list . sort_by ( | a , b | b . freq . cmp ( & a . freq ) ) ; } return freq_list . pop ( ) . unwrap ( ) ; }
Creates a a Huffman Coding Tree with given Frequency Map
We sort the frequency list alphabetically then we sort it by frequency to give us consitancy in the tree we generate
Creates a a Huffman Coding Tree with given Frequency Map We sort the frequency list alphabetically then we sort it by frequency to give us consitancy in the tree we generate
frequency_map
&HashMap<u8, i64>
frequency_map
&HashMap<u8, i64>