/// Takes an input string and the frequency list generated earlier to /// create a Huffman Tree to be used for the compression and /// decompression. It then encodes the provided text string and outputs /// it to the user in a compressed format, which can later be used /// to decompress to verify the result. private void BtnCompressClick(object sender, RoutedEventArgs e) { if (txtPlain.Text.Count() == 0) { MessageBox.Show("Cannot compress a message that doesn't" + " exist!\nPlease enter (1) or more " + "alphanumeric characters"); } else if (nodeList.Count == 0) { MessageBox.Show("Cannot compress text without a frequency" + " table!\nPlease generate a frequency " + "table before compression"); } else { HuffmanGenerator hg = new HuffmanGenerator(nodeList); huffmanTree = hg.CreateTree(); encodeDict = hg.BuildEncodingMap(huffmanTree); HuffmanEncoder he = new HuffmanEncoder(); txtCompressed.Text = he.Encode(txtPlain.Text.ToCharArray(), encodeDict); txtPlain.Text = ""; txtFreqTbl.Text = ""; } }
/// <summary> /// Generates a canonical depth-limited Huffman tree using the provided <paramref name="symbolFrequencies"/>. /// </summary> public static HuffmanTree <T> FromSymbols(FrequencyList <T> symbolFrequencies, byte maxDepth) { if (symbolFrequencies.Count == 0) { throw new ArgumentOutOfRangeException(nameof(symbolFrequencies), "Cannot generate a tree with no symbols."); } else if (symbolFrequencies.Count == 1) { return(new HuffmanTree <T>(new HuffmanNode <T> .Leaf(symbolFrequencies.First()))); } else { return(new HuffmanTree <T>(HuffmanGenerator <T> .FromFrequenciesCanonical(symbolFrequencies.HuffmanFreq.ToArray(), maxDepth))); } }
void AddMarkedSymbol(HuffmanGenerator <T> .Entry entry) { symbolEntries.Add(entry); reader.MarkStart(); reader.MarkEndValue("entry", entry); }