Esempio n. 1
0
        private static void GenerateCodes(HuffmanTreeNode node, uint code, uint depth)
        {
            if (node.IsLeaf())
            {
                node.NumBits = depth;
                node.Code    = code;
            }
            else
            {
                var leftCode = code & ~(1U << (int)depth);
                GenerateCodes(node.Left, leftCode, depth + 1);

                var rightCode = code | (1U << (int)depth);
                GenerateCodes(node.Right, rightCode, depth + 1);
            }
        }
Esempio n. 2
0
        public static void Build()
        {
            if (TablesBuilt)
            {
                return;
            }

            TablesBuilt = true;

            var nodes     = new HuffmanTreeNode[256];
            var currCount = 256;

            for (var i = 0; i < 256; ++i)
            {
                nodes[i] = new HuffmanTreeNode
                {
                    Symbol    = (byte)i,
                    Frequency = CharFreqs[i] + 1,
                    NumBits   = 0,
                    Code      = 0,
                    Left      = null,
                    Right     = null
                };

                Leaves.Add((byte)i, nodes[i]);
            }

            while (currCount != 1)
            {
                uint min1 = 0xFFFFFFFEU, min2 = 0xFFFFFFFFU;
                int  index1 = -1, index2 = -1;

                for (var i = 0; i < currCount; ++i)
                {
                    if (nodes[i].Frequency < min1)
                    {
                        min2   = min1;
                        index2 = index1;

                        min1   = nodes[i].Frequency;
                        index1 = i;
                    }
                    else if (nodes[i].Frequency < min2)
                    {
                        min2   = nodes[i].Frequency;
                        index2 = i;
                    }
                }

                if (index1 != -1 && index2 != -1 && index1 != index2)
                {
                    var node1 = nodes[index1];
                    var node2 = nodes[index2];

                    var parent = new HuffmanTreeNode
                    {
                        Symbol    = (byte)'*',
                        Frequency = node1.Frequency + node2.Frequency,
                        Left      = node1,
                        Right     = node2
                    };

                    nodes[index1 > index2 ? index2 : index1] = parent;

                    if (index2 != (currCount - 1))
                    {
                        nodes[index1 > index2 ? index1 : index2] = nodes[currCount - 1];
                    }

                    --currCount;
                }

                Root = nodes[0];
            }

            GenerateCodes(Root, 0, 0);
        }
Esempio n. 3
0
 public static bool IsLeaf(HuffmanTreeNode node)
 {
     return(node.IsLeaf());
 }