huffman encoding algorithm in csharp

Here's a simple implementation of the Huffman encoding algorithm in C#:

main.cs
using System;
using System.Collections.Generic;

namespace HuffmanEncoding
{
    class Node
    {
        public char Value { get; set; }
        public int Frequency { get; set; }
        public Node Left { get; set; }
        public Node Right { get; set; }
    }

    class PriorityQueue
    {
        private List<Node> nodes = new List<Node>();

        public void Enqueue(Node node)
        {
            nodes.Add(node);
            nodes.Sort((x, y) => x.Frequency - y.Frequency);
        }

        public Node Dequeue()
        {
            Node node = nodes[0];
            nodes.RemoveAt(0);
            return node;
        }

        public int Count
        {
            get { return nodes.Count; }
        }
    }

    class HuffmanEncoding
    {
        public static Dictionary<char, string> Encode(string input)
        {
            Dictionary<char, int> frequencies = new Dictionary<char, int>();
            foreach (char c in input)
            {
                if (frequencies.ContainsKey(c))
                    frequencies[c]++;
                else
                    frequencies[c] = 1;
            }

            PriorityQueue queue = new PriorityQueue();
            foreach (KeyValuePair<char, int> pair in frequencies)
            {
                queue.Enqueue(new Node() { Value = pair.Key, Frequency = pair.Value });
            }

            while (queue.Count > 1)
            {
                Node left = queue.Dequeue();
                Node right = queue.Dequeue();
                Node parent = new Node() { Value = '\0', Frequency = left.Frequency + right.Frequency, Left = left, Right = right };
                queue.Enqueue(parent);
            }

            Dictionary<char, string> codes = new Dictionary<char, string>();
            Traverse(queue.Dequeue(), "", codes);
            return codes;
        }

        private static void Traverse(Node node, string code, Dictionary<char, string> codes)
        {
            if (node == null)
                return;

            if (node.Left == null && node.Right == null)
                codes.Add(node.Value, code);
            else
            {
                Traverse(node.Left, code + "0", codes);
                Traverse(node.Right, code + "1", codes);
            }
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            string input = "hello world";
            Dictionary<char, string> codes = HuffmanEncoding.Encode(input);

            Console.WriteLine("Character\tCode");
            foreach (KeyValuePair<char, string> pair in codes)
            {
                Console.WriteLine("{0}\t\t{1}", pair.Key, pair.Value);
            }

            Console.ReadLine();
        }
    }
}
2756 chars
101 lines

To run this program, simply create a new C# console application project and copy the code into the Program.cs file. The Encode method takes a string input and returns a dictionary of character codes. In the example above, we encode the string "hello world" and print out the resulting codes for each character.

gistlibby LogSnag