-
-
Notifications
You must be signed in to change notification settings - Fork 360
Huffman encoding Kotlin implementation #640
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not bad, definitely better looking than the Java version, but there are a few things you can do to make it more idiomatic Kotlin.
class Leaf(freq: Int, val letter: Char): Node(freq) | ||
class Branch(freq: Int, val left: Node, val right: Node): Node(freq) | ||
|
||
class HuffmanTree (val textSample: String) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
class HuffmanTree (val textSample: String) { | |
class HuffmanTree (textSample: String) { |
@@ -0,0 +1,130 @@ | |||
import java.util.* | |||
|
|||
// node type |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comment doesn't really add anything.
val sourceText = "bibbity_bobbity" | ||
// create huffman tree | ||
val huffmanTree = HuffmanTree(sourceText) | ||
// encode the text |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This and the above comment aren't very useful.
// encode the text | ||
val encoded = huffmanTree.encode(sourceText) | ||
println("Encoded String: " + encoded) | ||
println("Decoded String: " + huffmanTree.decode(encoded)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For here and above, prefer template syntax rather than concatenation:
println("Decoded String: " + huffmanTree.decode(encoded)) | |
println("Decoded String: ${huffmanTree.decode(encoded)}") |
} | ||
|
||
// recursively populate a codebook with encodings from a node | ||
fun populateCodebook(node: Node, code: String, codebook: MutableMap<Char, String>) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be private.
fun encode(letter: Char) = codebook[letter] | ||
|
||
// the reverse codebook is just the original one with keys as values and values as keys | ||
val reverseCodebook: HashMap<String, Char> by lazy { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be private.
for (m in codebook.entries) { | ||
reversed.put(m.value, m.key) | ||
} | ||
reversed |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can replace the whole block with
codebook.entries.associateBy({ it.value }) { it.key }
} | ||
|
||
// encode a string using the tree | ||
fun encode(source: String): String { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fun encode(source: String): String { | |
fun encode(source: String) = source.map { codeBook.encode(it) }.joinToString(separator = "") |
replaces the whole function.
val frequencyMap = HashMap<Char, Int>() | ||
for (char in textSample) { | ||
val newFrequency = (frequencyMap.get(char) ?: 0) + 1 | ||
frequencyMap.put(char, newFrequency) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Replace both lines with
frequencyMap[char] = frequencyMap.getOrDefault(char, 0)
} | ||
|
||
// decode an encoded string | ||
fun decode(encoded: String) = buildString { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I won't work it out for you, but some of this can be re-written using functional idioms.
[lang: kotlin] |
No description provided.