Skip to main content

No Frills Prefix Tree (nf_prefix_tree)

This is a prefix tree with no frills. Supports simple insert, search, and basic querying of strings that a (can be) keyed. Written in C++ 11 and has python wrappers. Written because I couldn't find a fast, easy to install and use basic prefix tree in python.

Installation

C++

In order to build the C++, you need to have

  1. C++ 11
  2. g++ compiler

This program uses the STL so no other packages are necessary

Linux and macOS

  1. Change to the nf_prefix_tree directory
  2. Run chmod +x buildAndTest.sh
  3. Run ./buildAndTest.sh

If no errors occur, you should be good to go!

Windows

In order to build, ensure you have the g++ compiler and it is added to path

  1. Change to nf_prefix_tree/src/ directory
  2. Run make
  3. Change to nf_prefix_tree/tests/ directory if you want to run tests
  4. Run make
  5. Run ./testmain

Python

If you are installing on pip and use macOS, all you need to run is pip install nf_prefix_tree

Otherwise you will need to build from source. First follow the above steps for C++. After doing so, make sure you have the following

  1. Python 3
  2. Cython package

Once you have these, change into the python_bindings directory. Run python3 setup.py build_ext --inplace. You can now do from nf_prefix_tree import PyPrefixTree in the same directory!

Examples

C++

#include "PrefixTree.hpp"

int main() {

    PrefixTree * t = new PrefixTree();

    t->addSequence("ABCDEFG", "Key1");
    t->addSequence("ABCDEFG", "Key2");
    t->addSequence("ABCXYZ", "Key3");
    t->addSequence("LMNOP", "Key4");

    t->show();

    t->show(2);
}

Output:

root
|-> Sequence: A	 keys: Key1, Key2, Key3, 
 |-> Sequence: AB	 keys: Key1, Key2, Key3, 
  |-> Sequence: ABC	 keys: Key1, Key2, Key3, 
   |-> Sequence: ABCD	 keys: Key1, Key2, 
    |-> Sequence: ABCDE	 keys: Key1, Key2, 
     |-> Sequence: ABCDEF	 keys: Key1, Key2, 
      |-> Sequence: ABCDEFG	 keys: Key1, Key2, 
   |-> Sequence: ABCX	 keys: Key3, 
    |-> Sequence: ABCXY	 keys: Key3, 
     |-> Sequence: ABCXYZ	 keys: Key3, 
|-> Sequence: L	 keys: Key4, 
 |-> Sequence: LM	 keys: Key4, 
  |-> Sequence: LMN	 keys: Key4, 
   |-> Sequence: LMNO	 keys: Key4, 
    |-> Sequence: LMNOP	 keys: Key4, 
root
|-> Sequence: A	 keys: Key1, Key2, Key3, 
 |-> Sequence: AB	 keys: Key1, Key2, Key3, 
|-> Sequence: L	 keys: Key4, 
 |-> Sequence: LM	 keys: Key4, 

Python

from nf_prefix_tree import PyPrefixTree

t = PyPrefixTree()

t.addSequence('ABCDE', 'key1')
t.addSequence('ABCXY', 'key2')
t.addSequence('ZYAPW', 'key3')

t.show()

Output:

root
|-> Sequence: A	 keys: key1, key2, 
 |-> Sequence: AB	 keys: key1, key2, 
  |-> Sequence: ABC	 keys: key1, key2, 
   |-> Sequence: ABCD	 keys: key1, 
    |-> Sequence: ABCDE	 keys: key1, 
   |-> Sequence: ABCX	 keys: key2, 
    |-> Sequence: ABCXY	 keys: key2, 
|-> Sequence: Z	 keys: key3, 
 |-> Sequence: ZY	 keys: key3, 
  |-> Sequence: ZYA	 keys: key3, 
   |-> Sequence: ZYAP	 keys: key3, 
    |-> Sequence: ZYAPW	 keys: key3, 

API

C++

  • PrefixTree()
    • Constructor for the prefix tree
  • void addSequence(sequence, key)
    • Add a sequence with a key to the prefix tree
    • sequence - std::string the sequence to compress via tree
    • key - std::string the key for this sequence. If not desired, make sequence empty string
  • bool hasPrefix(prefix)
    • Check to see if the input sequence is in the tree
    • prefix - std::string the prefix to look up
  • std::vector<std::string> getKeysWithPrefix(prefix)
    • Get all keys associated with a prefix. Empty vector returned if the prefix is not in the tree
    • prefix - std::string the prefix to look for
  • void show(level=-1)
    • Show the tree in the console down to the specified level
    • level -- int the level down to which to print the tree. Leave as -1 if you want to print the entire tree

Python

  • PyPrefixTree()
    • Constructor for the prefix tree
  • addSequence(sequence, key) -> None
    • Add a sequence with a key to the prefix tree
    • sequence - str the sequence to compress via tree
    • key - str the key for this sequence. If not desired, make
  • hasPrefix(prefix) -> bool
    • Check to see if the input sequence is in the tree
    • prefix - str the prefix to look up
    • Returns True if prefix is found, False otherwise
  • getKeysWithPrefix(prefix) -> list
    • Get all keys associated with a prefix
    • prefix - str the prefix to look for
    • Returns a list of str of all keys. [] is returned if the prefix is not in the tree
  • show(level=-1)
    • Show the tree in the console down to the specified level
    • level -- int the level down to which to print the tree. Leave as -1 if you want to print the entire tree

Metadata

Release files for nf-prefix-tree 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for nf-prefix-tree 0.1.0
File Size Uploaded Provenance Actions
nf_prefix_tree-0.1.0.tar.gz 70.0 kB Without provenance Details for nf_prefix_tree-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for nf-prefix-tree 0.1.0
File Interpreter ABI Platform Actions
nf_prefix_tree-0.1.0-cp37-cp37m-macosx_10_9_x86_64.whl CPython 3.7 CPython 3.7 pymalloc macOS 10.9+ x86-64 Details for nf_prefix_tree-0.1.0-cp37-cp37m-macosx_10_9_x86_64.whl

Total release size: 124.5 kB

Release files / nf_prefix_tree-0.1.0.tar.gz

Download URL nf_prefix_tree-0.1.0.tar.gz
Size 70.0 kB
Tags Source
SHA-256 checksum
How to use checksums
cfdd3a4f7967719b296889b8a614e06595ca999467178944825a7cd2ec3ad1eb
BLAKE2b-256 checksum
How to use checksums
a8b95066b6c1d9e96f4ef02be1d78954261cb00e99c261fb640a1ae6ff0059c2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.7.4

Release files / nf_prefix_tree-0.1.0-cp37-cp37m-macosx_10_9_x86_64.whl

Download URL nf_prefix_tree-0.1.0-cp37-cp37m-macosx_10_9_x86_64.whl
Size 54.5 kB
Tags CPython 3.7 CPython 3.7 pymalloc macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
ec25f5558633157cafd68764664302b43f41e48a49620626fa5df351f31dc89a
BLAKE2b-256 checksum
How to use checksums
68a4a3b0fb3b1d5fe8de076b2239927fcdf16196b64866b5470e2423d26f0436
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.7.4

Security for nf-prefix-tree 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page