Git Product home page Git Product logo

ternary-search-trie's Introduction

Ternary Search Trie

A ternary search trie implementation in TypeScript.

Installing

You can install the package via npm or yarn.

NPM

npm install ternary-search-trie

Yarn

yarn add ternary-search-trie

Documentation

Trie

Represents a ternary search trie.

Usage

import { Trie } from 'ternary-search-trie';

interface Value {
  data: string;
}

const trie = new Trie<Value>();

Members

isEmpty

Returns true if the tree contains any nodes, otherwise false.

console.log(trie.isEmpty);
//=> true

size

Gets the size of the tree in terms of the number of nodes present within the tree.

console.log(trie.size);
//=> 0

Methods

set

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

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

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

contains(key: string): boolean

Checks if a node with the specified key exists in the tree.

Example:

console.log(trie.contains('foo'));
//=> false

keys

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

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

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

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

toString()

Returns the tree as a string.

Contributing

Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us.

License

This project is licensed under the MIT License - see the LICENSE file for details

Acknowledgments

  • This project was heavily inspired by work done by @jakwings on node-ternary-search-trie. This project differs in that it is written in TypeScript and chose to implement the underlying functionality of the tree by making heavy use of recursion instead of loops.

ternary-search-trie's People

Contributors

imax153 avatar

Stargazers

 avatar

Watchers

 avatar

ternary-search-trie's Issues

Benchmarks

  • Add insert, lookup, and delete benchmarks
  • The ternary search trie implementation used by the library relies heavily upon recursion. Therefore, the point at which a user could be in danger of overflowing the stack with recursive function calls should probably be determined, if possible.

Avoid exposing the Node interface

The current implementation of the callbacks that can be supplied to searchWithPrefix and dfs accept the entire Node object as a parameter. However, Nodes should never be exposed to the end user, only the key and value of the Node. In addition, the key value received by the searchWithPrefix callback should probably be the entire key used to retrieve the value of that Node, not just the single letter of the Node.

  • Provide the entire key as the parameter supplied to searchWithPrefix callback
  • Avoid exposing the full Node object in the callback parameter supplied to searchWithPrefix
  • Avoid exposing the full Node object in the callback parameter supplied to dfs

Improve README

There are several improvements that should be made to the README to make it easier for new users of the library to get started.

  • Change the format of the documentation
    • It would probably be more clear to the end user if the documentation for each method were in the form of type annotations as opposed to a table
  • Add examples for each method

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.