tinytrie


Nametinytrie JSON
Version 0.1.0a2 PyPI version JSON
download
home_pageNone
SummaryA minimal type-safe trie (prefix tree) implementation in Python.
upload_time2025-07-08 15:21:14
maintainerNone
docs_urlNone
authorNone
requires_python>=2
licenseNone
keywords trie prefix tree data structure python
VCS
bugtrack_url
requirements typing
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # TinyTrie

A minimal and type-safe trie (prefix tree) implementation for Python 2+.

## Features

- **Typed**: Works with arbitrary key and value types (`Generic[K, V]`)
- **Minimal**: Only essential functionalities
- **Efficient**: Memory-efficient with `__slots__`
- **Iterable**: Easily traverse and list all stored sequences
- **No external dependencies** (except `typing` on Python <3.5)

## Basic Operations

```python 
from tinytrie import *

# Create a trie with character (`str`) keys and integer values
root = TrieNode[str, int]()

# Insert some words with values
update(root, "apple", 1)
update(root, "app", 2)
update(root, "banana", 3)
update(root, "band", 4)

# Search for existing words
assert search(root, "apple").value == 1
assert search(root, "app").value == 2
assert search(root, "banana").value == 3

# Search for non-existent words
assert search(root, "orange") is None
assert search(root, "appetizer") is None

update(root, "apple", 10)
assert search(root, "apple").value == 10  # Value updated

# Insert a new word
update(root, "orange", 5)
assert search(root, "orange").value == 5

# Delete "apple", "app" remains
assert delete(root, "apple") is True
assert search(root, "apple") is None
assert delete(root, "apple") is False
assert search(root, "app") is not None

# Add back "apple", delete "app", "apple" remains
update(root, "apple", 10)
assert delete(root, "app") is True
assert search(root, "app") is None
assert delete(root, "app") is False
assert search(root, "apple") is not None

# Try to delete non-existent words
assert delete(root, "ban") is False
assert delete(root, "appetizer") is False

# Get common prefix from root (no common prefix)
prefix, node = longest_common_prefix(root)
assert prefix == []  # No common prefix among all words

# Get common prefix from "b" subtree
prefix, node = longest_common_prefix(root.children["b"])
assert prefix == ["a", "n"]  # Common between "banana" and "band" after "b"


# Get all words in the trie
words = ["".join(s) for s, _ in collect_sequences(root)]
assert set(words) == {"apple", "banana", "band", "orange"}
```

## Non-String Keys Example

```python
from tinytrie import *

# Create a trie with tuple keys
trajectory_trie = TrieNode[Tuple[int, int], str]()
update(trajectory_trie, [(1,2), (3,4)], "traj1")
update(trajectory_trie, [(1,2), (5,6)], "traj2")

assert search(trajectory_trie, [(1,2), (3,4)]).value == "traj1"
assert search(trajectory_trie, [(1,2), (5,6)]).value == "traj2"
assert search(trajectory_trie, [(1,2)]) is None  # Partial path

prefix, _ = longest_common_prefix(trajectory_trie)
assert prefix == [(1, 2)]
```

## Contributing

Contributions are welcome! Please submit pull requests or open issues on the GitHub repository.

## License

This project is licensed under the [MIT License](LICENSE).

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "tinytrie",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=2",
    "maintainer_email": null,
    "keywords": "trie, prefix tree, data structure, python",
    "author": null,
    "author_email": "Jifeng Wu <jifengwu2k@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/d6/e9/0771f7fe8b22abb6e96179fa43c0b0425d284651601d939284cbf1bcef3a/tinytrie-0.1.0a2.tar.gz",
    "platform": null,
    "description": "# TinyTrie\n\nA minimal and type-safe trie (prefix tree) implementation for Python 2+.\n\n## Features\n\n- **Typed**: Works with arbitrary key and value types (`Generic[K, V]`)\n- **Minimal**: Only essential functionalities\n- **Efficient**: Memory-efficient with `__slots__`\n- **Iterable**: Easily traverse and list all stored sequences\n- **No external dependencies** (except `typing` on Python <3.5)\n\n## Basic Operations\n\n```python \nfrom tinytrie import *\n\n# Create a trie with character (`str`) keys and integer values\nroot = TrieNode[str, int]()\n\n# Insert some words with values\nupdate(root, \"apple\", 1)\nupdate(root, \"app\", 2)\nupdate(root, \"banana\", 3)\nupdate(root, \"band\", 4)\n\n# Search for existing words\nassert search(root, \"apple\").value == 1\nassert search(root, \"app\").value == 2\nassert search(root, \"banana\").value == 3\n\n# Search for non-existent words\nassert search(root, \"orange\") is None\nassert search(root, \"appetizer\") is None\n\nupdate(root, \"apple\", 10)\nassert search(root, \"apple\").value == 10  # Value updated\n\n# Insert a new word\nupdate(root, \"orange\", 5)\nassert search(root, \"orange\").value == 5\n\n# Delete \"apple\", \"app\" remains\nassert delete(root, \"apple\") is True\nassert search(root, \"apple\") is None\nassert delete(root, \"apple\") is False\nassert search(root, \"app\") is not None\n\n# Add back \"apple\", delete \"app\", \"apple\" remains\nupdate(root, \"apple\", 10)\nassert delete(root, \"app\") is True\nassert search(root, \"app\") is None\nassert delete(root, \"app\") is False\nassert search(root, \"apple\") is not None\n\n# Try to delete non-existent words\nassert delete(root, \"ban\") is False\nassert delete(root, \"appetizer\") is False\n\n# Get common prefix from root (no common prefix)\nprefix, node = longest_common_prefix(root)\nassert prefix == []  # No common prefix among all words\n\n# Get common prefix from \"b\" subtree\nprefix, node = longest_common_prefix(root.children[\"b\"])\nassert prefix == [\"a\", \"n\"]  # Common between \"banana\" and \"band\" after \"b\"\n\n\n# Get all words in the trie\nwords = [\"\".join(s) for s, _ in collect_sequences(root)]\nassert set(words) == {\"apple\", \"banana\", \"band\", \"orange\"}\n```\n\n## Non-String Keys Example\n\n```python\nfrom tinytrie import *\n\n# Create a trie with tuple keys\ntrajectory_trie = TrieNode[Tuple[int, int], str]()\nupdate(trajectory_trie, [(1,2), (3,4)], \"traj1\")\nupdate(trajectory_trie, [(1,2), (5,6)], \"traj2\")\n\nassert search(trajectory_trie, [(1,2), (3,4)]).value == \"traj1\"\nassert search(trajectory_trie, [(1,2), (5,6)]).value == \"traj2\"\nassert search(trajectory_trie, [(1,2)]) is None  # Partial path\n\nprefix, _ = longest_common_prefix(trajectory_trie)\nassert prefix == [(1, 2)]\n```\n\n## Contributing\n\nContributions are welcome! Please submit pull requests or open issues on the GitHub repository.\n\n## License\n\nThis project is licensed under the [MIT License](LICENSE).\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "A minimal type-safe trie (prefix tree) implementation in Python.",
    "version": "0.1.0a2",
    "project_urls": {
        "Bug Tracker": "https://github.com/jifengwu2k/tinytrie/issues",
        "Homepage": "https://github.com/jifengwu2k/tinytrie"
    },
    "split_keywords": [
        "trie",
        " prefix tree",
        " data structure",
        " python"
    ],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "24b1665e2767033a8faec844fb3e063060cbc0f5a538cd58c2d71f0958117f1f",
                "md5": "1bfc02dddebc7a9923a278a46f6891b1",
                "sha256": "d47ae8a4cace80a00758fb28620e1ea56353479fe0c1b8413f653b8238bcc0c8"
            },
            "downloads": -1,
            "filename": "tinytrie-0.1.0a2-py2.py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "1bfc02dddebc7a9923a278a46f6891b1",
            "packagetype": "bdist_wheel",
            "python_version": "py2.py3",
            "requires_python": ">=2",
            "size": 4729,
            "upload_time": "2025-07-08T15:21:12",
            "upload_time_iso_8601": "2025-07-08T15:21:12.079948Z",
            "url": "https://files.pythonhosted.org/packages/24/b1/665e2767033a8faec844fb3e063060cbc0f5a538cd58c2d71f0958117f1f/tinytrie-0.1.0a2-py2.py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "d6e90771f7fe8b22abb6e96179fa43c0b0425d284651601d939284cbf1bcef3a",
                "md5": "923ca16ee8d06a3b436966177c8a1f97",
                "sha256": "3b2705c6f0b17113e4220b79e41dba25e24004788e24d6cb7aebea1a4eee0954"
            },
            "downloads": -1,
            "filename": "tinytrie-0.1.0a2.tar.gz",
            "has_sig": false,
            "md5_digest": "923ca16ee8d06a3b436966177c8a1f97",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=2",
            "size": 4442,
            "upload_time": "2025-07-08T15:21:14",
            "upload_time_iso_8601": "2025-07-08T15:21:14.287742Z",
            "url": "https://files.pythonhosted.org/packages/d6/e9/0771f7fe8b22abb6e96179fa43c0b0425d284651601d939284cbf1bcef3a/tinytrie-0.1.0a2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-07-08 15:21:14",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "jifengwu2k",
    "github_project": "tinytrie",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "requirements": [
        {
            "name": "typing",
            "specs": []
        }
    ],
    "lcname": "tinytrie"
}
        
Elapsed time: 3.20287s