cognitive-ts
Advanced tools
Comparing version 2.0.4 to 2.0.5
@@ -23,1 +23,12 @@ import * as _ from 'lodash' | ||
}) | ||
test('prefix search', () => { | ||
const t = new Trie() | ||
insertWords(t) | ||
const result = t.prefixSearch('car') | ||
expect(result).toHaveLength(4) | ||
expect(result).toContain('car') | ||
expect(result).toContain('cart') | ||
expect(result).toContain('carts') | ||
expect(result).toContain('card') | ||
}) |
@@ -86,2 +86,28 @@ import * as _ from 'lodash' | ||
findAllWords = (node: TrieNode, arr: string[] ) => { | ||
if (node.end) { | ||
arr.unshift(node.getWord()); | ||
} | ||
for (const child in node.children) { | ||
this.findAllWords(node.children[child], arr); | ||
} | ||
} | ||
prefixSearch = (prefix: string) => { | ||
let node = this.root | ||
const output: string[] = [] | ||
for(let i = 0; i < prefix.length; i++) { | ||
const letter = prefix[i] | ||
if (node.children[letter]) { | ||
node = node.children[letter]; | ||
} else { | ||
return output; | ||
} | ||
} | ||
this.findAllWords(node, output); | ||
return output; | ||
} | ||
delete = (word: string) => { | ||
@@ -88,0 +114,0 @@ if (!word) { |
{ | ||
"name": "cognitive-ts", | ||
"version": "2.0.4", | ||
"version": "2.0.5", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
21282
780