public HuffmanNodes BuildTree(Dictionary <string, int> freq, FrequencyTable frequencyTable) { int count = frequencyTable.DictionaryLength(); foreach (KeyValuePair <string, int> pair in freq) { frequencyTable.Enqueue(new HuffmanNodes { Key = pair.Key, Value = pair.Value }); // moves into the queue } Queue <HuffmanNodes> q = frequencyTable.queue; while (frequencyTable.count > 1) //while there's still elements { HuffmanNodes n1 = frequencyTable.queue.Dequeue(); frequencyTable.count--; HuffmanNodes n2 = frequencyTable.queue.Dequeue(); frequencyTable.count--; HuffmanNodes n3 = new HuffmanNodes { Left = n1, Right = n2, Value = n1.Value + n2.Value }; //adds together n1.Parent = n3; n2.Parent = n3; frequencyTable.Enqueue(n3); } q = frequencyTable.queue; root = q.Dequeue(); return(root); }
private void Encode(HuffmanNodes root, string path, IDictionary <string, string> code) { if (root.Left != null) // if there's something there { Encode(root.Left, path + "0", code); Encode(root.Right, path + "1", code); } else { int index = RemoveZero(path); code.Add(root.Key, path.Substring(index)); } }
internal void Enqueue(HuffmanNodes huffmanNodes) { count++; queue.Enqueue(huffmanNodes); }