Metadata-Version: 2.1
Name: trie-gnolano
Version: 0.0.1
Summary: A small package for trie structures
Project-URL: Bug Tracker, https://github.com/Nolanogenn/trie/issues
Project-URL: Homepage, https://github.com/Nolanogenn/trie
Author-email: Gennaro Nolano <nolanogenn@gmail.com>
License: Copyright (c) 2018 The Python Packaging Authority
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Requires-Python: >=3.7
Description-Content-Type: text/markdown

# 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:

1. 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.

2. 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.



