Skip to content

Instantly share code, notes, and snippets.

@1gravity
Last active September 14, 2022 15:55
Show Gist options
  • Save 1gravity/d1198ab3ab06d75868af3db6baa32f92 to your computer and use it in GitHub Desktop.
Save 1gravity/d1198ab3ab06d75868af3db6baa32f92 to your computer and use it in GitHub Desktop.
Kotlin Trie Delete Optimized
override fun delete(key: String) {
delete(key, 0, root)
}
private fun delete(key: String, index: Int, node: Node<Value>) {
if (index == key.length)
node.value = null
else node.children[key[index]]?.run {
delete(key, index + 1, this)
if (children.isEmpty() && value == null) node.children.remove(key[index])
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment