Skip to content

Instantly share code, notes, and snippets.

@notshekhar
Created May 28, 2021 13:26
Show Gist options
  • Select an option

  • Save notshekhar/7405ecca4d6b7798fc5a6cd50b3e563c to your computer and use it in GitHub Desktop.

Select an option

Save notshekhar/7405ecca4d6b7798fc5a6cd50b3e563c to your computer and use it in GitHub Desktop.
function Node() {
this.values = null
this.end = false
this.add = (v, t) => {
if (!this.values) this.values = {}
if (!this.values[v]) this.values[v] = new Node()
}
}
function Trees() {
this.root = new Node()
this.add = (s) => {
let temp = this.root
for (let i = 0; i < s.length; i++) {
temp.add(s[i])
if (i == s.length - 1) temp.values[s[i]].end = true
temp = temp.values[s[i]]
}
}
this.search = (s) => {
let temp = this.root
for (let i = 0; i < s.length; i++) {
if (!temp.values) return false
if ((i == s.length - 1) & !temp.values[s[i]].end) return false
if (!temp.values[s[i]]) return false
temp = temp.values[s[i]]
}
return true
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment