Skip to content

Commit 24b02c1

Browse files
authored
feat: added tries implementation using typescript (TheAlgorithms#150)
1 parent 5f5352d commit 24b02c1

File tree

2 files changed

+131
-0
lines changed

2 files changed

+131
-0
lines changed
+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { Trie } from "../tries";
2+
3+
describe('Trie', () => {
4+
let trie: Trie;
5+
6+
beforeEach(() => {
7+
trie = new Trie();
8+
});
9+
10+
it('should add and find a word', () => {
11+
trie.add('apple');
12+
expect(trie.find('apple')).toBe(true);
13+
});
14+
15+
it('should not find a word that was not added', () => {
16+
trie.add('apple');
17+
expect(trie.find('banana')).toBe(false);
18+
});
19+
20+
it('should not find a partial word', () => {
21+
trie.add('apple');
22+
expect(trie.find('app')).toBe(false);
23+
});
24+
25+
it('should add and find multiple words', () => {
26+
trie.add('apple');
27+
trie.add('banana');
28+
trie.add('cherry');
29+
expect(trie.find('apple')).toBe(true);
30+
expect(trie.find('banana')).toBe(true);
31+
expect(trie.find('cherry')).toBe(true);
32+
});
33+
34+
it('should find words with a common prefix', () => {
35+
trie.add('apple');
36+
trie.add('appetizer');
37+
expect(trie.find('app', true)).toBe(true);
38+
expect(trie.find('app', false)).toBe(false);
39+
});
40+
});

data_structures/tries/tries.ts

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/**
2+
* Represents a node in a Trie data structure.
3+
*/
4+
class TrieNode {
5+
/**
6+
* An object that stores child nodes for each character in the alphabet.
7+
*/
8+
children: { [key: string]: TrieNode } = {};
9+
10+
/**
11+
* Indicates whether the node represents the end of a word.
12+
*/
13+
isWord: boolean = false;
14+
}
15+
16+
/**
17+
* Trie Data structure for storing and searching words.
18+
*/
19+
export class Trie {
20+
/**
21+
* The root node of the Trie.
22+
*/
23+
root: TrieNode = new TrieNode();
24+
25+
/**
26+
* Creates a new Trie instance.
27+
*/
28+
constructor() {}
29+
30+
/**
31+
* Inserts a word into the Trie.
32+
*
33+
* @param word - The word to insert into the Trie.
34+
*/
35+
private insertNode(node: TrieNode, word: string): void {
36+
for (const char of word) {
37+
if (!node.children[char]) {
38+
node.children[char] = new TrieNode();
39+
}
40+
node = node.children[char];
41+
}
42+
node.isWord = true;
43+
}
44+
45+
/**
46+
* Searches for a word in the Trie.
47+
*
48+
* @param word - The word to search for.
49+
* @param isPrefixMatch - Indicates whether to perform a prefix match (default: false).
50+
* If true, the method returns true if the Trie contains words with the specified prefix.
51+
* If false, the method returns true only if an exact match is found.
52+
* @returns True if the word (or prefix) is found in the Trie; otherwise, false.
53+
*/
54+
public find(word: string, isPrefixMatch: boolean = false): boolean {
55+
return this.searchNode(this.root, word, isPrefixMatch);
56+
}
57+
58+
/**
59+
* Adds a word to the Trie.
60+
*
61+
* @param word - The word to add to the Trie.
62+
* @returns The Trie instance, allowing for method chaining.
63+
*/
64+
public add(word: string): this {
65+
this.insertNode(this.root, word);
66+
return this;
67+
}
68+
69+
/**
70+
* Searches for a word in the Trie.
71+
*
72+
* @param node - The current Trie node being examined.
73+
* @param word - The word to search for.
74+
* @param prefixMatch - Indicates whether to perform a prefix match.
75+
* @returns True if the word (or prefix) is found in the Trie; otherwise, false.
76+
* @private
77+
*/
78+
private searchNode(
79+
node: TrieNode,
80+
word: string,
81+
prefixMatch: boolean
82+
): boolean {
83+
for (const char of word) {
84+
if (!node.children[char]) {
85+
return false;
86+
}
87+
node = node.children[char];
88+
}
89+
return prefixMatch || node.isWord;
90+
}
91+
}

0 commit comments

Comments
 (0)