Package obitools :: Package graph :: Module tree
[hide private]
[frames] | no frames]

Source Code for Module obitools.graph.tree

 1  from obitools.graph import UndirectedGraph,Node 
 2  from obitools.graph.algorithms.component import componentCount 
 3   
 4   
5 -class Forest(UndirectedGraph):
6 7
8 - def getNode(self,node=None,index=None):
9 if index is None: 10 index = self._index.getIndex(node, True) 11 return TreeNode(index,self)
12
13 - def addEdge(self,node1=None,node2=None,index1=None,index2=None,**data):
14 index1=self.addNode(node1, index1) 15 index2=self.addNode(node2, index2) 16 17 cc = set(n.index for n in self.getNode(index=index2).componentIterator()) 18 19 assert index1 in self._node[index2] or index1 not in cc, \ 20 "No more than one path is alloed between two nodes in a tree" 21 22 UndirectedGraph.addEdge(self, index1=index1, index2=index2,**data) 23 24 return (index1,index2)
25
26 - def isASingleTree(self):
27 return componentCount(self)==1
28
29 -class TreeNode(Node):
30
31 - def componentIterator(self):
32 for c in self: 33 yield c 34 for cc in c: 35 yield cc
36