Giter VIP home page Giter VIP logo

Comments (3)

yuvalshi0 avatar yuvalshi0 commented on July 3, 2024

I used the node predecessor to iterate over the tree nodes, starting from the leaf to the root:

node = tree.get_node(node_id)
    while node.predecessor:
        if your_condition:
            return node.identifier
        node = tree[node.predecessor]

from treelib.

jmount1992 avatar jmount1992 commented on July 3, 2024

An alternative answer to @yuvalshi0 is:

  1. Get the depth of the two nodes
  2. Start at the deeper of the two nodes, and keep going up until at the same depth as the other node
  3. When at the same depth, if the nodes are not the same, then continue moving both up one parent and check if they are the same node

Execution time will depend on the depth of desired nodes. There are better alternatives out there. Search lower common ancestor graph as a start.

Example function

def get_common_frame(tree, node_1: str, node_2: str) -> str:
        """gets the lowest common ancester between two nodes.

        Args:
            tree: treelib tree object
            node_1 (str): the ID of the first node (first and second has no meaning)
            node_2 (str): the ID of the second node

        Returns:
            str: the identifier of the lowest common ancestor node
        """
        
        # check to see which node is deeper
        if tree.depth(node_1) > tree.depth(node_1):
            # node 1 is further down tree
            lower_node = node_1
            higher_node = node_2
        else:
            # node 2 is further down tree
            lower_node = node_2
            higher_node = node_1

        # go until lower node has same depth as higher node
        while tree.depth(lower_node) != tree.depth(higher_node):
            lower_node = tree.parent(lower_node).identifier

        # move both up one level until lower node equals higher node
        while lower_node != higher_node:
            lower_node = tree.parent(lower_node).identifier
            higher_node = tree.parent(higher_node).identifier

        # return
        return lower_node

Edited: To improve clarity

from treelib.

afrooze avatar afrooze commented on July 3, 2024

thanks for answers

from treelib.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.