pythonalgorithmgraphtreebinary-tree

How to convert a list of edges to a tree in python?


I am having a list of edges that has the following format:

edges=[[1,4],[1,3],[1,2],[3,5],[3,6],[3,7]]

Here in each edge the first element is the parent node and the second is a child node i.e, in

[1,4]---->(1 is the parent node and 4 is child node)

I have to create a function that returns the pointer to the root of the tree. At first I tried creating a dictionary but after creating I am unable to proceed.

Please provide any ideas of how to implement this?


Solution

  • There are many ways to create a tree data structure... moreover Python does not have a pointer data type, so the root of a tree would be an object.

    Here is one way to do it:

    First define a Node class:

    class Node():
        def __init__(self, data=None):
            self.data = data
            self.children = []
    

    Then the main algorithm:

    def create_tree(edges):
        # Get all the unique keys into a set
        node_keys = set(key for keys in edges for key in keys)
        # Create a Node instance for each of them, keyed by their key in a dict:
        nodes = { key: Node(key) for key in node_keys }
        # Populate the children attributes from the edges
        for parent, child in edges:
            nodes[parent].children.append(nodes[child])
            # Remove the child from the set, so we will be left over with the root
            node_keys.remove(child)
        # Get the root from the set, which at this point should only have one member
        for root_key in node_keys:  # Just need one
            return nodes[root_key]
    

    Run it as follows:

    # Example run
    edges = [[1,4],[1,3],[1,2],[3,5],[3,6],[3,7]]
    root = create_tree(edges)
    

    If you want to quickly verify the tree's shape, add this method to the Node class:

        def __repr__(self, indent=""):
            return (indent + repr(self.data) + "\n"
                    + "".join(child.__repr__(indent+"  ") 
                              for child in self.children))
    

    And use it to print the tree:

    print(root)
    

    This output string is just a very simple way to visualise the tree. With a bit more code you can also draw the branches of the tree, but this is enough to debug the code.