Giter Club home page Giter Club logo

trie's Introduction

Trie

An implementation of a Trie

The Trie can either be created string by string or from an array of strings.

Example

import { Trie } from "trie-structure";

const trie = new Trie();
const strings = ["he", "hello", "helios", "woof", "dog", "doom"];

trie.addMany(strings);

const allWords = trie.getAllWords(); // => ["he", "hello", "helios", "woof", "dog", "doom"]
const helPrefixedWords = trie.findWords("he"); // => ["hello", "helios"];

Usage

public methods:

class Trie {
  public add(word: string): void; // adds the word to the Trie

  public addMany(words: string[]): void; // invokes add for each string

  public remove(word: string): boolean; // removes the word from the Trie, does not delete the node

  public findWords(prefix: string): string[]; // Matches all words by the given prefix

  public size(): number; // returns how many full words are in the Trie

  public getAllWords(): string[]; // returns an array of all the words in the Trie

  public contains(word: string): boolean; // returns true if the given string exists within the tree, may not be a full word

  public findNode(prefix: string): TrieNode | undefined; // returns first match
}

The items in the Trie are stored as TrieNodes these should not need to be directly referenced

class TrieNode {
  public isLeaf: boolean; // defaults to false
  public readonly children: Map<string, TrieNode>; // defaults to an empty map

  public constructor(public readonly char: string) {}
}

trie's People

Contributors

jamesbarford avatar

Watchers

James Cloos avatar  avatar  avatar

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.