Skip to main content

DTree for Python

Python implementation of DTree, the incremental encoding of the topology of directed trees by two projections Q1 and Q2 (see the API reference and the paper). No dependencies; Python 3.8+.

pip install dtree-encoding

The package is imported as dtree.

from dtree import DTree

t = DTree("P1")
t.insert("A", "B")
t.insert("B", "C")

t.can_reach("A", "C")    # True
t.path_to_root("C")      # ['C', 'B', 'A']
t.q1, t.q2               # (['A', 'B', 'C'], ['A', 'B', 'C'])

t.insert("A", "D")
t.cut_n_paste("C", "D")  # move subtree C under D
t.delete("B")            # B's children (none left) move up to A
t.delete_subtree("D")    # removes D and C

Load one of the example files (parent: child1; child2 per line):

t = DTree.from_file("Test.csx")   # e.g. examples/Test.csx from the repository

API (C# name → Python name)

C# (docs) Python
DTree(id) DTree(tree_id="P1")
Insert(parent, child) insert(parent, child)
Contains(node) contains(node) / node in t
GetParent(node) get_parent(node) (None for the root)
CutNPaste(u, p) cut_n_paste(subtree_root, new_parent)
Delete(node) delete(node)—children move up to the parent
— delete_subtree(node)—removes the node and all descendants
Q1, Q2 Q1, Q2 (the DList projections, for list-level checks); q1, q2 (plain lists, without the [ ] markers)
Source, Sink source, sink
CanReach, CanNotReach can_reach, can_not_reach
IsRoot, IsLeaf is_root, is_leaf
IsAncestor, IsDescendant is_ancestor, is_descendant
SameSubtree, SubtreeSize same_subtree, subtree_size (plus subtree)
Depth depth (root = 0)
PathToRoot, RootToPath path_to_root, root_to_path (plus path(u, v))
GetNode, TryGetNode, DNode get_node, try_get_node, DNode (node handles; insert returns one)
SubtreeLast subtree_last (last node of a subtree in Q1)
VMin, VMax, UMin, UMax (of nodes) vmin, vmax, umin, umax
ReachesAll, MayReachSome, ReachesSome reaches_all, may_reach_some, reaches_some
ReachedByAll, MayBeReachedBySome, ReachedBySome reached_by_all, may_be_reached_by_some, reached_by_some

Semantics

  • Every method accepts a node name or a node handle (DNode). insert returns the handle of the new node, get_node(name) gives the handle of an existing one. A handle refers directly to the node's positions in Q1 and Q2, so can_reach on handles is two position comparisons without any lookup by name. Handles of deleted nodes, or of another tree, are rejected (KeyError) or reported as unreachable.

  • can_reach(u, v) is irreflexive: no node reaches itself, so can_reach(u, u) is False. (The C# prototype is reflexive.)

  • is_ancestor(u, v) equals can_reach(u, v) and is likewise proper.

  • delete(v) re-attaches v's children to v's parent, in v's place among its siblings.

  • delete_subtree(v) removes v with all its descendants and returns them in Q1 order.

  • cut_n_paste(u, p) moves the subtree rooted at u under p as p's last child; p must not lie inside u's subtree.

  • And/or queries on the topology: the constructed nodes vmin(S), vmax(S) (S as target) and umin(S), umax(S) (S as source) take the minimum or maximum of each index over S, separately in Q1 and Q2. reaches_all and reached_by_all are exact O(1) tests; may_reach_some and may_be_reached_by_some are only filters, and reaches_some and reached_by_some are exact in O(log k). A constructed node stays valid under insertions and is recomputed automatically after delete, delete_subtree or cut_n_paste. See and/or queries on the topology.

  • Projection positions are fractions: a new node gets the mediant (a+c)/(b+d) of its neighbors, so normally no other node moves. Only rare alternating insertion patterns trigger a local renumbering. Tree depth and width are unbounded. t.Q1.index(v) returns a position as a Fraction.

Tests

python -m unittest discover -s tests

Every operation is checked against a brute-force parent/children model on random trees, including random sequences of deletes and moves.

Benchmarks

python benchmarks/bench.py            # trees of 10,000 and 100,000 nodes
python benchmarks/bench.py --quick    # a few seconds
python benchmarks/bench.py --dynamic  # only insertions and queries alternating
python benchmarks/bench.py 1000000    # custom sizes

The script prints Markdown tables: build time and renumbering counts per tree shape (random, chain, star at the root, star at an inner node, alternating worst case), list growth at either end, and timings for can_reach, depth, delete, cut_n_paste and delete_subtree, and comparisons with a classical parent walk: reachability by tree depth, and/or queries with constructed nodes, and dynamic updates (insertions and queries alternating). Each DList keeps the counters renumber_count and renumber_moved, so the same figures are available in your own code via t.Q1 and t.Q2.

Provenance

Design and API: Enis Olgac. This Python port was written with Claude (Anthropic) from the C# original and the API reference.

Metadata

Release files for dtree-encoding 1.6.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 dtree-encoding 1.6.0
File Size Uploaded Provenance Actions
dtree_encoding-1.6.0.tar.gz 21.8 kB With provenance Details for dtree_encoding-1.6.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for dtree-encoding 1.6.0
File Interpreter ABI Platform Actions
dtree_encoding-1.6.0-py3-none-any.whl Python 3 none any Details for dtree_encoding-1.6.0-py3-none-any.whl

Total release size: 36.2 kB

Release files / dtree_encoding-1.6.0.tar.gz

Download URL dtree_encoding-1.6.0.tar.gz
Size 21.8 kB
Tags Source
SHA-256 checksum
How to use checksums
42857ba7e4691635c425c10a400b78174614692dd5ec408b42183bf5b440ac71
BLAKE2b-256 checksum
How to use checksums
05eecb86da528b4a21a6cdc261429d4807b43af5707f1ec395616eaaf7b3e47e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 17, 2026.

Transparency log

Release files / dtree_encoding-1.6.0-py3-none-any.whl

Download URL dtree_encoding-1.6.0-py3-none-any.whl
Size 14.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3c2e28191878e7d9a3cdc05dcc8c17a7e0b3701eb78e7ba52dec44bf174179c9
BLAKE2b-256 checksum
How to use checksums
28232141fab4585ad3834155b51f6fce1988df5100c5a06a441f0db26076244e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 17, 2026.

Transparency log

Security for dtree-encoding 1.6.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.

Full provenance

All 2 files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for dtree_encoding-1.6.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 17, 2026.

Transparency log

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