0

My task:

  1. Get string from user
  2. Find out how many times each character in the string repeats
  3. find the two characters that repeat the least adding their frequency and add them together with the sum of their frequencies
  4. Add this new sum back into the list, wherever it will now go, higher up
  5. 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!!!

ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
Marvin
  • 65
  • 1
  • 8

1 Answers1

1

At the moment you're enumerating the key collection, but you can actually enumerate the dictionary itself, meaning you get a key/value object:

foreach(KeyValuePair<char, int> item in timesRepeated)
{
    char key = item.Key;
    int count = item.Value;
    Console.WriteLine(key.ToString() + " : " + count.ToString());
}

How does this help us? Well, we can combine it with the .OrderByDescending LINQ method:

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());
}

You might also want to then sort in alphabetical order, for which you can then use ThenBy:

foreach(KeyValuePair<char, int> item in timesRepeated.OrderByDescending(i => i.Value).ThenBy(i => i.Key))
{
    char key = item.Key;
    int count = item.Value;
    Console.WriteLine(key.ToString() + " : " + count.ToString());
}

Note: This will only do alphabetical order for roman letters (A, B, etc.).

Note 2: You might need to add using System.Linq; at the top of your file if it doesn't already exist.

Try it online

Results:

O : 3
B : 1
M : 1
ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86