MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / delete_item

Method delete_item

data_structures/heap/heap_generic.py:90–103  ·  view source on GitHub ↗

Deletes given item from heap if present

(self, item: int)

Source from the content-addressed store, hash-verified

88 self._heapify_down(index)
89
90 def delete_item(self, item: int) -> None:
91 """Deletes given item from heap if present"""
92 if item not in self.pos_map:
93 return
94 index = self.pos_map[item]
95 del self.pos_map[item]
96 self.arr[index] = self.arr[self.size - 1]
97 self.pos_map[self.arr[self.size - 1][0]] = index
98 self.size -= 1
99 # Make sure heap is right in both up and down direction. Ideally only one
100 # of them will make any change- so no performance loss in calling both.
101 if self.size > index:
102 self._heapify_up(index)
103 self._heapify_down(index)
104
105 def insert_item(self, item: int, item_value: int) -> None:
106 """Inserts given item with given value in heap"""

Callers 1

extract_topMethod · 0.95

Calls 2

_heapify_upMethod · 0.95
_heapify_downMethod · 0.95

Tested by

no test coverage detected