示例#1
0
        static void Main(string[] args)
        {
            List <Tuple <char, int> > input = new List <Tuple <char, int> >();

            input.Add(new Tuple <char, int>('a', 10));
            input.Add(new Tuple <char, int>('c', 40));
            input.Add(new Tuple <char, int>('d', 5));
            input.Add(new Tuple <char, int>('e', 100));
            input.Add(new Tuple <char, int>('g', 30));

            Solution sln = new Solution();

            HuffmanNode n = sln.CreateHuffmanTree(input);

            Console.WriteLine("Hello World!");
        }
示例#2
0
        //Creates the Huffman Binary Tree from a sorted (by weight)
        //list of nodes
        private HuffmanNode createTree(List <HuffmanNode> list)
        {
            HuffmanNode node1 = null, node2 = null;

            while (list.Count > 1)
            {
                node1 = list[0];
                node2 = list[1];
                list.RemoveRange(0, 2);
                list.Add(
                    new HuffmanNode(node2.Symbol + node1.Symbol, node1.Weight + node2.Weight)
                {
                    Left  = node2,
                    Right = node1
                });
                list.Sort(new SortHufNodeByWeight());
            }

            return(list.First());
        }