Trees and path finding in Fortran
Question: I am attempting to replicate some Python code in Fortran 90 to make it work
within a larger Fortran project I am contributing to. Specifically, I am
trying to convert some code that recursively identifies upstream paths in a
binary tree, such as in the following example:

    
    
         4 -- 5 -- 8
        / 
       2 --- 6 - 9 -- 10
      /           \ 
     1              -- 11
      \
       3 ----7
    

This tree is represented and traversed by:

    
    
    class Node(object):
    
        def __init__(self):
            self.name = None
            self.parent = None
            self.children = set()
            self._upstream = set()
    
        def __repr__(self):
            return "Node({})".format(self.name)
    
        # Recursively search upstream in the drainage network, returns a set of all paths
        @property
        def upstream_paths(self):
            if not self._paths:
                for child in self.children:
                    if child.upstream_paths:
                        self._paths.extend([child] + path for path in child.upstream_paths)
                    else:
                        self._paths.append([child])
            return self._paths
    
    from collections import defaultdict
    
    edges = {(11, 9), (10, 9), (9, 6), (6, 2), (8, 5), (5, 4), (4, 2), (2, 1), (3, 1), (7, 3)}
    nodes = collections.defaultdict(lambda: Node())
    
    for node, parent in edges:
        nodes[node].name = node
        nodes[parent].name = parent
        nodes[node].parent = nodes[parent]
        nodes[parent].children.add(nodes[node])
    

Is it possible to implement anything like this in Fortran 90? I have a decent
understanding of recursion in f90 but without the object-orientedness of
Python, I can't imagine how this can be done.

EDIT:

For further description:

What I intend to do is identify upstream drainage paths in a dendritic stream
network. For any given outlet (root) there may be hundreds or thousands
upstream paths. There would be no modification of the network required once it
is initialized, although there will be calls to many different nodes within
the network (in the above example, a call will be made for all upstream paths
from 1, from 6, from 5, etc.) I've been looking into using pointers but can't
seem to find any examples out there of this kind of path-finding.


Answer: I find that it's much easier to convert Python towards another language while
you're still in Python (and then you can test in Python every step of the
way). Python is so flexible that it is much easier to make Python that looks
like F90 (or almost anything else) than the other way around.

I used to do this with assembly language. I'd modify my python to make it look
more like assembler, then try to code it in assembler, realize I'd missed
something, then modify the Python again. By the time I finished, I had Python
and assembly language that were easy to read and had one-to-one
correspondence, and _the Python had been tested_ every step of the way. The
assembly language just worked(TM) when I ran it.

FWIW, if removing the recursion is something you are considering,
[here](http://blog.moertel.com/posts/2013-05-11-recursive-to-iterative.html)
is an excellent guide on the best way to do exactly that.

