Reputation: 75
using System;
using System.Collections.Generic;
using System.Linq;
using System.ComponentModel;
using System.Diagnostics;
using System.Security.Cryptography.X509Certificates;
//Step 1: Get string from user
//Step 2: Find out how many times each character in the string repeats
//Step 3: find the two characters that repeat the least adding their frequency and add them together with the sum of their frequencies
//step 4: Add this new sum back into the list, wherever it will now go, higher up
//step 5: repeat theses steps, until you have a huffman tree!
namespace HuffmanTree
{
class Program
{
static void Main(string[] args)
{
HuffmanTree.GetValue();
}
}
class HuffmanTree
{
public static void GetValue()
{
Console.WriteLine("Write a string to be encoded"); //Here we are asking the user to input a string
string encodeme = Console.ReadLine(); // here the user inputs their string, which gets declared as the variable "encodeme"
Dictionary<char, int> timesRepeated = new Dictionary<char, int>();
List<Node> HuffmanNodes = new List<Node>();
foreach (char ch in encodeme.Replace(" ", string.Empty))
{
if (timesRepeated.ContainsKey(ch))
{
timesRepeated[ch] = timesRepeated[ch] + 1;
}
else
{
timesRepeated.Add(ch, 1);
}
}
foreach (KeyValuePair<char, int> item in timesRepeated.OrderByDescending(i => i.Value))
{
char key = item.Key;
int count = item.Value;
Console.WriteLine(key.ToString() + " : " + count.ToString());
HuffmanNodes.Add(new Node());
}
Console.WriteLine(HuffmanNodes(node));
}
public class Node //These are going to be the intialisation of our nodes
{
public char Symbol { get; set; }
public int Frequency { get; set; }
public Node Right { get; set; }
public Node Left { get; set; }
}
}
}
I am trying to Console.Write
my Huffman tree node list so I can see what is going on. Printing Console.WriteLine(HuffmanNodes(node));
doesn't seem to work. I have tried printing several different ways such as with [i]
and it just does not seem to work. This is for debugging, so I can see whats going on with each individual step.
Upvotes: 1
Views: 724
Reputation: 16612
You are trying to invoke HuffmanNodes
like a method when, because it is a List<>
, you'll need to enumerate it...
foreach (Node node in HuffmanNodes)
{
Console.WriteLine($"Symbol: {node.Symbol}, Frequency: {node.Frequency}");
}
...or iterate it...
for (int i = 0; i < HuffmanNodes.Count; i++)
{
Console.WriteLine($"Symbol: {HuffmanNodes[i].Symbol}, Frequency: {HuffmanNodes[i].Frequency}");
}
...to access each Node
it contains.
You could also just incorporate the code to display the list in the preceding foreach
loop, since that will give you access to the same Node
s in the same order as they're added to the List<>
...
foreach (KeyValuePair<char, int> item in timesRepeated.OrderByDescending(i => i.Value))
{
// [snip]
Node node = new Node();
HuffmanNodes.Add(node);
Console.WriteLine($"Symbol: {node.Symbol}, Frequency: {node.Frequency}");
}
Additional notes:
Node
you add to HuffmanNodes
.override
the ToString()
method of the Node
class you could build a diagnostic string
much the same way as my Console.WriteLine()
calls above. This would allow you to print the properties of a Node
with simply Console.WriteLine(node);
.GetValue()
that doesn't return anything.Upvotes: 2