Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.40 KB | None | 0 0
  1. def extract_min(self, ):
  2. parent_index = 0
  3. #left_children_index = 1
  4. #if parent_index * 2 < self.s ize:
  5. left_child_index = parent_index + 1
  6. #if parent_index * 2 + 1 < self.size:
  7. print(self.heap, self.size)
  8. right_child_index = parent_index + 2
  9. #last_element_index = self.size - 1
  10. if self.size == 0:
  11. return []
  12.  
  13. if right_child_index >= self.size - 1:
  14. if left_child_index >= self.size -1:
  15. elem = self.heap[0]
  16. self.heap = []
  17. return elem
  18. head = self.heap[parent_index]
  19. elem = self.heap[self.size-1]
  20. self.heap[parent_index] = self.heap[self.size - 1]
  21. del self.heap[self.size-1]
  22.  
  23.  
  24. while (self.heap[parent_index][1] > self.heap[left_child_index][1]) | \
  25. (self.heap[parent_index][1] > self.heap[right_child_index][1]):
  26. if self.heap[parent_index][1] > self.heap[left_child_index][1]:
  27. self.heap[parent_index] = self.heap[left_child_index]
  28. self.heap[left_child_index] = elem
  29. else:
  30. self.heap[parent_index] = self.heap[right_child_index]
  31. self.heap[right_child_index] = elem
  32.  
  33. if right_child_index >= self.size - 1:
  34. if left_child_index >= self.size - 1:
  35. return head
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement