示例#1
0
 public void CalcFreq()
 {
     if (zero != null && one != null)
     {
         zero.CalcFreq();
         one.CalcFreq();
         frequency = zero.frequency + one.frequency;
     }
 }
示例#2
0
 HuffNode BuildHuffTree()
 {
     Dictionary<byte, int> frequency = new Dictionary<byte, int>();
     foreach (byte i in src)
     {
         if (!frequency.ContainsKey(i))
             frequency.Add(i, 0);
         frequency[i]++;
     }
     var tmp = from t in frequency
               where true
               orderby t.Value
               select t.Key;
     List<byte> ordered = tmp.ToList();
     List<HuffNode> nodes = new List<HuffNode>();
     HuffNode one;
     HuffNode zero;
     HuffNode root;
     int index = 0;
     while (index < ordered.Count)
     {
         var tmp2 = from t in nodes
                    where t.top && (t.frequency < frequency[ordered[index]] || t.frequency < frequency[ordered[index + 1]])
                    orderby t.frequency
                    select t;
         List<HuffNode> finished = tmp2.ToList();
         int rest = 2 - finished.Count;
         for (int i = 0; i < rest; i++)
         {
             HuffNode node = new HuffNode();
             node.content = ordered[index++];
             node.frequency = frequency[node.content];
             node.top = true;
             nodes.Add(node);
             finished.Add(node);
         }
         zero = finished[0];
         one = finished[1];
         HuffNode top = new HuffNode();
         top.zero = zero;
         top.one = one;
         top.top = true;
         top.CalcFreq();
         zero.top = false;
         one.top = false;
         nodes.Add(top);
     }
     {
         root = nodes[nodes.Count - 1];
         if (root.frequency == src.Length)
         {
             List<HuffNode> opend = new List<HuffNode>();
             opend.Add(root);
             while (opend.Count > 0)
             {
                 HuffNode node = opend[0];
                 opend.Remove(node);
                 if (node.zero != null)
                 {
                     node.zero.bits = (byte)(node.bits + 1);
                     node.zero.code = (int)(node.code | (0 << node.zero.bits));
                     opend.Add(node.zero);
                 }
                 if (node.one != null)
                 {
                     node.one.bits = (byte)(node.bits + 1);
                     node.one.code = (int)(node.code | ((1 << (node.one.bits - 1))));
                     opend.Add(node.one);
                 }
             }
             return root;
         }
     }
     return null;
 }
示例#3
0
        HuffNode BuildHuffTree()
        {
            Dictionary <byte, int> frequency = new Dictionary <byte, int>();

            foreach (byte i in src)
            {
                if (!frequency.ContainsKey(i))
                {
                    frequency.Add(i, 0);
                }
                frequency[i]++;
            }
            var tmp = from t in frequency
                      where true
                      orderby t.Value
                      select t.Key;
            List <byte>     ordered = tmp.ToList();
            List <HuffNode> nodes   = new List <HuffNode>();
            HuffNode        one;
            HuffNode        zero;
            HuffNode        root;
            int             index = 0;

            while (index < ordered.Count)
            {
                var tmp2 = from t in nodes
                           where t.top && (t.frequency < frequency[ordered[index]] || t.frequency < frequency[ordered[index + 1]])
                           orderby t.frequency
                           select t;
                List <HuffNode> finished = tmp2.ToList();
                int             rest     = 2 - finished.Count;
                for (int i = 0; i < rest; i++)
                {
                    HuffNode node = new HuffNode();
                    node.content   = ordered[index++];
                    node.frequency = frequency[node.content];
                    node.top       = true;
                    nodes.Add(node);
                    finished.Add(node);
                }
                zero = finished[0];
                one  = finished[1];
                HuffNode top = new HuffNode();
                top.zero = zero;
                top.one  = one;
                top.top  = true;
                top.CalcFreq();
                zero.top = false;
                one.top  = false;
                nodes.Add(top);
            }
            {
                root = nodes[nodes.Count - 1];
                if (root.frequency == src.Length)
                {
                    List <HuffNode> opend = new List <HuffNode>();
                    opend.Add(root);
                    while (opend.Count > 0)
                    {
                        HuffNode node = opend[0];
                        opend.Remove(node);
                        if (node.zero != null)
                        {
                            node.zero.bits = (byte)(node.bits + 1);
                            node.zero.code = (int)(node.code | (0 << node.zero.bits));
                            opend.Add(node.zero);
                        }
                        if (node.one != null)
                        {
                            node.one.bits = (byte)(node.bits + 1);
                            node.one.code = (int)(node.code | ((1 << (node.one.bits - 1))));
                            opend.Add(node.one);
                        }
                    }
                    return(root);
                }
            }
            return(null);
        }