typescript-algorithms-and-datastructures
Version:
Useful algorithms and Data structures written in typescript.
68 lines • 2.3 kB
JavaScript
(function (factory) {
if (typeof module === "object" && typeof module.exports === "object") {
var v = factory(require, exports);
if (v !== undefined) module.exports = v;
}
else if (typeof define === "function" && define.amd) {
define(["require", "exports"], factory);
}
})(function (require, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
class TrieWithValue {
constructor() {
this.root = {};
this.getValue = function (word) {
const wordwSize = word.length;
let i = 0;
let level = this.root;
while (i < wordwSize) {
if (!level[word[i]]) {
return void 0;
}
level = level[word[i]];
i++;
if (i === wordwSize && level.hasOwnProperty(TrieWithValue.KEY)) {
return level[TrieWithValue.KEY];
}
}
return void 0;
};
}
insert(word, value = null) {
const wordwSize = word.length;
let i = 0;
let level = this.root;
while (i < wordwSize) {
if (!level[word[i]]) {
level[word[i]] = {};
}
level = level[word[i]];
i++;
if (i === wordwSize) {
level[TrieWithValue.KEY] = value;
}
}
return true;
}
contains(word) {
const wordwSize = word.length;
let i = 0;
let level = this.root;
while (i < wordwSize) {
if (!level[word[i]]) {
return false;
}
level = level[word[i]];
i++;
if (i === wordwSize && level.hasOwnProperty(TrieWithValue.KEY)) {
return true;
}
}
return false;
}
}
exports.TrieWithValue = TrieWithValue;
TrieWithValue.KEY = "id";
});
//# sourceMappingURL=TrieWithValue.js.map