-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathproblem_261.js
19 lines (14 loc) Β· 946 Bytes
/
problem_261.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// This problem was asked by Amazon.
// Huffman coding is a method of encoding characters based on their frequency. Each letter is assigned a variable-length binary string, such as `0101` or `111110`, where shorter lengths correspond to more common letters. To accomplish this, a binary tree is built such that the path from the root to any leaf uniquely maps to a character. When traversing the path, descending to a left child corresponds to a `0` in the prefix, while descending right corresponds to `1`.
// Here is an example tree (note that only the leaf nodes have letters):
// ```
// *
// / \
// * *
// / \ / \
// * a t *
// / \
// c s
// ```
// With this encoding, cats would be represented as `0000110111`.
// Given a dictionary of character frequencies, build a Huffman tree, and use it to determine a mapping between characters and their encoded binary strings.