Skip to main content

Offers classes implementing basic tree-like functionality, as well as iterators for walking the trees and node search functionality. Requires Python3.4 or later. Cython compilation is performed if cython is installed.

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). The root node of a tree is the node with parent equal to None. Setting the parent of a node checks that no loops are inserted and adds the current node to the parent’s children (if needed). Deleting a parent sets it to None and removes the node from the parent’s children.

Some additional convenience properties are defined - siblings, ancestors, depth, height - as well as methods for adding and removing child nodes.

FlexibleNode

This is a convenience class that add hooks to setting and deleting a parent. 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.

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.

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.

Release files for simpletree3 2.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 2.0.0
File Size Uploaded
simpletree3-2.0.0.tar.gz 21.1 kB Details

Built distribution (wheel)

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

Total release size: 37.1 kB

Release files / simpletree3-2.0.0.tar.gz

Download URL simpletree3-2.0.0.tar.gz
Size 21.1 kB
Tags Source
SHA-256 checksum
How to use checksums
ab1431fa2394ce1bbdedb2387869ea72e50eb2761387f4d4920ff0293e1100d7
BLAKE2b-256 checksum
How to use checksums
14c759110733b4f94e42f456ced13de0708fabe989622e22e0374c91d5201e46
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.10.8 {"installer":{"name":"uv","version":"0.10.8","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

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

Download URL simpletree3-2.0.0-py3-none-any.whl
Size 15.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7e09440a54cbd0d28732edaa9717cf73bfaa02fdb253604fbc55bc09f64adfe6
BLAKE2b-256 checksum
How to use checksums
532cec9357e4aa3ce7ef00a5736736ee15437177a990200b004c209d691a7eeb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.10.8 {"installer":{"name":"uv","version":"0.10.8","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release history Release notifications | RSS feed

This release

2.0.0 This release

2 release files

1.1.0

2 release files

1.0.10

1 release file

1.0.9

1 release file

1.0.8

1 release file

1.0.7

1 release file

1.0.6

2 release files

1.0.5

2 release files

1.0.4

1 release file

1.0.0

1 release file

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