2 Replies - 493 Views - Last Post: 22 January 2013 - 06:04 AM Rate Topic: -----

#1 DamianMRLY  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 27-December 12

Huffman

Posted 22 January 2013 - 03:34 AM

Hi,

I'm getting this error with in my application, it expects 2 argument Key/Value. But i am giving it ? char is key value int?

Error: IDictionary counts = new Dictionary<char, int>(); ->

type 'System.Collections.Generic.IDictionary<TKey,TValue>' requires 2 type arguments E:\Huffman Final\Huffmana\Huffmana\Program.cs


//////////////////////////////////program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Huffmana
{
    class Program
    {
        static void Main(string[] args)
        {
            IDictionary counts = new Dictionary<char, int>();
            //types of keys = char
            //types of values = int
            //van dictonary 


            //character op eerste plaatst
            //frequency in een int op tweede plaats
            //plaatst string in dictonary = we weten welke characters voorkomen en de frequenct

            // ac bca ba z 
            counts.Add(' ', 3);
            counts.Add('b', 2);
            counts.Add('a', 3);
            counts.Add('c', 2);
            counts.Add('z', 1);
            counts.Add('\n', 1);
            HuffmanTree tree = new HuffmanTree(counts);
            IDictionary<char, string> encodings = tree.CreateEncodings();

            foreach (KeyValuePair<char, string> kvp in encodings)
            {
                Console.WriteLine((kvp.Key == '\n' ? "EOF" : kvp.Key.ToString()) + ":\t" + kvp.Value);
            }

            Console.ReadLine();
        }
    }
}
//////////////////////////////////PriorityQueue.cs
namespace Huffmana
{
    class PriorityQueue<T>
    {
        private readonly SortedDictionary<int, Queue<T>> _SortedDictionary = new SortedDictionary<int, Queue<T>>();

        public int Count { get; set; }
        public void Enqueue (T item, int priority)
        {
            ++Count;
            if (!_SortedDictionary.ContainsKey(priority)) _SortedDictionary
                [priority] = new Queue<T>();
            _SortedDictionary[priority].Enqueue(item);
        }
        public T Dequeue()
            {
            --Count;
            var item = _SortedDictionary.First();
            if (item.Value.Count == 1) _SortedDictionary.Remove(item.Key);
            return item.Value.Dequeue();
            }
        }
    }

//////////////////////////////////HuffmanNode.cs

namespace Huffmana
{
    class HuffmanNode
    {
        //create node
        public HuffmanNode Parent   { get; set; }
        public HuffmanNode Left     { get; set; }
        public HuffmanNode Right    { get; set; }
        public char Value           { get; set; }
        public int Count            { get; set; } 
    }
}

//////////////////////////////////HuffmanNode.cs
namespace Huffmana
{
    class HuffmanTree
    {
         
        private readonly HuffmanNode _root;

        public HuffmanTree(IEnumerable<KeyValuePair<char, int>> counts)
        {
            var priorityQueue = new PriorityQueue<HuffmanNode>();

            foreach(KeyValuePair<char, int> kvp in counts)
            {
                priorityQueue.Enqueue(new HuffmanNode {Value = kvp.Key, Count = kvp.Value}, kvp.Value);
            }

            while(priorityQueue.Count > 1)
            {
                HuffmanNode n1 = priorityQueue.Dequeue();
                HuffmanNode n2 = priorityQueue.Dequeue();
                var n3 = new HuffmanNode {Left = n1, Right = n2, Count = n1.Count + n2.Count};
                n1.Parent = n3;
                n2.Parent = n3;
                priorityQueue.Enqueue(n3, n3.Count);
            }

            _root = priorityQueue.Dequeue();
        }

        public IDictionary<char, string> CreateEncodings()
        {
            var encodings = new Dictionary<char, string>();
            Encode(_root, "", encodings);
            return encodings;
        }

        private void Encode(HuffmanNode node, string path, IDictionary<char, string> encodings)
        {
            if (node.Left != null)
            {
                Encode(node.Left, path + "0", encodings);
                Encode(node.Right, path + "1", encodings);
            } else
            {
                encodings.Add(node.Value, path);    
            }
        }
    }
}





Is This A Good Question/Topic? 0
  • +

Replies To: Huffman

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2128
  • View blog
  • Posts: 3,261
  • Joined: 21-June 11

Re: Huffman

Posted 22 January 2013 - 05:14 AM

You correctly give two type arguments to Dictionary, but the error message is not complaining about Dictionary. It's complaining about IDictionary, to which you don't give any type arguments. You need to write:

IDictionary<char, int> counts = new Dictionary<char, int>();


Was This Post Helpful? 1
  • +
  • -

#3 DamianMRLY  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 27-December 12

Re: Huffman

Posted 22 January 2013 - 06:04 AM

That fixed the error, many thanxs must have overlooked that!:)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1