/// <summary> /// Factory for building a HuffmanTree from a frequency list. /// Also builds up a Dictionary of the nodes for encoding. /// </summary> /// <param name="freqlist">List of frequencies of characters.</param> /// <param name="htDict">Empty dictionary to be filled.</param> /// <returns>A Composite HuffmanTreeNode which is the root of the tree. Also passes a dictionary of the nodes by reference.</returns> public static HuffmanTreeNodeComposite HuffmanTreeFactory(List <Frequency> freqlist, Dictionary <char, HuffmanTreeNodeLeaf> htDict) { foreach (Frequency element in freqlist) { HuffmanTreeNodeLeaf newLeaf = HuffmanTreeFactory(element.symbol, element.frequency); htDict.Add(newLeaf.symbol, newLeaf); } HuffmanTreeNode[] nodeArray = htDict.Values.ToArray(); //swapped for more dynamic function which uses the array /*Heap heap = new Heap(); * for (int ii = 0; ii < nodeArray.Length; ii++) * { * if (nodeArray[ii] != null) * heap.insert(nodeArray[ii]); * }*/ Heap heap = new Heap(nodeArray); HuffmanTreeNodeComposite root = null; HuffmanTreeNode left; HuffmanTreeNode right; while (heap.size > 1) { left = heap.extractMin(); right = heap.extractMin(); root = HuffmanTreeFactory(left, right); if (heap.size > 0) { heap.insert(root); } } root.parent = null; return(root); }
/// <summary> /// Event handler for when the Decompress button is clicked. /// Uses the frequency table to build a HuffmanTree then uses that tree to decode the data. /// The frequency table must be the same as the one to encode it or bad things happen. /// </summary> /// <param name="sender">Event Stuff (Don't ask me, I didn't put it there?</param> /// <param name="e">State information</param> private void btnDecompress_Click(object sender, RoutedEventArgs e) { try { string[] freqStringList = txtFreqTbl.Text.Split('\n'); List <Frequency> frequencies = new List <Frequency>(); foreach (string s in freqStringList) { frequencies.Add(new Frequency(s)); } HuffmanTreeNodeComposite root = HuffmanTreeNode.HuffmanTreeFactory(frequencies, new Dictionary <char, HuffmanTreeNodeLeaf>()); Dictionary <char, DAABitArray> dict = new Dictionary <char, DAABitArray>(); for (int ii = 0; ii < 64; ii++) { long x = (long)ii; DAABitArray bits = new DAABitArray(); bits.Append(x, 6); dict.Add(DAABitArray.encoding[ii], bits); } DAABitArray encodedBits = new DAABitArray(); foreach (char character in txtCompressed.Text) { encodedBits.Append(dict[character]); } string decoded = ""; while (encodedBits.NumBits > 0) { decoded = decoded + root.decode(encodedBits).ToString(); } txtPlain.Text = decoded; } catch (Exception ex) { MessageBox.Show(ex.Message); } }
/// <summary> /// Event handler for when the Compress button is clicked. /// Uses the frequency table to build a HuffmanTree and a Dictionary of nodes /// then converts the symbols to DAABitArrays using the Dictionary to find the node. /// Also appends 0 bits to make the entire bit sequence divisable by 6. /// The frequency table must be the same as the one to encode it or bad things happen. /// </summary> /// <param name="sender">Event Stuff (Don't ask me, I didn't put it there?</param> /// <param name="e">State information</param> private void btnCompress_Click(object sender, RoutedEventArgs e) { //try { string[] freqStringList = txtFreqTbl.Text.Split('\n'); List <Frequency> frequencies = new List <Frequency>(); foreach (string s in freqStringList) { frequencies.Add(new Frequency(s)); } Dictionary <char, HuffmanTreeNodeLeaf> dict = new Dictionary <char, HuffmanTreeNodeLeaf>(); HuffmanTreeNodeComposite root = HuffmanTreeNode.HuffmanTreeFactory(frequencies, dict); DAABitArray bits = new DAABitArray(); string converted = ""; foreach (char character in txtPlain.Text) { DAABitArray append = new DAABitArray(); bits.Append(dict[character].encode(append)); } while (bits.NumBits % 6 != 0) { bits.Append(false); } int index = 0; while (index <= bits.NumBits - 6) { converted = converted + bits.SixToChar(index); index = index + 6; } txtCompressed.Text = converted; } //catch (Exception ex) { // MessageBox.Show(ex.Message); } }