JBN
JBN

Reputation: 87

Recursively Adding Nodes to zss Tree

I want to compare tree distances using the zss module. For that purpose, I am trying to create trees by adding nodes recursively. Unfortunately, the documentation only provides examples in which trees are created with all nodes at once and I can't wrap my head around how to navigate to a child in order to add another node after the tree object was created.

Here is a simple example:

from zss import Node
A = (
    Node("f")
        .addkid(Node("a")
            .addkid(Node("h"))
            .addkid(Node("c")))
        .addkid(Node("e"))
    )

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

All I am trying to do now is to add child "l" to node "c" in tree A, such that A == B. Any help is appreciated.

Upvotes: 1

Views: 106

Answers (1)

Dan D.
Dan D.

Reputation: 74655

After looking at zss, I see that this might work with the simple_tree that it uses:

A.get('c').addkid(Node('l'))

It seems to use labels that are likely should be unique. So get actually returns the first node with that label found during a depth first search.

A.get('f').get('a').get('c').addkid(Node('l'))

Upvotes: 1

Related Questions