Created
September 14, 2022 16:34
-
-
Save 1gravity/75f836077eb23f4d892585bbc372a5d1 to your computer and use it in GitHub Desktop.
Kotlin Trie Non-Recursive Delete
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) { | |
// search leaf | |
key.fold(root) { node, char -> | |
node.children[char] ?: return | |
}.let { leaf -> | |
leaf.value = null | |
key.foldRight(leaf) { char, current -> | |
if (current.children.isEmpty() && current.value == null) { | |
current.parent?.apply { children.remove(char) } ?: return | |
} else return | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment