Created
February 12, 2020 17:07
-
-
Save sanishkr/a322940c49b7d566ea4b13ca221104d1 to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/hewirun
This file contains hidden or 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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<script id="jsbin-javascript"> | |
"use strict"; | |
var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })(); | |
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | |
var SIZE = 26; | |
var TrieNode = (function () { | |
function TrieNode() { | |
var data = arguments.length <= 0 || arguments[0] === undefined ? null : arguments[0]; | |
_classCallCheck(this, TrieNode); | |
this.endOfWord = false; | |
this.children = Array(SIZE).fill(data); | |
} | |
_createClass(TrieNode, [{ | |
key: "containsKey", | |
value: function containsKey(ch) { | |
return this.children[ch.charCodeAt(0) - 97] !== null; | |
} | |
}, { | |
key: "setNode", | |
value: function setNode(ch, node) { | |
this.children[ch.charCodeAt(0) - 97] = node; | |
} | |
}, { | |
key: "setEnd", | |
value: function setEnd() { | |
this.endOfWord = true; | |
} | |
}, { | |
key: "getNode", | |
value: function getNode(ch) { | |
return this.children[ch.charCodeAt(0) - 97]; | |
} | |
}]); | |
return TrieNode; | |
})(); | |
var Trie = (function () { | |
function Trie() { | |
_classCallCheck(this, Trie); | |
this.root = new TrieNode(); | |
} | |
_createClass(Trie, [{ | |
key: "insert", | |
value: function insert(word) { | |
var node = this.root; | |
for (var i in word) { | |
if (!node.containsKey(word[i])) { | |
node.setNode(word[i], new TrieNode()); | |
} | |
node = node.getNode(word[i]); | |
} | |
node.setEnd(); | |
} | |
}, { | |
key: "searchPrefix", | |
value: function searchPrefix(word) { | |
var node = this.root; | |
for (var i in word) { | |
if (!node.containsKey(word[i])) { | |
return null; | |
} | |
node = node.getNode(word[i]); | |
} | |
return node; | |
} | |
}, { | |
key: "startsWith", | |
value: function startsWith(word) { | |
return this.searchPrefix(word) !== null; | |
} | |
}, { | |
key: "searchWord", | |
value: function searchWord(word) { | |
var node = this.searchPrefix(word); | |
if (node !== null && node.endOfWord) { | |
return true; | |
} else { | |
return false; | |
} | |
} | |
}]); | |
return Trie; | |
})(); | |
var trie = new Trie(); | |
trie.insert("text"); | |
trie.insert("string"); | |
trie.insert("happy"); | |
console.log(trie.searchWord("text")); | |
console.log(trie.startsWith("happy")); | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">const SIZE = 26 | |
class TrieNode { | |
constructor(data=null){ | |
this.endOfWord = false; | |
this.children = Array(SIZE).fill(data) | |
} | |
containsKey(ch){ | |
return this.children[ch.charCodeAt(0) - 97] !== null | |
} | |
setNode(ch, node){ | |
this.children[ch.charCodeAt(0) - 97] = node | |
} | |
setEnd(){ | |
this.endOfWord = true | |
} | |
getNode(ch){ | |
return this.children[ch.charCodeAt(0) - 97] | |
} | |
} | |
class Trie { | |
constructor() { | |
this.root = new TrieNode() | |
} | |
insert(word) { | |
let node = this.root; | |
for(let i in word){ | |
if(!node.containsKey(word[i])) { | |
node.setNode(word[i], new TrieNode()) | |
} | |
node = node.getNode(word[i]) | |
} | |
node.setEnd() | |
} | |
searchPrefix(word) { | |
let node = this.root; | |
for(let i in word){ | |
if(!node.containsKey(word[i])) { | |
return null | |
} | |
node = node.getNode(word[i]) | |
} | |
return node | |
} | |
startsWith(word){ | |
return this.searchPrefix(word) !== null | |
} | |
searchWord(word){ | |
const node = this.searchPrefix(word) | |
if(node !== null && node.endOfWord){ | |
return true | |
} else { | |
return false | |
} | |
} | |
} | |
let trie = new Trie() | |
trie.insert("text") | |
trie.insert("string") | |
trie.insert("happy") | |
console.log(trie.searchWord("text")) | |
console.log(trie.startsWith("happy")) | |
</script></body> | |
</html> |
This file contains hidden or 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
"use strict"; | |
var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })(); | |
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | |
var SIZE = 26; | |
var TrieNode = (function () { | |
function TrieNode() { | |
var data = arguments.length <= 0 || arguments[0] === undefined ? null : arguments[0]; | |
_classCallCheck(this, TrieNode); | |
this.endOfWord = false; | |
this.children = Array(SIZE).fill(data); | |
} | |
_createClass(TrieNode, [{ | |
key: "containsKey", | |
value: function containsKey(ch) { | |
return this.children[ch.charCodeAt(0) - 97] !== null; | |
} | |
}, { | |
key: "setNode", | |
value: function setNode(ch, node) { | |
this.children[ch.charCodeAt(0) - 97] = node; | |
} | |
}, { | |
key: "setEnd", | |
value: function setEnd() { | |
this.endOfWord = true; | |
} | |
}, { | |
key: "getNode", | |
value: function getNode(ch) { | |
return this.children[ch.charCodeAt(0) - 97]; | |
} | |
}]); | |
return TrieNode; | |
})(); | |
var Trie = (function () { | |
function Trie() { | |
_classCallCheck(this, Trie); | |
this.root = new TrieNode(); | |
} | |
_createClass(Trie, [{ | |
key: "insert", | |
value: function insert(word) { | |
var node = this.root; | |
for (var i in word) { | |
if (!node.containsKey(word[i])) { | |
node.setNode(word[i], new TrieNode()); | |
} | |
node = node.getNode(word[i]); | |
} | |
node.setEnd(); | |
} | |
}, { | |
key: "searchPrefix", | |
value: function searchPrefix(word) { | |
var node = this.root; | |
for (var i in word) { | |
if (!node.containsKey(word[i])) { | |
return null; | |
} | |
node = node.getNode(word[i]); | |
} | |
return node; | |
} | |
}, { | |
key: "startsWith", | |
value: function startsWith(word) { | |
return this.searchPrefix(word) !== null; | |
} | |
}, { | |
key: "searchWord", | |
value: function searchWord(word) { | |
var node = this.searchPrefix(word); | |
if (node !== null && node.endOfWord) { | |
return true; | |
} else { | |
return false; | |
} | |
} | |
}]); | |
return Trie; | |
})(); | |
var trie = new Trie(); | |
trie.insert("text"); | |
trie.insert("string"); | |
trie.insert("happy"); | |
console.log(trie.searchWord("text")); | |
console.log(trie.startsWith("happy")); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment