public static int start() { Debug.Print("TESTING HEAP"); Random rand = new Random(); List <HuffmanTreeNode> huff = new List <HuffmanTreeNode>(); for (int ii = 0; ii < 100; ii++) { huff.Add(HuffmanTreeNode.HuffmanTreeFactory('x', rand.Next(256))); } Heap h = new Heap(huff.ToArray()); for (int ii = 0; ii < 40; ii++) { h.extractMin(); } Debug.Print(h.size.ToString()); for (int ii = 0; ii < 40; ii++) { h.insert(HuffmanTreeNode.HuffmanTreeFactory('x', rand.Next(256))); } Debug.Print(h.size.ToString()); double one = h.extractMin().freq; double two; while (h.size > 0) { two = h.extractMin().freq; if (one > two) { Debug.Print("FAILED " + one.ToString() + " " + two.ToString()); } one = two; } Debug.Print("PASSED"); return(0); }
/// <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); } }