/// <summary> /// The Compress Method Using Huffman Algorithm Tree /// Create a coded byte[] from the data Given /// </summary> /// <param name="data">File To Compress</param> /// <returns>coded byte[] from the data Given</returns> public override byte[] Compress(byte[] data) { charFrequency = Get_Frequency(data); BinaryHeap <HChar> PriorityQueue = CreatePriorityQueue(charFrequency); HChar root = PriorityQueue.Remove(); SetBinRepDFS(root, ""); //adding the paris to array //110100110000111 ArrayList dictinary = new ArrayList(); foreach (KeyValuePair <int, HChar> entry in charFrequency) { dictinary.Add(entry.Key); dictinary.Add(entry.Value.GetBinaryRepresentation()); } // {1} size of dictinury byte[] dicSize = new byte[4]; int m_idicSize = charFrequency.Count; dicSize = BitConverter.GetBytes(m_idicSize); // {2} byte[] m_arrDic, m_tempL, m_tempVal, m_tempKey; m_arrDic = new byte[0]; foreach (var item in charFrequency) { m_tempKey = BitConverter.GetBytes(item.Value.GetCharacter());//6 m_tempL = BitConverter.GetBytes(item.Value.GetBinaryRepresentation().Length); m_tempVal = GetBytes(item.Value.GetBinaryRepresentation());//0010 m_arrDic = Combine(m_arrDic, m_tempKey, m_tempL, m_tempVal); } // {3} string totrans = ""; for (int i = 0; i < data.Length; i++) { totrans += charFrequency[data[i]].GetBinaryRepresentation(); } byte[] lang = new byte[4]; lang = BitConverter.GetBytes(totrans.Length); byte[] arrByteCode = to_binary(totrans); //uinon {1}{2}{3} return(Combine(dicSize, m_arrDic, lang, arrByteCode)); }
/// <summary> /// Set the Binary Representation of a certen node in the huffman tree /// Using Recurse Better using on the Root /// </summary> /// <param name="node">A node on the Tree</param> /// <param name="bin"></param> private void SetBinRepDFS(HChar node, string bin) { node.SetBinaryRepresentation(bin); if (node.GetLeft() != null) { SetBinRepDFS(node.GetLeft(), node.GetBinaryRepresentation() + "0"); } if (node.GetRight() != null) { SetBinRepDFS(node.GetRight(), node.GetBinaryRepresentation() + "1"); } }
/// <summary> /// Count the amount of time a certen token apper in the Byte[] data /// /// </summary> /// <param name="data">Arry of byte of a given file</param> /// <returns>Return Dictinury with a Count As a Key and HChar As a Value</returns> private Dictionary <int, HChar> Get_Frequency(byte[] data) { Dictionary <int, HChar> charFrequency = new Dictionary <int, HChar>(); HChar hchar; foreach (byte character in data) { if (!charFrequency.ContainsKey(character)) { hchar = new HChar(character, 1); node_counter++; charFrequency.Add(character, hchar); } else { hchar = charFrequency[character]; hchar.SetCount(hchar.GetCount() + 1); } } return(charFrequency); }
/// <summary> /// Building the Huffman Tree Using A BinaryHeap /// </summary> /// <param name="charFrequency">Dictunery with the amount of every Val</param> /// <returns>The Huffman Tree </returns> private BinaryHeap <HChar> CreatePriorityQueue(Dictionary <int, HChar> charFrequency) { BinaryHeap <HChar> PriorityQueue = new BinaryHeap <HChar>(); foreach (KeyValuePair <int, HChar> entry in charFrequency) { PriorityQueue.Add(entry.Value); } HChar first; HChar second; HChar newOne; while (PriorityQueue.Count > 1) { first = PriorityQueue.Remove(); second = PriorityQueue.Remove(); newOne = new HChar((byte)(first.GetCharacter() + second.GetCharacter()), first.GetCount() + second.GetCount()); node_counter++; newOne.SetLeft(first); newOne.SetRight(second); PriorityQueue.Add(newOne); } return(PriorityQueue); }