'Core function to output dot which sorts out parents and children
and do it in the right order'

def tree(self, level, eid, children):

    e = self.getEdge(eid)
    if debug:
        print ('/*    Grabed    edge    =    %s*/' % str(e))
    if (e['lock'] == 1):
        return
    props = e['properties']
    e['lock'] = 1
    if children:
        self.fd.write((level * self.padding_str))
        self.fd.write((self.padding_str + ('subgraph    cluster%d    {\n' % eid)))
        properties = self.propertiesAsStringGet(eid, props)
        self.fd.write((level * self.padding_str))
        self.fd.write((self.padding_str + ('%s' % properties)))
        self.__opened_braces.append([eid, level])
    else:
        properties = self.propertiesAsStringGet(eid, props)
        if self.__opened_braces:
            (last_cluster, last_level) = self.__opened_braces[(-1)]
        else:
            last_cluster = 0
            last_level = 0
        if debug:
            print ('/*    e[parent]    =    %s,    last_cluster    =    %d,    last_level    =    %d,    opened_braces:    %s    */' % (str(e['parent']), last_cluster, last_level, str(self.__opened_braces)))
        if e['parent']:
            if (e['parent'] != last_cluster):
                while (e['parent'] < last_cluster):
                    (last_cluster, last_level) = self.__opened_braces[(-1)]
                    if (e['parent'] == last_cluster):
                        last_level += 1
                        self.fd.write((last_level * self.padding_str))
                        self.fd.write((self.padding_str + ('edge%d    %s;\n' % (eid, properties))))
                    else:
                        self.fd.write((last_level * self.padding_str))
                        self.fd.write((self.padding_str + '}\n'))
                        self.__opened_braces.pop()
            else:
                self.fd.write((level * self.padding_str))
                self.fd.write((self.padding_str + ('edge%d    %s;\n' % (eid, properties))))
                cl = self.collectUnlockedLeaves(e['parent'])
                for leaf in cl:
                    l = self.getEdge(leaf)
                    props = l['properties']
                    properties = self.propertiesAsStringGet(leaf, props)
                    self.fd.write((last_level * self.padding_str))
                    self.fd.write(((self.padding_str + self.padding_str) + ('edge%d    %s;\n' % (leaf, properties))))
                    self.lockEdge(leaf)
                self.fd.write(((level * self.padding_str) + '}\n'))
                self.__opened_braces.pop()
        else:
            self.fd.write((self.padding_str + ('edge%d    %s;\n' % (eid, properties))))
