fork download
  1. # your code goes here
  2. class BinaryTree:
  3. def __init__(self, root):
  4. self.key = root
  5. self.left_child = None
  6. self.right_child = None
  7.  
  8. def insert_left(self, new_node):
  9. if (self.left_child is None):
  10. self.left_child = BinaryTree (new_node)
  11. else:
  12. t = BinaryTree(new_node)
  13. t.left_child = self.left_child
  14. self.left_child = t
  15. def insert_right(self, new_node):
  16. if self.right_child is None:
  17. self.right_child = BinaryTree(new_node)
  18. else:
  19. t = BinaryTree(new_node)
  20. t.right_child = self.right_child
  21. self.right_child = t
  22. def get_right_child(self):
  23. return self.right_child
  24. def get_left_child(self):
  25. return self.left_child
  26. def set_root_val(self, obj):
  27. self.key = obj
  28. def get_root_val(self):
  29. return self.key
  30.  
  31. r = BinaryTree('a')
  32.  
  33. print("-1", r.get_root_val())
  34.  
  35. print("-2",r.get_left_child())
  36.  
  37. r.insert_left('b')
  38.  
  39. print("-3",r.get_left_child())
  40.  
  41. print("-4", r.get_left_child().get_root_val())
  42.  
  43. r.insert_right('c')
  44.  
  45. print("-5", r.get_right_child())
  46.  
  47. print("-6", r.get_right_child().get_root_val())
  48.  
  49. r.get_right_child().set_root_val('hello')
  50.  
  51. print("-7",r.get_right_child().get_root_val())
Success #stdin #stdout 0.02s 9000KB
stdin
Standard input is empty
stdout
-1 a
-2 None
-3 <__main__.BinaryTree object at 0x14cc8f7b5f28>
-4 b
-5 <__main__.BinaryTree object at 0x14cc8f7b5f60>
-6 c
-7 hello