Ejemplo n.º 1
0
 public HuffmanTreeNode(HuffmanTreeNode left, HuffmanTreeNode right)
 {
     Left        = left;
     Right       = right;
     Left.Parent = Right.Parent = this;
     if (ByteCount == null)
     {
         ByteCount = new ByteCount();
     }
     ByteCount.Count = Left.ByteCount.Count + Right.ByteCount.Count;
 }
Ejemplo n.º 2
0
        public HuffmanTree(ByteCount[] bytesFreqs)
        {
            var nodes = new List <HuffmanTreeNode>();

            for (int i = 0; i < bytesFreqs.Length; i++)
            {
                nodes.Add(new HuffmanTreeNode
                {
                    ByteCount = new ByteCount
                    {
                        Byte  = bytesFreqs[i].Byte,
                        Count = bytesFreqs[i].Count
                    }
                });
            }

            HuffmanTreeNode left, right;

            while (nodes.Count > 1)
            {
                int min    = nodes[0].ByteCount.Count;
                int minInd = 0;
                for (int j = 1; j < nodes.Count; j++)
                {
                    if (min > nodes[j].ByteCount.Count)
                    {
                        min    = nodes[j].ByteCount.Count;
                        minInd = j;
                    }
                }
                left = nodes[minInd];
                nodes.RemoveAt(minInd);

                min    = nodes[0].ByteCount.Count;
                minInd = 0;
                for (int j = 1; j < nodes.Count; j++)
                {
                    if (min > nodes[j].ByteCount.Count)
                    {
                        min    = nodes[j].ByteCount.Count;
                        minInd = j;
                    }
                }
                right = nodes[minInd];
                nodes.RemoveAt(minInd);

                nodes.Add(new HuffmanTreeNode(left, right));
            }

            Root = nodes[0];
            CalculateBytes();
        }
Ejemplo n.º 3
0
     private void TraverseTree(HuffmanTreeNode node, int length, int value)
     {
         if (node.Left != null)
         {
             length++;
             TraverseTree(node.Left, length, value);
             value |= (1 << (length - 1));
             TraverseTree(node.Right, length, value);
         }
         else
         {
             CompressedBytes[node.ByteCount.Byte] = new CompressedByte {
                 Length = length, Value = value
             }
         };
     }
 }
Ejemplo n.º 4
0
        public static byte GetValue(HuffmanTreeNode root, byte[] bytes, ref int bitPos)
        {
            int curBytePos      = bitPos / 8;
            int curBitInBytePos = bitPos % 8;
            int bit;
            var curNode = root;

            while (curNode.Left != null)
            {
                bit = bytes[curBytePos] & (128 >> curBitInBytePos);
                if (bit == 0)
                {
                    curNode = curNode.Left;
                }
                else
                {
                    curNode = curNode.Right;
                }
                curBytePos     += (curBitInBytePos + 1) / 8;
                curBitInBytePos = (curBitInBytePos + 1) % 8;
            }
            bitPos = curBytePos * 8 + curBitInBytePos;
            return(curNode.ByteCount.Byte);
        }
Ejemplo n.º 5
0
        public HuffmanTree(ByteCount[] bytesFreqs)
        {
            var nodes = new List<HuffmanTreeNode>();
            for (int i = 0; i < bytesFreqs.Length; i++)
                nodes.Add(new HuffmanTreeNode
                {
                    ByteCount = new ByteCount
                    {
                        Byte = bytesFreqs[i].Byte,
                        Count = bytesFreqs[i].Count
                    }
                });

            HuffmanTreeNode left, right;
            while (nodes.Count > 1)
            {
                int min = nodes[0].ByteCount.Count;
                int minInd = 0;
                for (int j = 1; j < nodes.Count; j++)
                    if (min > nodes[j].ByteCount.Count)
                    {
                        min = nodes[j].ByteCount.Count;
                        minInd = j;
                    }
                left = nodes[minInd];
                nodes.RemoveAt(minInd);

                min = nodes[0].ByteCount.Count;
                minInd = 0;
                for (int j = 1; j < nodes.Count; j++)
                    if (min > nodes[j].ByteCount.Count)
                    {
                        min = nodes[j].ByteCount.Count;
                        minInd = j;
                    }
                right = nodes[minInd];
                nodes.RemoveAt(minInd);

                nodes.Add(new HuffmanTreeNode(left, right));
            }

            Root = nodes[0];
            CalculateBytes();
        }
Ejemplo n.º 6
0
 public HuffmanTreeNode(HuffmanTreeNode left, HuffmanTreeNode right)
 {
     Left = left;
     Right = right;
     Left.Parent = Right.Parent = this;
     if (ByteCount == null)
         ByteCount = new ByteCount();
     ByteCount.Count = Left.ByteCount.Count + Right.ByteCount.Count;
 }
Ejemplo n.º 7
0
 private void TraverseTree(HuffmanTreeNode node, int length, int value)
 {
     if (node.Left != null)
     {
         length++;
         TraverseTree(node.Left, length, value);
         value |= (1 << (length - 1));
         TraverseTree(node.Right, length, value);
     }
     else
         CompressedBytes[node.ByteCount.Byte] = new CompressedByte { Length = length, Value = value };
 }
Ejemplo n.º 8
0
 public static byte GetValue(HuffmanTreeNode root, byte[] bytes, ref int bitPos)
 {
     int curBytePos = bitPos / 8;
     int curBitInBytePos = bitPos % 8;
     int bit;
     var curNode = root;
     while (curNode.Left != null)
     {
         bit = bytes[curBytePos] & (128 >> curBitInBytePos);
         if (bit == 0)
             curNode = curNode.Left;
         else
             curNode = curNode.Right;
         curBytePos += (curBitInBytePos + 1) / 8;
         curBitInBytePos = (curBitInBytePos + 1) % 8;
     }
     bitPos = curBytePos * 8 + curBitInBytePos;
     return curNode.ByteCount.Byte;
 }