Skip to content

Instantly share code, notes, and snippets.

@livingsilver94
Last active August 9, 2019 20:56
Show Gist options
  • Select an option

  • Save livingsilver94/36fb2af6f5754b1326d7fbba3e7d256f to your computer and use it in GitHub Desktop.

Select an option

Save livingsilver94/36fb2af6f5754b1326d7fbba3e7d256f to your computer and use it in GitHub Desktop.
JavaScript Trie implementation (ES2015+).
/*
MIT License
Copyright (c) 2019 Fabio Forni
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
class Node {
constructor() {
this._children = new Map()
// isLeaf tells whether this node completes an element
this.isLeaf = false
}
child(item) {
return this._children.get(item)
}
children() {
return this._children.entries()
}
addChild(item) {
this._children.set(item, new Node())
}
hasChild(item) {
return this._children.has(item)
}
hasChildren() {
return this._children.size > 0
}
removeChild(item) {
return this._children.delete(item)
}
}
class Trie {
constructor() {
this.root = new Node()
}
add(list) {
var node = this.root
for (const item of list) {
if (!node.hasChild(item)) {
node.addChild(item)
}
node = node.child(item)
}
// Last element has been reached, so this last node is leaf
node.isLeaf = true
}
has(list) {
const lastNode = (list) => {
var node = this.root
for (const item of list) {
if (!node.hasChild(item)) {
return null
}
node = node.child(item)
}
if (node.isLeaf) {
return node
}
return null
}
return lastNode(list) !== null
}
remove(list) {
var node = this.root
var chain = []
for (const item of list) {
if (!node.hasChild(item)) {
return false
}
node = node.child(item)
chain.push(node)
}
// Here, list[i] has the value of the node in chain[i]
for (var i = chain.length - 1; i >= 0; i--) {
node = chain[i]
if (node.isLeaf) {
if (!node.hasChildren()) {
chain[i - 1].removeChild(list[i])
} else {
node.isLeaf = false
}
} else if (!node.hasChildren()) {
chain[i - 1].removeChild(list[i])
}
}
}
traverse() {
const traverse = (node, value, path, pathList) => {
path.push(value)
if (node.isLeaf) {
let pathClone = [...path]
pathClone.shift()
pathList.push(pathClone)
}
for (const [value, next_node] of node.children()) {
traverse(next_node, value, path, pathList)
}
path.pop()
}
var paths = []
traverse(this.root, undefined, [], paths)
return paths
}
}
module.exports = { Trie: Trie, }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment