示例#1
0
    def _parse_subtrees(self):
        """
            Returns the number of leaves and a list of the subtrees,
            for a given rooted tree.

            OUTPUT:
                nleaves  -- number of leaves attached directly to the root
                subtrees -- list of non-leaf subtrees attached to the root

            The method can be thought of as returning what remains if the
            root of the tree is removed.  For efficiency, instead of
            returning possibly many copies of 'T', the leaves are just
            returned as a number.
        """
        from nodepy.strmanip import get_substring, open_to_close, getint
        if str(self)=='T' or str(self)=='': return 0,[]
        pos=0
        #Count leaves at current level
        if self[1]=='T':
            if self[2]=='^':
                nleaves=getint(self[3:])
            else: nleaves=1
        else: nleaves=0

        subtrees=[]
        while pos!=-1:
            pos=self.find('{',pos+1)
            if pos!=-1:
                subtrees.append(RootedTree(get_substring(self,pos)))
                pos=open_to_close(self,pos)

        return nleaves,subtrees
示例#2
0
    def _parse_subtrees(self):
        """
            Returns the number of leaves and a list of the subtrees,
            for a given rooted tree.

            OUTPUT:
                nleaves  -- number of leaves attached directly to the root
                subtrees -- list of non-leaf subtrees attached to the root

            The method can be thought of as returning what remains if the
            root of the tree is removed.  For efficiency, instead of
            returning possibly many copies of 'T', the leaves are just
            returned as a number.
        """
        from nodepy.strmanip import get_substring, open_to_close, getint
        if str(self) == 'T' or str(self) == '': return 0, []
        pos = 0
        #Count leaves at current level
        if self[1] == 'T':
            if self[2] == '^':
                nleaves = getint(self[3:])
            else:
                nleaves = 1
        else:
            nleaves = 0

        subtrees = []
        while pos != -1:
            pos = self.find('{', pos + 1)
            if pos != -1:
                subtrees.append(RootedTree(get_substring(self, pos)))
                pos = open_to_close(self, pos)

        return nleaves, subtrees