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

Heap Sort Deletion

this
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Heap Sort Deletion

this
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 8

HEAP SORT DELETION

GROUP MEMBERS:
MOEL JOHN DEL ROSARIO
KAEL MALLON
BRYAN LAWAS
WHAT IS HEAP SORT DELETION?

• In the context of heap sort, "deletion" typically refers to the


process of removing elements from a heap data structure,
specifically the root element, which is the largest (in a max
heap) or the smallest (in a min heap) value. This is a key step
in the heap sort algorithm, where elements are repeatedly
deleted from the heap to produce a sorted array.
DELETION PROCESS IN A BINARY HEAP
• Remove the Root:
• The root of the heap is the element to be deleted. In a max heap, this is the
maximum element; in a min heap, it’s the minimum.
• Replace with the Last Element:
• Replace the root with the last element in the heap (the bottom-rightmost element).
• Heapify Down:
• After replacing the root, the heap may no longer satisfy the heap property. To
restore it:Compare the new root with its children.
• Swap it with the larger child (in a max heap) or the smaller child (in a min heap) if
the heap property is violated.
• Repeat this process until the heap property is restored.
START WITH A HEAP:

• Remember, in a max heap, the largest element is at the top


(the root). For example, let’s say we have the heap:
10
/ \
9 8
/ \ /
7 6 5
REMOVE THE ROOT:
• The first step in deletion is to remove the root element (in this case, 10).
This is the largest element in a max heap.
• Replace with the Last Element:
• Take the last element in the heap (hear its 5) and move it to root position.
Now the heap looks like this
5
/ \
9 8
/ \ /
7 6
HEAPIFY DOWN:
• The new root(5) may not satisfy the heap properly( its
smaller than 9 and 8 )
• To fix this, compare it with its children (9 and 8) and swap it
with its larger child (9)
9
/ \
7 8
/ \ /
5 6
• Now, 5 is still not in the right position, so we compare it again
with its children (7 and 6 ) and swap it with 7:
REPEAT:

• Continue this process of removing the root and heapifying


until the heap is empty or all elements are sorted.

You might also like