trie

Prefix tree (Trie) for word insertion, lookup, deletion, prefixes and autocomplete.

v1.0.0Data structuresbundledimport "trie";
Overview Installation Documentation Examples

Overview

A zero-dependency prefix tree (Trie) library for HolyPHP supporting fast word insertion, lookup, deletion with node pruning, prefix checking, lexicographically sorted autocomplete, and word counting.

bundled — works offline zero dependencies prefix autocomplete

Quick start

import "trie";

$trie = trie_from_array(["apple", "app", "application", "apricot", "banana"]);

println($trie->search("app"));           // true
println($trie->startsWith("apr"));       // true
println($trie->count());                 // 5

$suggestions = $trie->autocomplete("app", 3);
// ["app", "apple", "application"]

$trie->delete("app");
println($trie->search("app"));           // false
println($trie->search("apple"));         // true

Installation

In a project

Inside a project (a folder with hphp.yaml, see Projects & packages):

hphp install trie

This adds the library to hphp.yaml, installs it into .hphp/packages/trie/ and pins the exact version in hphp.lock:

require:
  trie: "^1.0"

Use it

import "trie";

trie is bundled with the compiler, so import "trie"; also works in a single file without installing anything, and hphp install works offline.

Outside a project

hphp install trie          # for your user, shared by all scripts
hphp create myapp                 # or start a project, then: hphp install trie
Packagetrie
Version1.0.0 (follows the compiler)
Entry filetrie.hphp
Sourcepackages/lib/trie ↗

Documentation

API Reference

Helper Functions

Trie Class

Error Handling

Invalid arguments throw an Exception with a clear trie: ... message:

Examples

01Quick start

Bundled with the compiler — import "trie"; works offline in any file, or add it to a project with hphp install trie.

import "trie";

$trie = trie_from_array(["apple", "app", "application", "apricot", "banana"]);

println($trie->search("app"));           // true
println($trie->startsWith("apr"));       // true
println($trie->count());                 // 5

$suggestions = $trie->autocomplete("app", 3);
// ["app", "apple", "application"]

$trie->delete("app");
println($trie->search("app"));           // false
println($trie->search("apple"));         // true
Try it
hphp install trie
hphp run main.hphp

Covered by the official test suite in tests/positive/lib_trie.hphp.