Skip to content

Huffman encoding Kotlin implementation #640

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

neverix
Copy link
Member

@neverix neverix commented Oct 10, 2019

No description provided.

@neverix neverix changed the title Implemented Huffman encoding in Kotlin Huffman encoding Kotlin implementation Oct 10, 2019
@berquist berquist self-assigned this Oct 11, 2019
@berquist berquist added Implementation This provides an implementation for an algorithm. (Code and maybe md files are edited.) Hacktoberfest The label for all Hacktoberfest related things! labels Oct 11, 2019
Copy link
Member

@berquist berquist left a 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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
class HuffmanTree (val textSample: String) {
class HuffmanTree (textSample: String) {

@@ -0,0 +1,130 @@
import java.util.*

// node type
Copy link
Member

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
Copy link
Member

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))
Copy link
Member

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:

Suggested change
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>) {
Copy link
Member

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 {
Copy link
Member

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
Copy link
Member

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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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)
Copy link
Member

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 {
Copy link
Member

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.

@berquist berquist mentioned this pull request May 24, 2020
@ntindle
Copy link
Member

ntindle commented Aug 28, 2021

[lang: kotlin]

@github-actions github-actions bot added the lang: kotlin Kotlin programming language label Aug 28, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Hacktoberfest The label for all Hacktoberfest related things! Implementation This provides an implementation for an algorithm. (Code and maybe md files are edited.) lang: kotlin Kotlin programming language
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants