Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def extract_min(self, ):
- parent_index = 0
- #left_children_index = 1
- #if parent_index * 2 < self.s ize:
- left_child_index = parent_index + 1
- #if parent_index * 2 + 1 < self.size:
- print(self.heap, self.size)
- right_child_index = parent_index + 2
- #last_element_index = self.size - 1
- if self.size == 0:
- return []
- if right_child_index >= self.size - 1:
- if left_child_index >= self.size -1:
- elem = self.heap[0]
- self.heap = []
- return elem
- head = self.heap[parent_index]
- elem = self.heap[self.size-1]
- self.heap[parent_index] = self.heap[self.size - 1]
- del self.heap[self.size-1]
- while (self.heap[parent_index][1] > self.heap[left_child_index][1]) | \
- (self.heap[parent_index][1] > self.heap[right_child_index][1]):
- if self.heap[parent_index][1] > self.heap[left_child_index][1]:
- self.heap[parent_index] = self.heap[left_child_index]
- self.heap[left_child_index] = elem
- else:
- self.heap[parent_index] = self.heap[right_child_index]
- self.heap[right_child_index] = elem
- if right_child_index >= self.size - 1:
- if left_child_index >= self.size - 1:
- return head
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement