How to resolve the algorithm Huffman coding step by step in the Fantom programming language
How to resolve the algorithm Huffman coding step by step in the Fantom programming language
Table of Contents
Problem Statement
Huffman encoding is a way to assign binary codes to symbols that reduces the overall number of bits used to encode a typical string of those symbols. For example, if you use letters as symbols and have details of the frequency of occurrence of those letters in typical strings, then you could just encode each letter with a fixed number of bits, such as in ASCII codes. You can do better than this by encoding more frequently occurring letters such as e and a, with smaller bit strings; and less frequently occurring letters such as q and x with longer bit strings. Any string of letters will be encoded as a string of bits that are no-longer of the same length per letter. To successfully decode such as string, the smaller codes assigned to letters such as 'e' cannot occur as a prefix in the larger codes such as that for 'x'. The Huffman coding scheme takes each symbol and its weight (or frequency of occurrence), and generates proper encodings for each symbol taking account of the weights of each symbol, so that higher weighted symbols have fewer bits in their encoding. (See the WP article for more information). A Huffman encoding can be computed by first creating a tree of nodes:
Traverse the constructed binary tree from root to leaves assigning and accumulating a '0' for one branch and a '1' for the other at each node. The accumulated zeros and ones at each leaf constitute a Huffman encoding for those symbols and weights:
Using the characters and their frequency from the string: create a program to generate a Huffman encoding for each character as a table.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Huffman coding step by step in the Fantom programming language
Source code in the fantom programming language
class Node
{
Float probability := 0.0f
}
class Leaf : Node
{
Int character
new make (Int character, Float probability)
{
this.character = character
this.probability = probability
}
}
class Branch : Node
{
Node left
Node right
new make (Node left, Node right)
{
this.left = left
this.right = right
probability = this.left.probability + this.right.probability
}
}
class Huffman
{
Node[] queue := [,]
Str:Str table := [:]
new make (Int[] items)
{
uniqueItems := items.dup.unique
uniqueItems.each |Int item|
{
num := items.findAll { it == item }.size
queue.add (Leaf(item, num.toFloat / items.size))
}
createTree
createTable
}
Void createTree ()
{
while (queue.size > 1)
{
queue.sort |a,b| {a.probability <=> b.probability}
node1 := queue.removeAt (0)
node2 := queue.removeAt (0)
queue.add (Branch (node1, node2))
}
}
Void traverse (Node node, Str encoding)
{
if (node is Leaf)
{
table[(node as Leaf).character.toChar] = encoding
}
else // (node is Branch)
{
traverse ((node as Branch).left, encoding + "0")
traverse ((node as Branch).right, encoding + "1")
}
}
Void createTable ()
{
if (queue.size != 1) return // error!
traverse (queue.first, "")
}
override Str toStr ()
{
result := "Huffman Encoding Table:\n"
table.keys.sort.each |Str key|
{
result += "$key -> ${table[key]}\n"
}
return result
}
}
class Main
{
public static Void main ()
{
example := "this is an example for huffman encoding"
huffman := Huffman (example.chars)
echo ("From \"$example\"")
echo (huffman)
}
}
You may also check:How to resolve the algorithm Arithmetic/Integer step by step in the 6502 Assembly programming language
You may also check:How to resolve the algorithm Include a file step by step in the FreeBASIC programming language
You may also check:How to resolve the algorithm Loops/Infinite step by step in the TI-89 BASIC programming language
You may also check:How to resolve the algorithm Negative base numbers step by step in the J programming language
You may also check:How to resolve the algorithm XML/Output step by step in the Action! programming language