A small package for trie structures
Project description
trie
This repository is dedicated to my implementation of trie structures for Python. This was mainly developed to handle vocabulary search.
A trie is a type of tree data structure, where each node identifies a string, and edges between nodes represent individual characters. Given a vocabulary, two nodes $n_1$ and $n_2$ are connected by an edge $c_1$ only if $n_1+c_1$ = $n_2$. The resulting structure can traversed depth-first to optimize search in the given vocabulary.
Our trie structure is initialized as follows:
from trie.trie import Trie
T = Trie()
Once it is initialized, our structure has only a _root_ node, which will represent the starting point of all the following nodes. The root node can be accessed through T.root, and it cannot have any parent node.
A node is represented by a simple object with a string value (usually a single character), a depth, an identifier, a parent and a list of children. Only the value is needed to initialize a basic node, as follows:
a = Node('a')
# this node represents the letter 'a'
Adding Nodes
Nodes can then be inserted in two different ways:
- manually by using the function
T.add\_node(parent_node, child_node)
T.add_node(T.root, a)
this will automatically assign the correct depth to the child node, give it an unique identifier, and update its parent node. Furthermore, the children list for T.root are updated as well.
- automatically using the function
T.insert_string(string). In this case, the module will look for the string in the current trie, and the it will insert all the characters that are not present in the structure
T.insert_string('alba')
#since the string 'a' is already present in the trie, this function will add nodes representing the strings 'al', 'alb', and 'alba'.
Searching for Strings
You can look for a specific string by using the function T.search(string), which will traverse the trie depth-first and will return the deepest node it can find given the query string. If the node is terminal (i.e., it does not have any children of its own) the string was found successfully.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file trie_gnolano-0.0.1.tar.gz.
File metadata
- Download URL: trie_gnolano-0.0.1.tar.gz
- Upload date:
- Size: 3.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.6.0 importlib_metadata/4.6.3 pkginfo/1.7.1 requests/2.27.0 requests-toolbelt/0.9.1 tqdm/4.62.1 CPython/3.10.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bd94cfbbf6134d166904e60cc80cf30a6fc01389e3bc13eae9956f1a97ff3169
|
|
| MD5 |
8e2a1f15aee94e0356ad5138d371be27
|
|
| BLAKE2b-256 |
d813a785ad7fffbe86001ceae80dafef5e4906aa82653690215426bdbd6ac89f
|
File details
Details for the file trie_gnolano-0.0.1-py3-none-any.whl.
File metadata
- Download URL: trie_gnolano-0.0.1-py3-none-any.whl
- Upload date:
- Size: 5.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.6.0 importlib_metadata/4.6.3 pkginfo/1.7.1 requests/2.27.0 requests-toolbelt/0.9.1 tqdm/4.62.1 CPython/3.10.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0474989c90ed8461c4abf6588d4afa7de216f9e9d99e47e08a3ae87bbb592e43
|
|
| MD5 |
f173988d7471e0a036dfd68170993ac5
|
|
| BLAKE2b-256 |
a4711933da7c14d01087b881dedf7b0c90299f4e2f5a8ee202e6a37d5690a93a
|