Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat : added tries implementation using typescript #150

Merged
merged 7 commits into from
Sep 29, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions data_structures/tries/test/tries.test.ts
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);
});
});
64 changes: 64 additions & 0 deletions data_structures/tries/tries.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/**
* Trie Data structure
* It is a methodology of inserting word/string in O(n) time complexity where, n represents length of word
*
*/

/**
* TrieNode class definition
*/
class TrieNode {
children: { [key: string]: TrieNode } = {};
isWord: boolean = false;
}

export class Trie {
root: TrieNode = new TrieNode();

constructor() {}

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;
}


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;
}

/**
*
* Method to add word in Trie
*/
public add(word: string): this {
this.insertNode(this.root, word);
return this;
}

/**
* @param word
* @returns
* Method to find/search word inside letterMap which is of type Trie
*/

//isPrefixMatch is a parameter used to specify whether we perform prefix match or not
raklaptudirm marked this conversation as resolved.
Show resolved Hide resolved
public find(word: string, isPrefixMatch: boolean = false): boolean {
raklaptudirm marked this conversation as resolved.
Show resolved Hide resolved
return this.searchNode(this.root, word, isPrefixMatch);
}
}
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.