Example #1
0
        public T Decode(List <int> bitString, ref int position)
        {
            HuffmanNode <T> nodeCur = _root;

            while (!nodeCur.IsLeaf)
            {
                if (position > bitString.Count)
                {
                    throw new ArgumentException("Invalid bitstring in Decode");
                }
                nodeCur = bitString[position++] == 0 ? nodeCur.LeftSon : nodeCur.RightSon;
            }
            return(nodeCur.Value);
        }
Example #2
0
        public void Encode(T value, List <int> encoding)
        {
            if (!_leafDictionary.ContainsKey(value))
            {
                throw new ArgumentException("Invalid value in Encode");
            }
            HuffmanNode <T> nodeCur         = _leafDictionary[value];
            var             reverseEncoding = new List <int>();

            while (!nodeCur.IsRoot)
            {
                reverseEncoding.Add(nodeCur.Bit);
                nodeCur = nodeCur.Parent;
            }

            reverseEncoding.Reverse();
            encoding.AddRange(reverseEncoding);
        }