Guest User

lab05 solutions

a guest
Mar 16th, 2015
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.92 KB | None | 0 0
  1. from csc148_queue import Queue
  2.  
  3. class Tree:
  4. ''' Represent a Bare-bones Tree ADT'''
  5.  
  6. def __init__(self, value=None, children=None):
  7. ''' (Tree, object, list-of-Tree) -> NoneType
  8.  
  9. Create Tree(self) with root containing value and
  10. 0 or more children Trees.
  11. '''
  12. self.value = value
  13. # copy children if not None
  14. self.children = children.copy() if children else []
  15. # the functional if on the line above is equivalent to:
  16. #if not children:
  17. #self.children = []
  18. #else:
  19. #self.children = children.copy()
  20.  
  21. def __repr__(self):
  22. ''' (Tree) -> str
  23.  
  24. Return representation of Tree (self) as string that
  25. can be evaluated into an equivalent Tree.
  26.  
  27. >>> t1 = Tree(5)
  28. >>> t1
  29. Tree(5)
  30. >>> t2 = Tree(7, [t1])
  31. >>> t2
  32. Tree(7, [Tree(5)])
  33. '''
  34. # Our __repr__ is recursive, because it can also be called via repr...!
  35. return ('Tree({}, {})'.format(repr(self.value), repr(self.children))
  36. if self.children
  37. else 'Tree({})'.format(repr(self.value)))
  38.  
  39. def __eq__(self, other):
  40. ''' (Tree, object) -> bool
  41.  
  42. Return whether this Tree is equivalent to other.
  43.  
  44.  
  45. >>> t1 = Tree(5)
  46. >>> t2 = Tree(5, [])
  47. >>> t1 == t2
  48. True
  49. >>> t3 = Tree(5, [t1])
  50. >>> t2 == t3
  51. False
  52. '''
  53. return (isinstance(other, Tree) and
  54. self.value == other.value and
  55. self.children == other.children)
  56.  
  57. # omit __str__ for now..
  58.  
  59. ##################################################################################
  60. # Here are some module-level functions. Alternatively these could be implemented
  61. # as methods, but then they might not be appropriate for every Tree
  62.  
  63.  
  64. def list_all(t):
  65. ''' (Tree) -> list
  66.  
  67. Return a list of values in t.
  68.  
  69. >>> t = Tree(0)
  70. >>> list_all(t)
  71. [0]
  72. >>> t = descendents_from_list(Tree(0), [1, 2, 3, 4, 5, 6, 7, 8], 3)
  73. >>> L = list_all(t)
  74. >>> L.sort()
  75. >>> L
  76. [0, 1, 2, 3, 4, 5, 6, 7, 8]
  77. '''
  78. if not t.children:
  79. return [t.value]
  80. else:
  81. return gather_lists([list_all(c) for c in t.children]) + [t.value]
  82.  
  83. def list_leaves(t):
  84. ''' (Tree) -> list
  85.  
  86. Return a list of values in leaves of t.
  87.  
  88. >>> t = Tree(0)
  89. >>> list_leaves(t)
  90. [0]
  91. >>> t = descendents_from_list(Tree(0), [1, 2, 3, 4, 5, 6, 7, 8], 3)
  92. >>> L = list_leaves(t)
  93. >>> L.sort() # so list is predictable to compare
  94. >>> L
  95. [3, 4, 5, 6, 7, 8]
  96. '''
  97.  
  98. if not t.children:
  99. return [t.value]
  100. else:
  101. return gather_lists([list_leaves(c) for c in t.children])
  102.  
  103. def list_interior(t):
  104. ''' (Tree) -> list
  105.  
  106. Return a list of values in interior nodes of t.
  107.  
  108. >>> t = Tree(0)
  109. >>> list_interior(t)
  110. []
  111. >>> t = descendents_from_list(Tree(0), [1, 2, 3, 4, 5, 6, 7, 8], 3)
  112. >>> L = list_interior(t)
  113. >>> L.sort()
  114. >>> L
  115. [0, 1, 2]
  116. '''
  117. if not t.children:
  118. return []
  119. else:
  120. return gather_lists([list_interior(x) for x in t.children]) + [t.value]
  121.  
  122.  
  123. def list_if(t, p):
  124. ''' (Tree, function) -> list
  125.  
  126. Return a list of values in Tree t that satisfy p(value)
  127.  
  128. >>> def p(v): return v > 4
  129. >>> t = descendents_from_list(Tree(0), [1, 2, 3, 4, 5, 6, 7, 8], 3)
  130. >>> L = list_if(t, p)
  131. >>> L.sort()
  132. >>> L
  133. [5, 6, 7, 8]
  134. >>> def p(v): return v % 2 == 0
  135. >>> L = list_if(t, p)
  136. >>> L.sort()
  137. >>> L
  138. [0, 2, 4, 6, 8]
  139. '''
  140. #if not t.children and p(t.value):
  141. #return [t.value]
  142. #else:
  143. #if p(t.value):
  144. #return gather_lists([list_if(c, p) for c in t.children] + [[t.value]])
  145. #else:
  146. #return gather_lists([list_if(c, p) for c in t.children])
  147. return ([t.value] if p(t.value) else []) + \
  148. gather_lists([list_if(c,p) for c in t.children])
  149.  
  150. def list_to_depth(t, n):
  151. ''' (Tree, int) -> list
  152.  
  153. Return a list of values in t from nodes with paths no longer
  154. than n from root
  155.  
  156. >>> t = Tree(0)
  157. >>> list_to_depth(t, 0)
  158. [0]
  159. >>> t = descendents_from_list(Tree(0), [1, 2, 3, 4, 5, 6, 7, 8], 3)
  160. >>> L = list_to_depth(t, 1)
  161. >>> L.sort()
  162. >>> L
  163. [0, 1, 2, 3]
  164. '''
  165.  
  166. if n == 0:
  167. return [t.value]
  168. else:
  169. return [t.value] + gather_lists([list_to_depth(c, n-1) for c\
  170. in t.children])
  171. #if n == 0:
  172. #return [t.value]
  173. #else:
  174. #depth = []
  175. #for c in t.children:
  176. #depth.append(list_to_depth(c,n-1))
  177. #return depth + [t.value]
  178.  
  179. def gather_lists(L):
  180. ''' (list-of-lists) -> list
  181.  
  182. Concatenate all the sublists of L and return the result.
  183.  
  184. >>> gather_lists([[1, 2], [3, 4, 5]])
  185. [1, 2, 3, 4, 5]
  186. >>> gather_lists([[6, 7], [8], [9, 10, 11]])
  187. [6, 7, 8, 9, 10, 11]
  188. '''
  189. new_list = []
  190. for l in L:
  191. new_list += l
  192. return new_list
  193.  
  194.  
  195. def descendents_from_list(t, L, arity):
  196. ''' (Tree, list, int) -> Tree
  197.  
  198. Populate t's descendents from L, filling them
  199. in in level order, with up to arity children per node.
  200. Then return t.
  201.  
  202. >>> descendents_from_list(Tree(0), [1, 2, 3, 4], 2)
  203. Tree(0, [Tree(1, [Tree(3), Tree(4)]), Tree(2)])
  204. '''
  205. q = Queue()
  206. q.enqueue(t)
  207. L = L.copy()
  208. while not q.is_empty(): # unlikely to happen
  209. new_t = q.dequeue()
  210. for i in range(0,arity):
  211. if len(L) == 0:
  212. return t # our work here is done
  213. else:
  214. new_t_child = Tree(L.pop(0))
  215. new_t.children.append(new_t_child)
  216. q.enqueue(new_t_child)
  217. return t
  218.  
  219.  
  220. if __name__ == '__main__':
  221. import doctest
  222. doctest.testmod()
Advertisement
Add Comment
Please, Sign In to add comment