Hoi,
Ik zit met een error met mijn Huffman alghoritme
bij me program.cs zegt dat die 2 argumenten verwacht maar die geef ik toch ook een char voor de key en een int voor de value, iemand enig idee?
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
Ik zit met een error met mijn Huffman alghoritme
bij me program.cs zegt dat die 2 argumenten verwacht maar die geef ik toch ook een char voor de key en een int voor de value, iemand enig idee?
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
Code:
//////////////////////////////////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
// 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);
}
}
}
}