Example #1
0
    def sub_tree(self, vtx_id, copy=True):
        """Return the subtree rooted on `vtx_id`.

        The induced subtree of the tree has the vertices in the ancestors of vtx_id.

        :Parameters:
          - `vtx_id`: A vertex of the original tree.
          - `copy`:  
            If True, return a new tree holding the subtree. If False, the subtree is
            created using the original tree by deleting all vertices not in the subtree.

        :returns: A sub tree of the tree. If copy=True, a new Tree is returned. 
            Else the subtree is created inplace by modifying the original tree. 
        """
        if not copy:
            # remove all vertices not in the sub_tree
            bunch = set(pre_order(self, vtx_id))
            remove_bunch = set(self) - bunch

            for vid in remove_bunch:
                self._remove_vertex_properties(vid)

                #self.remove_vertex(vid)
                # remove parent edge
                pid = self.parent(vid)
                if pid is not None:
                    self._children[pid].remove(vid)
                    del self._parent[vid]
                # remove children edges
                for cid in self.children(vid):
                    self._parent[cid] = None
                if vid in self._children:
                    del self._children[vid]

            self.root = vtx_id
            return self
        else:
            treeid_id = {}
            tree = self.__class__()
            tree.root = 0

            for name in self.properties():
                tree.add_property(name)

            treeid_id[vtx_id] = tree.root
            tree._add_vertex_properties(tree.root,
                                        self.get_vertex_property(vtx_id))
            subtree = pre_order(self, vtx_id)
            subtree.next()
            for vid in subtree:
                pid = self.parent(vid)
                if pid is not None:
                    parent = treeid_id[pid]
                    v = tree.add_child(parent)
                    treeid_id[vid] = v

                tree._add_vertex_properties(v, self.get_vertex_property(vid))

            return tree
Example #2
0
    def sub_tree(self, vtx_id, copy=True):
        """Return the subtree rooted on `vtx_id`.

        The induced subtree of the tree has the vertices in the ancestors of vtx_id.

        :Parameters:
          - `vtx_id`: A vertex of the original tree.
          - `copy`:  
            If True, return a new tree holding the subtree. If False, the subtree is
            created using the original tree by deleting all vertices not in the subtree.

        :returns: A sub tree of the tree. If copy=True, a new Tree is returned. 
            Else the subtree is created inplace by modifying the original tree. 
        """
        if not copy:
            # remove all vertices not in the sub_tree
            bunch = set(pre_order(self, vtx_id))
            remove_bunch = set(self) - bunch

            for vid in remove_bunch:
                self._remove_vertex_properties(vid)

                #self.remove_vertex(vid)
                # remove parent edge
                pid = self.parent(vid)
                if pid is not None:
                    self._children[pid].remove(vid)
                    del self._parent[vid]
                # remove children edges
                for cid in self.children(vid):
                    self._parent[cid] = None
                if vid in self._children:
                    del self._children[vid]

            self.root = vtx_id
            return self
        else:
            treeid_id = {}
            tree = self.__class__()
            tree.root = 0

            for name in self.properties():
                tree.add_property(name)
            
            treeid_id[vtx_id] = tree.root
            tree._add_vertex_properties(tree.root, self.get_vertex_property(vtx_id))
            subtree = pre_order(self, vtx_id)
            subtree.next()
            for vid in subtree:
                pid = self.parent(vid)
                if pid is not None:
                    parent = treeid_id[pid]
                    v = tree.add_child(parent)
                    treeid_id[vid] = v

                tree._add_vertex_properties(v, self.get_vertex_property(vid))

            return tree
Example #3
0
    def add_child_tree(self, parent, tree):
        """
        Add a tree after the children of the parent vertex.
        Complexity has to be O(1) if tree == sub_tree()
        This method copies the tree and renumbers its vertices.

        Returns a map between original tree vids and the newly added vids.

        :param parent: vertex identifier
        :param tree: a rooted tree

        :returns: dict (original tree id -> new id)
        """
        treeid_id = {}
        root = tree.root
        root_id = self.add_child(parent)
        treeid_id[root]=root_id

        # pre_order traversal from root and renumbering
        for vtx_id in pre_order(tree, root):
            if vtx_id == root:
               continue 
            parent = treeid_id[tree.parent(vtx_id)]
            vid = self.add_child(parent)
            treeid_id[vtx_id] = vid

        return treeid_id
Example #4
0
    def add_child_tree(self, parent, tree):
        """
        Add a tree after the children of the parent vertex.
        Complexity has to be O(1) if tree == sub_tree()
        This method copies the tree and renumbers its vertices.

        Returns a map between original tree vids and the newly added vids.

        :param parent: vertex identifier
        :param tree: a rooted tree

        :returns: dict (original tree id -> new id)
        """
        treeid_id = {}
        root = tree.root
        root_id = self.add_child(parent)
        treeid_id[root] = root_id

        # pre_order traversal from root and renumbering
        for vtx_id in pre_order(tree, root):
            if vtx_id == root:
                continue
            parent = treeid_id[tree.parent(vtx_id)]
            vid = self.add_child(parent)
            treeid_id[vtx_id] = vid

        return treeid_id
Example #5
0
    def sub_tree(self, vtx_id, copy=True):
        """Return the subtree rooted on `vtx_id`.

        The induced subtree of the tree has the vertices in the ancestors of vtx_id.

        :Parameters:
          - `vtx_id`: A vertex of the original tree.
          - `copy`:  
            If True, return a new tree holding the subtree. If False, the subtree is
            created using the original tree by deleting all vertices not in the subtree.

        :returns: A sub tree of the tree. If copy=True, a new Tree is returned. 
            Else the subtree is created inplace by modifying the original tree. 
        """

        if not copy:
            # remove all vertices not in the sub_tree
            bunch = set(pre_order(self, vtx_id))
            for vid in self:
                if vid not in bunch:
                    self.remove_vertex(vid)

            self._root = vtx_id
            self._parent[self._root] = None
            return self
        else:
            treeid_id = {}
            tree = Tree()
            tree.root = 0
            treeid_id[vtx_id] = tree.root
            subtree = pre_order(self, vtx_id)
            
            subtree.next()
            for vid in subtree:
                parent = treeid_id[self.parent(vid)]
                v = tree.add_child(parent)
                treeid_id[vid] = v

            return tree
Example #6
0
    def sub_tree(self, vtx_id, copy=True):
        """Return the subtree rooted on `vtx_id`.

        The induced subtree of the tree has the vertices in the ancestors of vtx_id.

        :Parameters:
          - `vtx_id`: A vertex of the original tree.
          - `copy`:  
            If True, return a new tree holding the subtree. If False, the subtree is
            created using the original tree by deleting all vertices not in the subtree.

        :returns: A sub tree of the tree. If copy=True, a new Tree is returned. 
            Else the subtree is created inplace by modifying the original tree. 
        """

        if not copy:
            # remove all vertices not in the sub_tree
            bunch = set(pre_order(self, vtx_id))
            for vid in self:
                if vid not in bunch:
                    self.remove_vertex(vid)

            self._root = vtx_id
            self._parent[self._root] = None
            return self
        else:
            treeid_id = {}
            tree = Tree()
            tree.root = 0
            treeid_id[vtx_id] = tree.root
            subtree = pre_order(self, vtx_id)

            subtree.next()
            for vid in subtree:
                parent = treeid_id[self.parent(vid)]
                v = tree.add_child(parent)
                treeid_id[vid] = v

            return tree
Example #7
0
    def insert_sibling_tree(self, vid, tree ):
        """
        Insert a tree before the vid.
        vid and the root of the tree are siblings.
        Complexity have to be O(1) if tree comes from the actual tree
        ( tree= self.sub_tree() )

        :param vid: vertex identifier
        :param tree: a rooted tree
        """
        treeid_id = {}
        root = tree.root
        root_id = self.insert_sibling(vid)
        treeid_id[root]=root_id

        # pre_order traversal from root and renumbering
        for vtx_id in pre_order(tree, vid):
            parent = treeid_id[tree.parent(vtx_id)]
            v = self.add_child(parent)
            treeid_id[vtx_id] = v

        return treeid_id
Example #8
0
    def insert_sibling_tree(self, vid, tree):
        """
        Insert a tree before the vid.
        vid and the root of the tree are siblings.
        Complexity have to be O(1) if tree comes from the actual tree
        ( tree= self.sub_tree() )

        :param vid: vertex identifier
        :param tree: a rooted tree
        """
        treeid_id = {}
        root = tree.root
        root_id = self.insert_sibling(vid)
        treeid_id[root] = root_id

        # pre_order traversal from root and renumbering
        for vtx_id in pre_order(tree, vid):
            parent = treeid_id[tree.parent(vtx_id)]
            v = self.add_child(parent)
            treeid_id[vtx_id] = v

        return treeid_id