0% found this document useful (0 votes)
13 views

Binary Heap & Heap Sort

Uploaded by

nvkeerthuvcet
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Binary Heap & Heap Sort

Uploaded by

nvkeerthuvcet
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 30

Binary Heap

Definition
• A Binary Heap is a complete Binary Tree which is
used to store data efficiently to get the max or min
element based on its structure.
• A Binary Heap is either Min Heap or Max Heap.
• Min Heap – Minimum root
• Max Heap – Maximum root
• The same property must be recursively true for all
nodes in Binary Tree.
Min Heap example
Min Heap Property: The value of each node is greater than or
equal to the value of its parent, with the minimum value at the root.
Max heap Example
Max Heap Property: The value of each node is less than or equal
to the value of its parent, with the maximum value at the root.
Binary Heap Array
Representation
• A Binary Heap is a Complete Binary Tree. A binary
heap is typically represented as an array.
• The root element will be at Arr[0].
• The below table shows indices of other nodes for the
ith node, i.e., Arr[i]:
• The traversal method use to achieve Array
representation is Level Order
Operations
• getMin(): It returns the root element of Min Heap. The
time Complexity of this operation is O(1). In case of a
maxheap it would be getMax().
• extractMin(): Removes the minimum element from
MinHeap. The time Complexity of this Operation
is O(log N) as this operation needs to maintain the
heap property (by calling heapify()) after removing the
root.
• decreaseKey(): Decreases the value of the key. The
time complexity of this operation is O(log N). If the
decreased key value of a node is greater than the
parent of the node, then we don’t need to do anything.
Otherwise, we need to traverse up to fix the violated
heap property.
Operations
• insert(): Inserting a new key takes O(log N) time. We
add a new key at the end of the tree. If the new key is
greater than its parent, then we don’t need to do
anything. Otherwise, we need to traverse up to fix the
violated heap property.
• delete(): Deleting a key also takes O(log N) time. We
replace the key to be deleted with the minimum infinite
by calling decreaseKey(). After decreaseKey(), the
minus infinite value must reach root, so we
call extractMin() to remove the key.
Heapify:

• Given a binary tree, converting it into a heap is called


"Heapify". We use the array implementation of the tree,
and we'll convert it into an array representing a heap:
Inserting a node
• Given a node, we need to append the node to the array
at the end such that the last node from left to right is
getting filled, and the most important step is to heapify
the array after insertion
Inserting a node
Deletion
• If the node we want to delete from the heap is a leaf
node, we can delete it. If it is an internal node, we need
to swap the node with any leaf node and then delete it.
• Never forget to heapify the array.
Deletion
Get Minimum node/ Maximum
node:
• In a minheap, the root node will have the minimum
value. Hence, we can write a function to find the min
node and return the root(index-0).
• In the same way, in a maxheap, the root node will have
the maximum value:
Get Minimum node/ Maximum
node:
Using heapq module
Heap Sort
• Heap sort is a comparison-based sorting technique
based on Binary Heap data structure.
• It is similar to the selection sort where we first find the
minimum element and place the minimum element at
the beginning.
• Repeat the same process for the remaining elements.
Algorithm
• First convert the array into heap data structure using heapify
• Delete the root node of the Max-heap one by one
• replace it with the last node in the heap
• heapify the root of the heap
• Repeat this process until size of heap is greater than 1
• Build a heap from the given input array.
• Repeat the following steps until null
• Swap the root element of the heap (which is the largest element)
with the last element of the heap.
• Remove the last element of the heap (which is now in the correct
position).
• Heapify the remaining elements of the heap.
• The sorted array is obtained by reversing the order of the
elements
• Transform into max heap: After that, the task is to
construct a tree from that unsorted array and try to
convert it into max heap.
• To transform a heap into a max-heap, the parent node
should always be greater than or equal to the child
nodes
• Perform heap sort: Remove the maximum element in
each step (i.e., move it to the end position and remove
that) and then consider the remaining elements and
transform it into a max heap.
Complexity analysis
Time Complexity: O(N log N)
Auxiliary Space: O(log n)
Advantages of Heap sort:
• Efficient Time Complexity
• Less Memory usage
• Simplicity
Disadvantages of Heap Sort

• Costly : Heap sort is costly as the constants are higher


compared to merge sort even if the time complexity is
O(n Log n) for both.
• Unstable : Heap sort is unstable. It might rearrange the
relative order.
• Efficient: Heap Sort is not very efficient when working
with highly complex data.

You might also like