
Product
Introducing GitHub Actions Scanning Support
Detect malware, unsafe data flows, and license issues in GitHub Actions with Socket’s new workflow scanning support.
ternary-search-trie
Advanced tools
A ternary search trie implementation in TypeScript.
You can install the package via npm or yarn.
npm install ternary-search-trie
yarn add ternary-search-trie
Represents a ternary search trie.
import { Trie } from 'ternary-search-trie';
interface Value {
data: string;
}
const trie = new Trie<Value>();
Returns true if the tree contains any nodes, otherwise false.
console.log(trie.isEmpty);
//=> true
Gets the size of the tree in terms of the number of nodes present within the tree.
console.log(trie.size);
//=> 0
set(key: string, value: Value): Trie<Value>
Adds the specified key/value pair to the tree.
Example:
const value = { data: 'test' };
trie.set('data', value);
get(key: string): Value | null
Returns the value of the node with the specified key.
Example:
const value = { data: 'test' };
trie.set('data', value);
console.log(trie.get('data'));
//=> { data: 'test' }
del(key: string): Trie<Value>
Deletes the node with the specified key.
Example:
const value = { data: 'test' };
trie.set('data', value);
console.log(trie.get('data'));
//=> { data: 'test' }
trie.del('data');
console.log(trie.get('data'));
//=> null;
contains(key: string): boolean
Checks if a node with the specified key exists in the tree.
Example:
console.log(trie.contains('foo'));
//=> false
keys(): string[]
Returns an array of all keys present in the tree.
Example:
const value = { data: 'test' };
trie.set('foo', value);
trie.set('bar', value);
trie.set('baz', value);
console.log(trie.keys());
//=> [ 'bar', 'baz', 'foo' ]
keysWithPrefix(prefix: string): string[]
Returns all keys present in the tree that begin with the specified prefix.
Example:
const value = { data: 'test' };
trie.set('foo', value);
trie.set('bar', value);
trie.set('baz', value);
console.log(trie.keysWithPrefix('ba'));
//=> [ 'bar', 'baz' ]
searchWithPrefix(prefix: string, callback: (key: string, value: Value) => void): void;
Executes the specified callback at each node in the tree whose key begins with the specified prefix.
Example:
const value = { data: 'test' };
trie.set('foo', value);
trie.set('bar', value);
trie.set('baz', value);
trie.searchWithPrefix('ba', (key, value) => console.log({ key, value }));
//=> { key: 'bar', value: { data: 'test' } }
//=> { key: 'baz', value: { data: 'test' } }
dfs(callback: (key: string, value: Value | null) => void): void;
Performs a depth-first search of the tree beginning from the root node. Executes the specified callback at each visited node.
Example:
const value = { data: 'test' };
trie.set('foo', value);
trie.dfs((key, value) => console.log({ key, value }));
//=> { key: 'f', value: null }
//=> { key: 'o', value: null }
//=> { key: 'o', value: { data: 'test' } }
Returns the tree as a string.
Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us.
This project is licensed under the MIT License - see the LICENSE file for details
FAQs
A ternary search tree implementation in TypeScript.
We found that ternary-search-trie demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Detect malware, unsafe data flows, and license issues in GitHub Actions with Socket’s new workflow scanning support.
Product
Add real-time Socket webhook events to your workflows to automatically receive pull request scan results and security alerts in real time.
Research
The Socket Threat Research Team uncovered malicious NuGet packages typosquatting the popular Nethereum project to steal wallet keys.