Skip to content
Prev Previous commit
Next Next commit
Make HuffmanCoding non-static. Fix Add method. Change input to "bibbi…
…ty bobbity". Remove unnecessary braces.
  • Loading branch information
june128 committed May 29, 2018
commit ff95d099b60b28b85e9a49338a84e896b3e39ca1
24 changes: 11 additions & 13 deletions chapters/data_compression/huffman/code/cs/HuffmanCoding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public EncodingResult(string bitString, Dictionary<char, string> dictionary, Huf
}
}

public static class HuffmanCoding
public class HuffmanCoding
{
// The Node class used for the Huffman Tree.
public class Node : IComparable<Node>
Expand Down Expand Up @@ -63,13 +63,11 @@ public NodePriorityList(List<Node> givenNodes)

public void Add(Node newNode)
{
var index = ~this.nodes.BinarySearch(newNode);
if (index == this.nodes.Count)
{
this.nodes.Add(newNode);
return;
}
this.nodes.Insert(~index, newNode);
var index = this.nodes.BinarySearch(newNode);
if (index < 0)
this.nodes.Insert(~index, newNode);
else
this.nodes.Insert(index, newNode);
}

public Node Pop()
Expand All @@ -80,7 +78,7 @@ public Node Pop()
}
}

public static EncodingResult Encode(string input)
public EncodingResult Encode(string input)
{
var root = CreateTree(input);
var dictionary = CreateDictionary(root);
Expand All @@ -89,7 +87,7 @@ public static EncodingResult Encode(string input)
return new EncodingResult(bitString, dictionary, root);
}

public static string Decode(EncodingResult result)
public string Decode(EncodingResult result)
{
var output = "";
Node currentNode = result.Tree;
Expand All @@ -110,7 +108,7 @@ public static string Decode(EncodingResult result)
return output;
}

private static Node CreateTree(string input)
private Node CreateTree(string input)
{
// Create a List of all characters and their count in input by putting them into nodes.
var nodes = input
Expand All @@ -135,7 +133,7 @@ private static Node CreateTree(string input)
return nodePriorityList.Pop();
}

private static Dictionary<char, string> CreateDictionary(Node root)
private Dictionary<char, string> CreateDictionary(Node root)
{
// We're using a string instead of a actual bits here, since it makes the code somewhat more readable and this is an educational example.
var dictionary = new Dictionary<char, string>();
Expand All @@ -157,7 +155,7 @@ void CreateDictionary(Node node, string bitString, Dictionary<char, string> loca
}


private static string CreateBitString(string input, Dictionary<char, string> dictionary)
private string CreateBitString(string input, Dictionary<char, string> dictionary)
{
// We're using a string right here. While no compression is achieved with a string, it's the easiest way to display what the compressed result looks like. Also this is just an educational example.
var bitString = "";
Expand Down
8 changes: 4 additions & 4 deletions chapters/data_compression/huffman/code/cs/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ class Program
{
static void Main(string[] args)
{
var result = HuffmanCoding.Encode("aaaabbbccd");
var huffmanCoding = new HuffmanCoding();

var result = huffmanCoding.Encode("bibbity bobbity");
// The bitStrings are just strings and provide no compression. Look in HuffmanCoding.cs for explanation.
// Print dictionary.
foreach (var entry in result.Dictionary)
{
System.Console.WriteLine($"{entry.Key} {entry.Value}");
}
// Print BitString.
System.Console.WriteLine($"{result.BitString} count: {result.BitString.Length}");

var originalString = HuffmanCoding.Decode(result);
var originalString = huffmanCoding.Decode(result);
System.Console.WriteLine(originalString);
}
}
Expand Down