Пример #1
0
 public HuffmanCodeNode(int priority, char?value = null, HuffmanCodeNode left = null, HuffmanCodeNode right = null)
 {
     Priority   = priority;
     Value      = value;
     LeftChild  = left;
     RightChild = right;
 }
Пример #2
0
        private static HuffmanCodeNode CreateTree(List <HuffmanCodeNode> orderedList)
        {
            if (orderedList.Count == 1)
            {
                return(orderedList[0]);
            }
            var newList = orderedList.Skip(2).ToList();
            var first   = orderedList[0];
            var second  = orderedList[1];
            var newNode = new HuffmanCodeNode(first.Priority + second.Priority, null, first, second);

            newList.Add(newNode);
            return(CreateTree(newList.OrderBy(item => item.Priority).ToList()));
        }
Пример #3
0
 private static void FormResult(HuffmanCodeNode node, Dictionary <char, string> result, string str)
 {
     if (node == null)
     {
         return;
     }
     if (node.Value != null)
     {
         result.Add((char)node.Value, str);
     }
     if (node.LeftChild != null)
     {
         FormResult(node.LeftChild, result, str + "0");
     }
     if (node.RightChild != null)
     {
         FormResult(node.RightChild, result, str + "1");
     }
 }