-
-
Notifications
You must be signed in to change notification settings - Fork 341
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added tries implementation using typescript (#150)
- Loading branch information
Showing
2 changed files
with
131 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { Trie } from "../tries"; | ||
|
||
describe('Trie', () => { | ||
let trie: Trie; | ||
|
||
beforeEach(() => { | ||
trie = new Trie(); | ||
}); | ||
|
||
it('should add and find a word', () => { | ||
trie.add('apple'); | ||
expect(trie.find('apple')).toBe(true); | ||
}); | ||
|
||
it('should not find a word that was not added', () => { | ||
trie.add('apple'); | ||
expect(trie.find('banana')).toBe(false); | ||
}); | ||
|
||
it('should not find a partial word', () => { | ||
trie.add('apple'); | ||
expect(trie.find('app')).toBe(false); | ||
}); | ||
|
||
it('should add and find multiple words', () => { | ||
trie.add('apple'); | ||
trie.add('banana'); | ||
trie.add('cherry'); | ||
expect(trie.find('apple')).toBe(true); | ||
expect(trie.find('banana')).toBe(true); | ||
expect(trie.find('cherry')).toBe(true); | ||
}); | ||
|
||
it('should find words with a common prefix', () => { | ||
trie.add('apple'); | ||
trie.add('appetizer'); | ||
expect(trie.find('app', true)).toBe(true); | ||
expect(trie.find('app', false)).toBe(false); | ||
}); | ||
}); |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/** | ||
* Represents a node in a Trie data structure. | ||
*/ | ||
class TrieNode { | ||
/** | ||
* An object that stores child nodes for each character in the alphabet. | ||
*/ | ||
children: { [key: string]: TrieNode } = {}; | ||
|
||
/** | ||
* Indicates whether the node represents the end of a word. | ||
*/ | ||
isWord: boolean = false; | ||
} | ||
|
||
/** | ||
* Trie Data structure for storing and searching words. | ||
*/ | ||
export class Trie { | ||
/** | ||
* The root node of the Trie. | ||
*/ | ||
root: TrieNode = new TrieNode(); | ||
|
||
/** | ||
* Creates a new Trie instance. | ||
*/ | ||
constructor() {} | ||
|
||
/** | ||
* Inserts a word into the Trie. | ||
* | ||
* @param word - The word to insert into the Trie. | ||
*/ | ||
private insertNode(node: TrieNode, word: string): void { | ||
for (const char of word) { | ||
if (!node.children[char]) { | ||
node.children[char] = new TrieNode(); | ||
} | ||
node = node.children[char]; | ||
} | ||
node.isWord = true; | ||
} | ||
|
||
/** | ||
* Searches for a word in the Trie. | ||
* | ||
* @param word - The word to search for. | ||
* @param isPrefixMatch - Indicates whether to perform a prefix match (default: false). | ||
* If true, the method returns true if the Trie contains words with the specified prefix. | ||
* If false, the method returns true only if an exact match is found. | ||
* @returns True if the word (or prefix) is found in the Trie; otherwise, false. | ||
*/ | ||
public find(word: string, isPrefixMatch: boolean = false): boolean { | ||
return this.searchNode(this.root, word, isPrefixMatch); | ||
} | ||
|
||
/** | ||
* Adds a word to the Trie. | ||
* | ||
* @param word - The word to add to the Trie. | ||
* @returns The Trie instance, allowing for method chaining. | ||
*/ | ||
public add(word: string): this { | ||
this.insertNode(this.root, word); | ||
return this; | ||
} | ||
|
||
/** | ||
* Searches for a word in the Trie. | ||
* | ||
* @param node - The current Trie node being examined. | ||
* @param word - The word to search for. | ||
* @param prefixMatch - Indicates whether to perform a prefix match. | ||
* @returns True if the word (or prefix) is found in the Trie; otherwise, false. | ||
* @private | ||
*/ | ||
private searchNode( | ||
node: TrieNode, | ||
word: string, | ||
prefixMatch: boolean | ||
): boolean { | ||
for (const char of word) { | ||
if (!node.children[char]) { | ||
return false; | ||
} | ||
node = node.children[char]; | ||
} | ||
return prefixMatch || node.isWord; | ||
} | ||
} |