Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. Good Tree
  2. 1
  3. 2
  4. 3
  5. [< 3 >]
  6. Bad Tree
  7. 1
  8. 2
  9. 2
  10. [< 2 >, < 3 >]
  11.  
  12. #!/usr/bin/python
  13. class NodeBad:
  14. def __init__(self, value, c=[]):
  15. self.value = value
  16. self.children = c
  17. def addchild(self, node):
  18. self.children.append(node)
  19. def __str__(self):
  20. return '< %s >' % self.value
  21. def __repr__(self):
  22. return '< %s >' % self.value
  23.  
  24.  
  25. class NodeGood:
  26. def __init__(self, value):
  27. self.value = value
  28. self.children = []
  29. def addchild(self, node):
  30. self.children.append(node)
  31. def __str__(self):
  32. return '< %s >' % self.value
  33. def __repr__(self):
  34. return '< %s >' % self.value
  35.  
  36. if __name__ == '__main__':
  37. print 'Good Tree'
  38. ng = NodeGood(1) # Root Node
  39. rootgood = ng
  40. ng.addchild(NodeGood(2)) # 1nd Child
  41. ng = ng.children[0]
  42. ng.addchild(NodeGood(3)) # 2nd Child
  43.  
  44. print rootgood.value
  45. print rootgood.children[0].value
  46. print rootgood.children[0].children[0].value
  47. print rootgood.children[0].children
  48.  
  49. print 'Bad Tree'
  50. nb = NodeBad(1) # Root Node
  51. rootbad = nb
  52. nb.addchild(NodeBad(2)) # 1st Child
  53. nb = nb.children[0]
  54. nb.addchild(NodeBad(3)) # 2nd Child
  55.  
  56. print rootbad.value
  57. print rootbad.children[0].value
  58. print rootbad.children[0].children[0].value
  59. print rootbad.children[0].children
  60.  
  61. def __init__(self, value, c=None):
  62.  
  63. if c == None:
  64. c = []
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement