Posted By

vinaykr41 on 11/01/17


Tagged

python tree


Versions (?)

Animal Tree


 / Published in: Python
 

URL: animaltree

A interactive animal tree

  1. class Tree:
  2. def __init__(self, cargo, left=None, right=None):
  3. self.cargo = cargo
  4. self.left = left
  5. self.right = right
  6.  
  7. def __str__(self):
  8. return str(self.cargo)
  9.  
  10. def preorder(self):
  11. if self == None: return
  12. print(self.cargo)
  13. if not self.left == None:
  14. self.left.preorder()
  15. if not self.right == None:
  16. self.right.preorder()
  17.  
  18. def inorder(self):
  19. if self == None: return
  20. if not self.left == None:
  21. self.left.inorder()
  22. print(self.cargo)
  23. if not self.right == None:
  24. self.right.inorder()
  25.  
  26. def postorder(self):
  27. if self == None: return
  28. if not self.left == None:
  29. self.left.postorder()
  30. if not self.right == None:
  31. self.right.postorder()
  32. print(self.cargo)
  33.  
  34. def depth(self):
  35. if tree == None: return 0
  36. left_depth = 0
  37. right_depth = 0
  38. if not self.left == None:
  39. left_depth = self.left.depth()
  40. if not self.right == None:
  41. right_depth = self.right.depth()
  42. return max(left_depth,right_depth) + 1
  43.  
  44. def printTree(self,level=0):
  45. if self == None: return
  46. print(" " * (level) + str(level+1) + ". " + self.cargo.__str__())
  47. if not self.left == None:
  48. self.left.printTree(level+1)
  49. if not self.right == None:
  50. self.right.printTree(level+1)
  51.  
  52. def yes(ques):
  53. ans = input(ques).lower()
  54. return ans[0] == 'y'
  55.  
  56. def animal():
  57. # start with a singleton
  58. root = Tree("pigeon")
  59.  
  60. # loop until the user quits
  61. while True:
  62. prompt = "Are you thinking of an animal? "
  63.  
  64. response = yes(prompt)
  65. if not response:
  66. # print("---------PreOrder-------------")
  67. # root.preorder()
  68. # print("---------InOrder-------------")
  69. # root.inorder()
  70. # print("---------PostOrder-------------")
  71. # root.postorder()
  72. print("----------Tree----------")
  73. root.printTree()
  74. break
  75. # walk the tree
  76. tree = root
  77. while tree.right != None:
  78. prompt = tree.cargo + "? "
  79. if yes(prompt):
  80. tree = tree.left
  81. else:
  82. tree = tree.right
  83.  
  84. # make a guess
  85. guess = tree.cargo
  86. prompt = "Is it a " + guess + "? "
  87. if yes(prompt):
  88. print ("I rule!")
  89. continue
  90.  
  91. # get new information
  92. prompt = "What is the animal's name? "
  93. animal = input(prompt)
  94. prompt = "What question would distinguish a %s from a %s? "
  95. question = input(prompt % (animal, guess))
  96.  
  97. # add new information to the tree
  98. tree.cargo = question
  99. prompt = "If the animal were %s the answer would be? "
  100. if yes(prompt % animal):
  101. tree.right = Tree(guess)
  102. tree.left = Tree(animal)
  103. else:
  104. tree.right = Tree(animal)
  105. tree.left = Tree(guess)
  106.  
  107. animal()
  108.  
  109.  
  110. '''
  111. Sample Input:
  112.  
  113. y
  114. n
  115. dog
  116. can it fly
  117. n
  118. y
  119. n
  120. n
  121. tiger
  122. is it wild
  123. y
  124. y
  125. y
  126. n
  127. eagle
  128. is it wild
  129. y
  130. y
  131. n
  132. y
  133. n
  134. lion
  135. does it have strips
  136. n
  137. y
  138. y
  139. y
  140. n
  141. dragon
  142. does it breathe fire
  143. y
  144. y
  145. y
  146. n
  147. n
  148. crow
  149. will it be in a cage
  150. n
  151. y
  152. n
  153. n
  154. n
  155. cat
  156. will it bark
  157. n
  158. n
  159.  
  160. Sample Output:
  161.  
  162. 1. can it fly
  163. 2. is it wild
  164. 3. does it breathe fire
  165. 4. dragon
  166. 4. eagle
  167. 3. will it be in a cage
  168. 4. pigeon
  169. 4. crow
  170. 2. is it wild
  171. 3. does it have strips
  172. 4. tiger
  173. 4. lion
  174. 3. will it bark
  175. 4. dog
  176. 4. cat
  177.  
  178. '''

Report this snippet  

You need to login to post a comment.