Zhang-Shasha: Tree Edit Distance in Python

1.2.0 · active · verified Thu Apr 16

ZSS is a Python library that implements the Zhang-Shasha algorithm for computing the tree edit distance between two ordered labeled trees. It is currently at version 1.2.0 and, while not frequently updated, provides a stable and specialized tool for comparing tree structures. The library can be extended with custom node formats and distance metrics, and optionally leverages `editdist` and `numpy` for enhanced functionality and performance.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates how to define two simple trees using the built-in `Node` class and compute their edit distance using `simple_distance()`. The `addkid` method is used to construct the tree structure.

from zss import simple_distance, Node

A = (
    Node("f")
    .addkid(Node("a")
        .addkid(Node("h"))
        .addkid(Node("c")
            .addkid(Node("l"))
        )
    )
    .addkid(Node("e"))
)

B = (
    Node("f")
    .addkid(Node("a")
        .addkid(Node("d"))
        .addkid(Node("c")
            .addkid(Node("b"))
        )
    )
    .addkid(Node("e"))
)

distance = simple_distance(A, B)
print(f"Tree edit distance: {distance}")
# Expected: 2

view raw JSON →