private static void PrefixCodes(HuffmanNode Node, string traversal) { if (Node.IsLeaf()) { CharacterTable.Add(traversal, Node.Fact); return; } else { if (Node.LeftNode != null) { PrefixCodes(Node.LeftNode, traversal + "0"); } if (Node.RightNode != null) { PrefixCodes(Node.RightNode, traversal + "1"); } } }
private static void PrefixCodes(HuffmanNode Node, string Route) { if (Node.IsLeaf()) { CharacterTable.Add(Node.Fact, Route); } else { if (Node.LeftNode != null) { PrefixCodes(Node.LeftNode, Route + "0"); } if (Node.RightNode != null) { PrefixCodes(Node.RightNode, Route + "1"); } } }
static void HuffmanTree(string path) { using var nameJumper = new StreamReader(path); var position = nameJumper.ReadLine().Length; nameJumper.Close(); using (var File = new FileStream(path, FileMode.Open)) { File.Position = position + 1; int separator1 = 0; var buffer = new byte[BufferLenght]; string Data_Lenght1 = ""; string frequency = ""; string Datamount = ""; int final = 0; byte bit = new byte(); using (var reader = new BinaryReader(File)) { while (reader.BaseStream.Position != reader.BaseStream.Length) { buffer = reader.ReadBytes(BufferLenght); foreach (var item in buffer) { if (separator1 == 0) { if (Convert.ToChar(item) == '|' || Convert.ToChar(item) == 'ÿ' || Convert.ToChar(item) == 'ß') { separator1 = 1; if (Convert.ToChar(item) == '|') { Separator = '|'; } else if (Convert.ToChar(item) == 'ÿ') { Separator = 'ÿ'; } else { Separator = 'ß'; } } else { Data_Lenght1 += Convert.ToChar(item).ToString(); } } else if (separator1 == 2) { break; } else { if (final == 1 && Convert.ToChar(item) == Separator) { final = 2; separator1 = 2; } else { final = 0; } if (Datamount == "") { Datamount = Convert.ToChar(item).ToString(); bit = item; } else if (Convert.ToChar(item) == Separator && final == 0) { FrequencyTable.Add(bit, Convert.ToInt32(frequency)); Datamount = ""; frequency = ""; final = 1; } else { frequency += Convert.ToChar(item).ToString(); } } } } } DataLenght = Convert.ToDecimal(Data_Lenght1); } List <HuffmanNode> FrequencyList = new List <HuffmanNode>(); foreach (KeyValuePair <byte, int> Nodes in FrequencyTable) { FrequencyList.Add(new HuffmanNode(Nodes.Key, Convert.ToDecimal(Nodes.Value) / DataLenght)); } FrequencyList = FrequencyList.OrderBy(x => x.Probability).ToList(); while (FrequencyList.Count > 1) { FrequencyList = FrequencyList.OrderBy(x => x.Probability).ToList(); HuffmanNode Link = LinkNodes(FrequencyList[1], FrequencyList[0]); FrequencyList.RemoveRange(0, 2); FrequencyList.Add(Link); } Root = FrequencyList[0]; }