示例#1
0
        public static Dictionary <byte?, string> CreateHuffmanCodes(Node node, string NodePath, StringBuilder currentString = null)
        {
            StringBuilder temp = (currentString == null) ? new StringBuilder() : new StringBuilder(currentString.ToString());

            temp.Append(NodePath);
            if (node != null)
            {
                if (node.b == null)
                {
                    CreateHuffmanCodes(node.left, "0", temp);
                    CreateHuffmanCodes(node.right, "1", temp);
                }
                else
                {
                    HuffmanCodes.Add(node.b, temp.ToString());
                }
            }
            return(HuffmanCodes);
        }
示例#2
0
        public static void GetMinMaxLengthHuffmanCode()
        {
            var huffman  = new HuffmanCodes();
            var encoding = huffman.GetCharacterEncoding(ParseCharWeightsFromWeb());

            var max = 0;
            var min = int.MaxValue;

            foreach (var code in encoding.Values)
            {
                if (code.Length > max)
                {
                    max = code.Length;
                }
                if (code.Length < min)
                {
                    min = code.Length;
                }
            }

            Console.WriteLine($"Maximum length of encoding is {max}\nMinimum length of encoding is {min}");
        }