/// <summary> /// Swaps the data of two nodes /// </summary> /// <param name="a">First node to swap.</param> /// <param name="b">Second node to swap.</param> public void swap(int a, int b) { HuffmanTreeNode tmp = heapArray[a]; heapArray[a] = heapArray[b]; heapArray[b] = tmp; }
/// <summary> /// Equivilant of Build_Max_Heap. Takes an unsorted array and turns into heap /// </summary> /// <param name="array">Array to heapify.</param> /// <returns>Heapified Array</returns> public Heap(HuffmanTreeNode[] array) { heapArray = new HuffmanTreeNode[array.Length + 1]; Array.Copy(array, heapArray, array.Length); size = array.Length; for (int ii = size - 1; ii >= 0; ii--) this.minHeapify(ii); }
/// <summary> /// Constructor for building a Composite node from two nodes. /// Also makes the new node their parent. /// </summary> /// <param name="left">The left node.</param> /// <param name="right">The right node.</param> /// <returns>A HuffmanTreeNodeNode with two children and a frequency that is the sum of the children.</returns>f public HuffmanTreeNodeComposite(HuffmanTreeNode left, HuffmanTreeNode right) { freq = left.freq + right.freq; this.left = left; this.right = right; left.parent = this; right.parent = this; left.bit = true; right.bit = false; }
/// <summary> /// Extract item with smallest priority and the heapify the heap. /// </summary> /// <returns>Root node of the heap</returns> public HuffmanTreeNode extractMin() { HuffmanTreeNode max = null; if (!isEmpty()) { max = heapArray[0]; heapArray[0] = heapArray[size - 1]; size--; this.minHeapify(0); } return(max); }
/// <summary> /// Insert a node into the heap preserving heap status /// </summary> /// <param name="item">Data to insert.</param> public void insert(HuffmanTreeNode item) { size++; int index = size - 1; heapArray[index] = item; while (index > 0 && heapArray[parent(index)].freq > item.freq) { heapArray[index] = heapArray[parent(index)]; index = parent(index); } heapArray[index] = item; }
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); } }
/// <summary> /// Factory for building a HuffmanTreeNodeComposite given a both of it's children. /// </summary> /// <param name="left">Right child of node.</param> /// <param name="right">Left child of node.</param> /// <returns>A HuffmanTreeNodeComposite with the fields passed to it as it's children and the frequency as the sum of them both.</returns> public static HuffmanTreeNodeComposite HuffmanTreeFactory(HuffmanTreeNode left, HuffmanTreeNode right) { return(new HuffmanTreeNodeComposite(left, right)); }
/// <summary> /// Factory for building a HuffmanTreeNodeComposite given a both of it's children. /// </summary> /// <param name="left">Right child of node.</param> /// <param name="right">Left child of node.</param> /// <returns>A HuffmanTreeNodeComposite with the fields passed to it as it's children and the frequency as the sum of them both.</returns> public static HuffmanTreeNodeComposite HuffmanTreeFactory(HuffmanTreeNode left, HuffmanTreeNode right) { return new HuffmanTreeNodeComposite(left, right); }