Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.66 KB | None | 0 0
  1. def validate(self):
  2. """
  3. Validates the heap. Returns True if the heap is a valid min-heap, and
  4. False otherwise.
  5.  
  6. >>> h = MinHeap()
  7. >>> h._items = [1, 3, 5]
  8. >>> h.validate()
  9. True
  10. >>> h._items = [1, 3, 5, 8, 9, 6, 7, 11]
  11. >>> h.validate()
  12. True
  13. >>> h._items = [2, 3, 1, 7]
  14. >>> h.validate()
  15. False
  16. """
  17. if self._items[0] > self._items[1]:
  18. return False
  19. for i in range(1,len(self._items)/2):
  20. if self._items[i-1] > (self._items[2*i] or self._items[2*i+1]):
  21. return False
  22. return True
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement