tree.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. # copyright 2003-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
  2. # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
  3. #
  4. # This file is part of logilab-common.
  5. #
  6. # logilab-common is free software: you can redistribute it and/or modify it under
  7. # the terms of the GNU Lesser General Public License as published by the Free
  8. # Software Foundation, either version 2.1 of the License, or (at your option) any
  9. # later version.
  10. #
  11. # logilab-common is distributed in the hope that it will be useful, but WITHOUT
  12. # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  13. # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
  14. # details.
  15. #
  16. # You should have received a copy of the GNU Lesser General Public License along
  17. # with logilab-common. If not, see <http://www.gnu.org/licenses/>.
  18. """Base class to represent a tree structure.
  19. """
  20. __docformat__ = "restructuredtext en"
  21. import sys
  22. from logilab.common import flatten
  23. from logilab.common.visitor import VisitedMixIn, FilteredIterator, no_filter
  24. ## Exceptions #################################################################
  25. class NodeNotFound(Exception):
  26. """raised when a node has not been found"""
  27. EX_SIBLING_NOT_FOUND = "No such sibling as '%s'"
  28. EX_CHILD_NOT_FOUND = "No such child as '%s'"
  29. EX_NODE_NOT_FOUND = "No such node as '%s'"
  30. # Base node ###################################################################
  31. class Node(object):
  32. """a basic tree node, characterized by an id"""
  33. def __init__(self, nid=None) :
  34. self.id = nid
  35. # navigation
  36. self.parent = None
  37. self.children = []
  38. def __iter__(self):
  39. return iter(self.children)
  40. def __str__(self, indent=0):
  41. s = ['%s%s %s' % (' '*indent, self.__class__.__name__, self.id)]
  42. indent += 2
  43. for child in self.children:
  44. try:
  45. s.append(child.__str__(indent))
  46. except TypeError:
  47. s.append(child.__str__())
  48. return '\n'.join(s)
  49. def is_leaf(self):
  50. return not self.children
  51. def append(self, child):
  52. """add a node to children"""
  53. self.children.append(child)
  54. child.parent = self
  55. def remove(self, child):
  56. """remove a child node"""
  57. self.children.remove(child)
  58. child.parent = None
  59. def insert(self, index, child):
  60. """insert a child node"""
  61. self.children.insert(index, child)
  62. child.parent = self
  63. def replace(self, old_child, new_child):
  64. """replace a child node with another"""
  65. i = self.children.index(old_child)
  66. self.children.pop(i)
  67. self.children.insert(i, new_child)
  68. new_child.parent = self
  69. def get_sibling(self, nid):
  70. """return the sibling node that has given id"""
  71. try:
  72. return self.parent.get_child_by_id(nid)
  73. except NodeNotFound :
  74. raise NodeNotFound(EX_SIBLING_NOT_FOUND % nid)
  75. def next_sibling(self):
  76. """
  77. return the next sibling for this node if any
  78. """
  79. parent = self.parent
  80. if parent is None:
  81. # root node has no sibling
  82. return None
  83. index = parent.children.index(self)
  84. try:
  85. return parent.children[index+1]
  86. except IndexError:
  87. return None
  88. def previous_sibling(self):
  89. """
  90. return the previous sibling for this node if any
  91. """
  92. parent = self.parent
  93. if parent is None:
  94. # root node has no sibling
  95. return None
  96. index = parent.children.index(self)
  97. if index > 0:
  98. return parent.children[index-1]
  99. return None
  100. def get_node_by_id(self, nid):
  101. """
  102. return node in whole hierarchy that has given id
  103. """
  104. root = self.root()
  105. try:
  106. return root.get_child_by_id(nid, 1)
  107. except NodeNotFound :
  108. raise NodeNotFound(EX_NODE_NOT_FOUND % nid)
  109. def get_child_by_id(self, nid, recurse=None):
  110. """
  111. return child of given id
  112. """
  113. if self.id == nid:
  114. return self
  115. for c in self.children :
  116. if recurse:
  117. try:
  118. return c.get_child_by_id(nid, 1)
  119. except NodeNotFound :
  120. continue
  121. if c.id == nid :
  122. return c
  123. raise NodeNotFound(EX_CHILD_NOT_FOUND % nid)
  124. def get_child_by_path(self, path):
  125. """
  126. return child of given path (path is a list of ids)
  127. """
  128. if len(path) > 0 and path[0] == self.id:
  129. if len(path) == 1 :
  130. return self
  131. else :
  132. for c in self.children :
  133. try:
  134. return c.get_child_by_path(path[1:])
  135. except NodeNotFound :
  136. pass
  137. raise NodeNotFound(EX_CHILD_NOT_FOUND % path)
  138. def depth(self):
  139. """
  140. return depth of this node in the tree
  141. """
  142. if self.parent is not None:
  143. return 1 + self.parent.depth()
  144. else :
  145. return 0
  146. def depth_down(self):
  147. """
  148. return depth of the tree from this node
  149. """
  150. if self.children:
  151. return 1 + max([c.depth_down() for c in self.children])
  152. return 1
  153. def width(self):
  154. """
  155. return the width of the tree from this node
  156. """
  157. return len(self.leaves())
  158. def root(self):
  159. """
  160. return the root node of the tree
  161. """
  162. if self.parent is not None:
  163. return self.parent.root()
  164. return self
  165. def leaves(self):
  166. """
  167. return a list with all the leaves nodes descendant from this node
  168. """
  169. leaves = []
  170. if self.children:
  171. for child in self.children:
  172. leaves += child.leaves()
  173. return leaves
  174. else:
  175. return [self]
  176. def flatten(self, _list=None):
  177. """
  178. return a list with all the nodes descendant from this node
  179. """
  180. if _list is None:
  181. _list = []
  182. _list.append(self)
  183. for c in self.children:
  184. c.flatten(_list)
  185. return _list
  186. def lineage(self):
  187. """
  188. return list of parents up to root node
  189. """
  190. lst = [self]
  191. if self.parent is not None:
  192. lst.extend(self.parent.lineage())
  193. return lst
  194. class VNode(Node, VisitedMixIn):
  195. """a visitable node
  196. """
  197. pass
  198. class BinaryNode(VNode):
  199. """a binary node (i.e. only two children
  200. """
  201. def __init__(self, lhs=None, rhs=None) :
  202. VNode.__init__(self)
  203. if lhs is not None or rhs is not None:
  204. assert lhs and rhs
  205. self.append(lhs)
  206. self.append(rhs)
  207. def remove(self, child):
  208. """remove the child and replace this node with the other child
  209. """
  210. self.children.remove(child)
  211. self.parent.replace(self, self.children[0])
  212. def get_parts(self):
  213. """
  214. return the left hand side and the right hand side of this node
  215. """
  216. return self.children[0], self.children[1]
  217. if sys.version_info[0:2] >= (2, 2):
  218. list_class = list
  219. else:
  220. from UserList import UserList
  221. list_class = UserList
  222. class ListNode(VNode, list_class):
  223. """Used to manipulate Nodes as Lists
  224. """
  225. def __init__(self):
  226. list_class.__init__(self)
  227. VNode.__init__(self)
  228. self.children = self
  229. def __str__(self, indent=0):
  230. return '%s%s %s' % (indent*' ', self.__class__.__name__,
  231. ', '.join([str(v) for v in self]))
  232. def append(self, child):
  233. """add a node to children"""
  234. list_class.append(self, child)
  235. child.parent = self
  236. def insert(self, index, child):
  237. """add a node to children"""
  238. list_class.insert(self, index, child)
  239. child.parent = self
  240. def remove(self, child):
  241. """add a node to children"""
  242. list_class.remove(self, child)
  243. child.parent = None
  244. def pop(self, index):
  245. """add a node to children"""
  246. child = list_class.pop(self, index)
  247. child.parent = None
  248. def __iter__(self):
  249. return list_class.__iter__(self)
  250. # construct list from tree ####################################################
  251. def post_order_list(node, filter_func=no_filter):
  252. """
  253. create a list with tree nodes for which the <filter> function returned true
  254. in a post order fashion
  255. """
  256. l, stack = [], []
  257. poped, index = 0, 0
  258. while node:
  259. if filter_func(node):
  260. if node.children and not poped:
  261. stack.append((node, index))
  262. index = 0
  263. node = node.children[0]
  264. else:
  265. l.append(node)
  266. index += 1
  267. try:
  268. node = stack[-1][0].children[index]
  269. except IndexError:
  270. node = None
  271. else:
  272. node = None
  273. poped = 0
  274. if node is None and stack:
  275. node, index = stack.pop()
  276. poped = 1
  277. return l
  278. def pre_order_list(node, filter_func=no_filter):
  279. """
  280. create a list with tree nodes for which the <filter> function returned true
  281. in a pre order fashion
  282. """
  283. l, stack = [], []
  284. poped, index = 0, 0
  285. while node:
  286. if filter_func(node):
  287. if not poped:
  288. l.append(node)
  289. if node.children and not poped:
  290. stack.append((node, index))
  291. index = 0
  292. node = node.children[0]
  293. else:
  294. index += 1
  295. try:
  296. node = stack[-1][0].children[index]
  297. except IndexError:
  298. node = None
  299. else:
  300. node = None
  301. poped = 0
  302. if node is None and len(stack) > 1:
  303. node, index = stack.pop()
  304. poped = 1
  305. return l
  306. class PostfixedDepthFirstIterator(FilteredIterator):
  307. """a postfixed depth first iterator, designed to be used with visitors
  308. """
  309. def __init__(self, node, filter_func=None):
  310. FilteredIterator.__init__(self, node, post_order_list, filter_func)
  311. class PrefixedDepthFirstIterator(FilteredIterator):
  312. """a prefixed depth first iterator, designed to be used with visitors
  313. """
  314. def __init__(self, node, filter_func=None):
  315. FilteredIterator.__init__(self, node, pre_order_list, filter_func)