Premium Prayagraj ❤️🍑 6378878445 👄🫦Independent Escort Service
Unit III Heaps.ppt
1. Heap
A heap is an array object that can be viewed as
a nearly complete binary tree.
3
5
2 4 1
6
6 5 3 2 4 1
1
2 3
4 5 6
1
2. 2
Array Representation of Heaps
A heap can be stored as
an array A.
Root of tree is A[1]
Left child of A[i] = A[2i]
Right child of A[i] = A[2i + 1]
Parent of A[i] = A[ i/2 ]
3. 3
Heap Types
Max-heaps (largest element at root), have
the max-heap property:
for all nodes i, excluding the root:
A[PARENT(i)] ≥ A[i]
Min-heaps (smallest element at root),
have the min-heap property:
for all nodes i, excluding the root:
A[PARENT(i)] ≤ A[i]
4. MAX HEAP
A complete binary tree called max heap ,if the
parent at any node is greater to the value of
each of its child nodes
Root nodes of max heap always the largest
values in tree.
Also called descending heap.
5. MIN HEAP
A complete binary tree called min heap ,if the
parent at any node is smaller to the value of
each of its child nodes
Root nodes of min heap always the smallest
values in tree.
Also called ascending heap.
6. What are Heaps Useful for?
To implement priority queues
Priority queue = a queue where all elements
have a “priority” associated with them
Remove in a priority queue removes the
element with the smallest priority
insert
removeMin
7. 7
Operations on Heaps
Maintain/Restore the max-heap property
MAX-HEAPIFY
Create a max-heap from an unordered array
BUILD-MAX-HEAP
Sort an array in place
HEAPSORT
8. 8
Maintaining the Heap Property
Suppose a node is smaller than a
child
Left and Right subtrees of i are max-heaps
To eliminate the violation:
Exchange with larger child
Move down the tree
Continue until node is not smaller than
children
15. HEAP SORT ALGORITHM
HEAP SORT
a)Insmax heap( ) algorithm
b) Delmax Heap( ) algorithm
c) Heapsort( ) algorithm
16. 16
Heap sort
Goal:
Sort an array using heap representations
Idea:
Build a max-heap from the array
Swap the root (the maximum element) with the last element
in the array
“Discard” this last node by decreasing the heap size
Call MAX-HEAPIFY on the new root
Repeat this process until only one node remains
HEAP SORT COMPLEXITY:- O(n log n)
17. InsMaxHeap (Tree, N, Item)
InsMaxHeap (Tree, N, Item)
A Heap H with N elements is stored in array Tree and Item of information is
given. Ptr gives location of Item as it rises in tree and Par denotes location of
parent of Item
1. Set N := N+1 and Ptr = N [Add new node to H and initialize Ptr]
2. [Find location to insert Item]
Repeat steps 3 to 6 while Ptr >1
3. Set Par := [Ptr/2] [Location of parent node]
4. If Item <= Tree[Par], then
Set Tree[Ptr] := Item and Return
[End of If structure]
5. Set Tree[Ptr] := Tree[Par] [Moves node down]
6. Set Ptr := Par [Updates Ptr]
[End of step 2 loop]
7. Set Tree[1] := Item [Assign Item as root of H]
8. Return
18. DelMaxHeap (Tree, N, Item)
A Heap H with N elements is stored in array Tree. Variable Last saves value of
original last node of H. Pointers Ptr, Left and Right give locations of Last and its
left & right children as Last sinks in tree.
1. Set Item := Tree[1] [Removes root of H]
2. Set Last := Tree[N] and N:=N-1 [Removes last node of H]
3. Set Ptr:= 1, Left:= 2 and Right:= 3 [Initializes pointers]
4. Repeat Steps 5 to 7 while Right <=N
5. If Last >= Tree[Left] and Last >= Tree[Right], then
Set Tree[Ptr] := Last and Return
[End of If structure]
6. If Tree[Right] <= Tree[Left], then
Set Tree[Ptr] := Tree[Left] and Ptr := Left
Else
Set Tree[Ptr] := Tree[Right] and Ptr := Right
[End of If structure]
7. Set Left := 2*Ptr and Right := Left +1
8. [End of Step 4 loop]
9. If Left = N and if Last < Tree[Left],
then Set Tree[PTR] = Tree[Left] and Ptr := Left
7. Set Tree[Ptr] := Last
8. Return
19. HEAP SORT ALGORITHM
HeapSort (A, N)
An array A with N elements is given. This algo. sorts elements of A.
1. [Build a Heap H]
Repeat for J=1 to N-1
Call InsHeap(A, J, A[J+1]) //InsHeap (Tree, N, Item)
[End of loop]
2. [Sort A by repeatedly deleting root of H]
Repeat while N>1
a) Call DelHeap(A, N, Item) //decreases N and returns Item
b) Set A[N+1] := Item
[End of loop]
3. Exit
21. COMPLEXITY OF HEAP
Heap is a balanced binary tree, the height of the tree is O(log n)
For the insert operation, we start by adding a value to the end of the
array O(1) (assuming the array doesn't have to be expanded).
Then we swap values up the tree until the order property has been
restored proportional to the height of the tree O(log n)
For remove operation, we follow a path down the tree from the root to
a leaf O(log n).
22. Heap sort Complexity:
a). To insert a single node in an empty heap is : O(1).
b). To insert a single element in a n node heap is : O(log n).
dd=time complexity for inserting one element=O(log n).
c.) To insert n elements in a n node heap is : O(n log n).
d.) To delete the largest element in a max heap tree : O(1).
e.) To delete the smallest element in a max heap tree :
O(log n).
f.) To delete n elements in a max heap tree : O(n log n).
g.) To create a heap, time complexity will be : O(n log n).
Now to sort the heap tree requires,
1. arrange or insert the elements in a form of heap i.e O(n log n) and
2. delete the elements one by one after swapping operation O(nlogn)
This gives Heap sort complexity = O(nlogn) + O(nlogn).
= 2 O(nlogn) = O(nlogn).
23. Polling Questions
On which algorithm is heap sort based on?
a) Fibonacci heap
b) Binary tree
c) Priority queue
d) FIFO