Skip to main content

Heap

Operationfind MIN/MAXdelete MIN/MAX - remove root nodeinsert - update a keyincrease keymeld - merge 2 heaps

Binary

O(1)O(log n)O(log n)O(log n)O(n)

Leftist

O(1)O(log n)O(log n)O(log n)O(log n)

Binomial

O(1)O(log n)O(1)O(log n)O(log n)

Fibonacci

O(1)O(log n)O(1)O(1)O(1)

Pairing

O(1)O(log n)O(1)O(log n)O(1)

Brodal

O(1)O(log n)O(1)O(1)O(1)

Rank-pairing

O(1)O(log n)O(1)O(1)O(1)

Strict Fibonacci

O(1)O(log n)O(1)O(1)O(1)

2-3 heap

O(log n)O(log n)O(log n)O(1)O(log n)

Definition​

Heap is a tree-like data structure used for efficient storage and retrieval of data.

Simplified

Heap, it's like a family tree of blocks where each block (called 'nodes') has children, but the rule is, the parent block is always bigger (or smaller depending on the type of heap) than its children blocks. This helps find the biggest or smallest block quickly.

Practice​

AspectPseudo Code
Heapify Up
heapify_up(index):
current_index = index
current_element = heap[current_index]

while current_index > 0:
parent_index = (current_index - 1) / 2
parent = heap[parent_index]

if current_element > parent:
break
heap[current_index] = parent
current_index = parent_index

heap[current_index] = current_element
Heapify Down
heapify_down(index):
current_index = index
while current_index < heap.length:
left_child_index=2 * current_index + 1
right_child_index = 2 * current_index + 2
left_child = heap[left_child_index]
right_child = heap[right_child_index]

min_child_index = ø
if left_child == ø || right_child == ø:
if left_child == ø:
min_child_index = left_child_index
else:
min_child_index = right_child_index
else:
if left_child == right_child:
if left_child < heap[current_index]:
min_child_index = left_child_index
else:
min_child_index = right_child_index
else if left_child < heap[current_index]:
min_child_index = left_child_index
else:
min_child_index = right_child_index

if min_child_index == ø || heap[min_child_index] > heap[current_index]:
break

min_child = heap[min_child_index]
heap[current_index] = min_child
current_index = min_child_index
Find MIN
find_min():
if heap == ø:
return ø
return heap[0]
Find MAX
find_max():
if heap == ø:
return ø
return heap[heap.length - 1]
Extract MIN
extract_min():
if heap == ø:
return ø
min = heap[0]
heap[0] = heap[heap.length - 1]
heap.remove_last()
heapify_down(0)
return min
Extract MAX
extract_max():
if heap == ø:
return ø

max = heap[heap.length - 1]
heap[0] = heap[heap.length - 1]
heap.remove_last()
heapify_down()
return max
Insertion
insert(element):
heap.add(element)
heapify_up(heap.length - 1)
Increase Key
increase_key(index, newKey):
if newKey <> heap[index]:
"New key must be greater than current key"

heap[index] = newKey
heapifyUp(index)
Decrease Key
decrease_key(index, newKey):
if newKey > heap[index]:
"New key must be less than current key"

heap[index] = newKey
heapify_down(index)
Meld
meld(other):
merged_heap = Heap()
merged_heap.heap = heap + other.heap

for i, i in heap.length / 2, i >= 0:
heapify_down(i)

return merged_heap