https://github.com/navpreetdevpuri/trie-js
Trie data structure implementation in javascript
https://github.com/navpreetdevpuri/trie-js
data-structures javascript trie trie-data-structure typescript
Last synced: about 1 year ago
JSON representation
Trie data structure implementation in javascript
- Host: GitHub
- URL: https://github.com/navpreetdevpuri/trie-js
- Owner: NavpreetDevpuri
- License: mit
- Created: 2023-03-17T12:27:50.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2023-03-26T04:46:57.000Z (about 3 years ago)
- Last Synced: 2024-10-19T02:16:35.637Z (over 1 year ago)
- Topics: data-structures, javascript, trie, trie-data-structure, typescript
- Language: TypeScript
- Homepage:
- Size: 20.5 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# trie-js
Trie data structure implementation in javascript
# Example
```javascript
const Trie = require('@navpreetdevpuri/trie-js');
const names = [
'Easton Bass',
'Easdon Wilkinson',
'Easfon Barreson',
'Easgon Barrtera',
'Easson banson',
'Clarence Barrera',
'Heidi Murphy',
];
const nodes = [];
names.forEach((name) => {
const [firstName, lastName] = name.split(' ');
const node = {
key: `${firstName}${lastName}`.replace(/\s/g, ''), // remove spaces
value: {
firstName,
lastName,
},
};
nodes.push(node);
});
const trie = new Trie(nodes, true); // caseInsensitive = true
console.log(
trie.search({
skip: 1,
limit: 2,
prefix: 'Eas',
contains: 'son',
reverse: false,
})
);
/*
[
{ currPrefix: 'easfonbarreson', values: [ [Object] ] },
{ currPrefix: 'eassonbanson', values: [ [Object] ] }
]
*/
console.log(trie.getPreorderPredecessorAndSuccessorForNewkey('eastonbassy'));
/*
{
predecessorValues: [ { firstName: 'Easton', lastName: 'Bass' } ],
successorValues: [ { firstName: 'Heidi', lastName: 'Murphy' } ]
}
*/
console.log(
trie.getPreorderPredecessorAndSuccessorForExistingKey('eastonbass')
);
/*
{
predecessorValues: [ { firstName: 'Easson', lastName: 'banson' } ],
successorValues: [ { firstName: 'Heidi', lastName: 'Murphy' } ]
}
*/
```