Advertisement
Guest User

Untitled

a guest
Feb 27th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1. class DrzewoBST:
  2. def __init__(self, value = None):
  3. self.val = value
  4. self.left = None
  5. self.right = None
  6.  
  7. def __str__(self):
  8. return "[%s, %s, %s]" % (self.left, str(self.val), self.right)
  9.  
  10. def wstawianie(self, wartosc):
  11. if self is None:
  12. self = DrzewoBST(wartosc)
  13. if wartosc < self.val:
  14. if self.left is None:
  15. self.left = DrzewoBST(wartosc)
  16. else:
  17. self.left.wstawianie(wartosc)
  18. elif wartosc > self.val:
  19. if self.right is None:
  20. self.right = DrzewoBST(wartosc)
  21. else:
  22. self.right.wstawianie(wartosc)
  23.  
  24. def wyszukiwanie(self, liczba):
  25. print(self.val)
  26. if self is None:
  27. return False
  28. if int(self.val) == liczba:
  29. return True
  30. if liczba < self.val:
  31. self.left.wyszukiwanie(liczba)
  32. else:
  33. self.right.wyszukiwanie(liczba)
  34.  
  35. t = DrzewoBST(2)
  36. t.wstawianie(3)
  37. t.wstawianie(3)
  38. print(t.wyszukiwanie(3))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement