binary heap
- shape property: a binary heap is an almost complete tree
- heap property: the key stored in each node is either greater than or equal to, or less than or equal to the keys in the node's children
bubbling
bubble up
bubbling a node upwards consists of recursively switching it with its parent until its parent is bigger/smaller than it (depending on whether we're using a min heap or a max heap)
bubble down
bubbling a node downards consists of recursively switching it with one of its children until it gets to its correct position
this is a pseudocode for the algorithm:
l = left(i) // index of left child
r = right(i) // index of right child
if l <= heap-size[A] and A[l] > A[i]
largest = l
else
largest = i
if r <= heap-size[A] and A[r] > A[largest]
largest = r
if largest != i
exchange A[i] A[largest]
heapify(A, largest)
time complexity is insertion
inserting a new node to the heap consists of adding it as a leaf such that the heap remains almost complete and bubbling up the newly added node so that the keys in the tree remain sorted
if the heap is in array representation, we insert the new node to the end of the array and heapify it upwards (bubble up)
deletion
deletion of a node consists of replacing the rightmost leaf with it and bubble it downwards if its smaller than one of its children or upwards if its bigger than its parent
binary heap array representation
arrays that represent heap have the following characterstics:
- root is
- the parent of the i^{th} node (in the array) is
- left child of the i^{th} node is
- right child of the i^{th} node is
the array should hold 2 values,
array-size
, which is the size of the array, and heap-size
, which is the number of elements of the heap in the arrayconstruction of binary heap from array
given some random array, e.g. A=[10,20,30,40,50,70,35,60,80]
, we need to reorganize its elements such that it represents a binary heap
to achieve this we apply bubble down to the first half of the array in reverse (because the leaf nodes need not be heapified as they already follow the heap property)
- g. for the given array we apply
bubble(A, i)
such that
Build-Heap(A)
n = length(A)
heap-size = n
for i = n/2 to 1
heapify(A, i)
binary heap algorithms
biggest k nodes
initial inefficient solution:we analyze the time complexity of this algorithm:
which means that the time complexity is
we cant arrive at broken link: blk:1670086431 for this algorithm
we suggest another algorithm that is more efficient: