Last active
September 14, 2022 15:55
-
-
Save 1gravity/d1198ab3ab06d75868af3db6baa32f92 to your computer and use it in GitHub Desktop.
Kotlin Trie Delete Optimized
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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