コード例 #1
0
 // 先序读取构造一颗huffman树
 private static HuffmanNode ReadTrie(BitVirtualSteam bvStream, ref int index)
 {
     if (bvStream.ReadBoolean())
     {
         //byte b = BitsToChar(bits, ref index);
         return(new HuffmanNode(bvStream.ReadChar(), 0, null, null));
     }
     return(new HuffmanNode('\0', 0, ReadTrie(bvStream, ref index), ReadTrie(bvStream, ref index)));
 }
コード例 #2
0
        public static string Expand(byte[] bytes)
        {
            int             index    = 0;
            BitArray        bits     = new BitArray(bytes);
            BitVirtualSteam bvStream = new BitVirtualSteam(bytes);
            HuffmanNode     root     = ReadTrie(bvStream, ref index);
            BitArray        lenbs    = new BitArray(32);

            for (int i = 0; i < 32; i++)
            {
                lenbs[i] = bits[index++];
            }
            byte[] lenBytes = new byte[4];
            lenbs.CopyTo(lenBytes, 0);
            //int N = BitConverter.ToInt32(lenBytes);
            int N = bvStream.ReadInt();

            char[] chars = new char[N];
            for (int i = 0; i < N; i++)
            {
                HuffmanNode x = root;
                while (!x.isLeaf())
                {
                    //一直往前读取
                    if (bvStream.ReadBoolean())
                    {
                        x = x.Right;
                    }
                    else
                    {
                        x = x.Left;
                    }
                }
                chars[i] = x.Ch;
            }
            return(string.Join("", chars));
        }