Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mzusin/mz-trie
Typescript implementation of trie/digital tree/radix tree/prefix tree/suffix tree.
https://github.com/mzusin/mz-trie
digital-tree longest-common-prefix prefix-tree suffix-tree suffix-trees trie trie-data-structure trie-tree tries
Last synced: about 1 month ago
JSON representation
Typescript implementation of trie/digital tree/radix tree/prefix tree/suffix tree.
- Host: GitHub
- URL: https://github.com/mzusin/mz-trie
- Owner: mzusin
- License: mit
- Created: 2023-11-17T09:39:23.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2023-11-18T12:45:43.000Z (about 1 year ago)
- Last Synced: 2023-11-19T10:44:44.936Z (about 1 year ago)
- Topics: digital-tree, longest-common-prefix, prefix-tree, suffix-tree, suffix-trees, trie, trie-data-structure, trie-tree, tries
- Language: TypeScript
- Homepage:
- Size: 129 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Trie
Typescript implementation of trie/digital tree/radix tree/prefix tree/suffix tree.
![](docs/trie.png)
## Definition
**Trie** (derived from re**trie**val) is a type of k-ary **search tree** used for storing and searching a specific string from a set. It is used to store a large amount of strings. The **pattern matching** can be done efficiently using tries.- Using trie, the key can be searched in **O(M)** time, where M is the **maximum string length**. However, the penalty is the storage requirements.
- Tries take less space when they contain a large number of short strings, as nodes are shared between the keys.## Usage Examples
- Autocompletion.
- The longest prefix.
- Spell-checking software.## Interfaces
```ts
export interface INode {
children: Map;
isEndOfWord: boolean;
}
export interface ITrie {
insert: (key: string) => void;
remove: (key: string) => void;
search: (key: string) => boolean;
isEmpty: (node?: INode) => boolean;
getLeavesCount: (node?: INode) => number;
getHeight: (node?: INode) => number;
printTrie: () => string;
log: () => string;
getWords: () => string[];
}export const trie: (keys?: string[]) => ITrie;
export const suffixTree: (text: string) => ITrie;
```### Documentation
```ts
const _trie = trie(['orange', 'apple']);console.log(_trie.search('orange')); // true or false
_trie.insert('potato');
_trie.remove('potato');console.log(_trie.log());
console.log(_trie.getWords()); // ['orange', 'apple']
console.log(_trie.printTrie());
console.log(_trie.longestCommonPrefix());
```### Suffix Tree
```ts
const st = suffixTree('banana');
console.log(st.search('apple')); // false
console.log(st.search('banana')); // true
console.log(st.search('anana')); // true
console.log(st.search('nana')); // true
console.log(st.search('ana')); // true
console.log(st.search('na')); // true
console.log(st.search('a')); // true
```**Suffix tree usage**
- Pattern Searching
- Finding the longest repeated substring
- Finding the longest common substring
- Finding the longest palindrome in a string