Skip to content

Instantly share code, notes, and snippets.

@lykkin
Created February 7, 2017 06:59
Show Gist options
  • Save lykkin/99e55500ea870e581388107efcde6ba1 to your computer and use it in GitHub Desktop.
Save lykkin/99e55500ea870e581388107efcde6ba1 to your computer and use it in GitHub Desktop.
trie me
var hash = {
trie: {},
store(k, v) {
let current = this.trie
k.split('').forEach((letter) => {
if (!this.trie[letter]) {
this.trie[letter] = {}
}
current = this.trie[letter]
})
current.val = v
},
get(k) {
var current = this.trie
k.split('').forEach((letter) => {
if (!this.trie[letter]) {
return
}
current = this.trie[letter]
})
return current.val
}
}
hash.store('asdf', 1234)
console.log(hash.get('asdf'))
console.log(hash.get('as'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment