Skip to main content

Simpletree3 module

Offers classes implementing basic tree-like functionality, as well as iterators for walking the trees and node search functionality. Requires Python 3.10 or later.

The design goal is to provide a basic class implementing the essential tree structure and functionality. Practical applications will want to inherit from one of the provided classes in order to provide custom functionality. Tree walking algorithms are implemented as generators, in order to minimize the use of temporary objects and focus on speed and efficiency.

Classes

Two classes are provided: SimpleNode and FlexibleNode.

SimpleNode

This is the base class that provides the tree node functionality by connecting parents to children. Additional functionality can be added by deriving from SimpleNode.

Each node is identified by a key parameter of an arbitrary hashable type. Node keys must be unique among the direct children of a node, since node children are held in a dict using the key attribute as dictionary keys (hence the hashable requirement).

Connections with nodes up and down the tree are accessed through the parent and children properties (children offers an iterator for the child nodes, sorted by node key; set the class attribute sort_children = False in a subclass to iterate in insertion order instead, which is faster and allows keys of mixed, non-comparable types). The root node of a tree is the node with parent equal to None. Setting the parent of a node checks that no loops or duplicate keys are inserted and adds the current node to the parent’s children (if needed). All checks run before anything is changed, so a failed assignment leaves the tree untouched. Deleting a parent sets it to None and removes the node from the parent’s children.

Some additional convenience properties are defined - siblings, ancestors, path, key_path, root, depth, height, size - as well as methods for adding and removing child nodes (add_child, remove_child, remove_children, detach), walking down by keys (get_descendant(*keys)) and checking relationships (is_ancestor_of, is_descendant_of).

FlexibleNode

This is a convenience class that adds hooks to setting and deleting a parent. The delete hooks fire whenever a node is detached from its parent (del node.parent, detach(), remove_child(), remove_children()). The motivation for providing it is that currently Python does not offer a simple syntax for calling a base class property from a derived class when that property is overriden, so by inheriting from FlexibleNode instead of SimpleNode one only has to override the hooks to perform any desired operations. Adding and deleting children can be directly overriden, since they are not implemented as properties. The design decision of having the hook functionality implemented in a separate class allows one to choose the trade-off between speed and flexibility of tree building (important for very large trees).

Iterators

The module implements preorder, postorder, level and leaf iterators. Each comes in two flavors - a simple one, iterating through all nodes, and a filtered one, where specific nodes can be selected and/or specific subtrees can be ignored. All walks are iterative, so arbitrarily deep trees do not hit Python’s recursion limit.

Utility functions count_nodes(node) and lowest_common_ancestor(a, b) are also provided.

Search functionality

The simplest search is done using a preorder iteration procedure that yields nodes with the specified key. A separate find function returns the first node matching that key. Search results are never cached, so they always reflect the current state of the tree.

A common use case when building trees is that subsequent nodes are added in a subtree containing the last inserted node. To optimize for this case, a separate search procedure is implemented, which walks up the tree from the start node and searches for the specified key in the subtree rooted in each ancestor.

Metadata

Release files for simpletree3 3.0.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 simpletree3 3.0.0
File Size Uploaded Provenance Actions
simpletree3-3.0.0.tar.gz 20.7 kB Without provenance Details for simpletree3-3.0.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for simpletree3 3.0.0
File Interpreter ABI Platform Actions
simpletree3-3.0.0-py3-none-any.whl Python 3 none any Details for simpletree3-3.0.0-py3-none-any.whl

Total release size: 34.3 kB

Release files / simpletree3-3.0.0.tar.gz

Download URL simpletree3-3.0.0.tar.gz
Size 20.7 kB
Tags Source
SHA-256 checksum
How to use checksums
0844590895c954f0eef2e426458e96e00f99b9b8a65d6f95e155abec14b0c110
BLAKE2b-256 checksum
How to use checksums
1888c2f8e46419ce9dda669b823001d23d2abec0974ff9e1dfc4d6af359f6991
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.3

Release files / simpletree3-3.0.0-py3-none-any.whl

Download URL simpletree3-3.0.0-py3-none-any.whl
Size 13.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f195b88f1d4835515eb2097d071daba49a4c32ef6e89bc00a3c13c600ab5023b
BLAKE2b-256 checksum
How to use checksums
4a07221a04b1e01378f64d5d33fa3e5e69a91f92dc7913714b6e1ab8fcbe6a13
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.3

Security for simpletree3 3.0.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.

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