My task:
- Get string from user
- Find out how many times each character in the string repeats
- find the two characters that repeat the least adding their frequency and add them together with the sum of their frequencies
- Add this new sum back into the list, wherever it will now go, higher up
- repeat theses steps, until you have a huffman tree!
My code:
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>();
foreach (char ch in encodeme.Replace(" ", string.Empty))
{
if (timesRepeated.ContainsKey(ch))
{
timesRepeated[ch] = timesRepeated[ch] + 1;
}
else
{
timesRepeated.Add(ch, 1);
}
}
foreach (var item in timesRepeated.Keys)
{
Console.WriteLine(item + " : " + timesRepeated[item]);
}
Console.ReadKey();
}
}
class Node
{
}
So I am trying to sort the dictionary values "times repeated" into descending order so that when it prints out the amount of times that characters are repeated, it show these values in descending order.
For example if I inputted the string "BOOOM"
O = 3
B = 1
M = 1
At the moment it says:
B = 1
O = 3
M = 1
I am not sure how to do this!!!