0% found this document useful (0 votes)
21 views14 pages

Heaps

Uploaded by

Marco Dela Rosa
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)
21 views14 pages

Heaps

Uploaded by

Marco Dela Rosa
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/ 14

HEAPS

What is Heaps?
Heaps = data structure to manage information
Sometimes called as:
-binary heaps
-nearly complete binary trees

A heap is a specialized tree-based


data structure that satisfies the
heap property. It's often
implemented as an array and
commonly used for priority
queues.
There are Two types of Heaps:
• Max-Heaps
• Min-Heaps
Uses:
• Heapsort
• Priority queues
Heapsort
Heap sort is a comparison-based sorting algorithm that uses
a binary heap data structure to sort elements. It operates by
first building a max-heap (for ascending order) or a min-
heap (for descending order) from the input array and then
repeatedly extracting the root element (which is the
maximum in a max-heap or minimum in a min-heap) to
obtain a sorted array.
Priority Queues
Priority queues are abstract data structures that
store elements along with associated priorities.
They enable efficient access to the element with
the highest (or lowest) priority. Unlike traditional
queues where elements are retrieved in a first-
in-first-out (FIFO) manner, in a priority queue,
elements are processed based on their priority
levels.
Min-Heaps
Priority Queues
-value of i ≥
value of
In aparent
min-heap, the value of the
parent node is less than or
equal to the values of its
children nodes. Consequently,
the lowest value is at the root,
and each node's value is greater
than or equal to its parent's
value.
Max-Heaps
Heapsort
-value of i ≤
value of parent
In a max-heap, the value
of the parent node is
greater than or equal to the
values of its children
nodes. This means the
highest value is at the
root, and each node's
value is less than or equal
to its parent's value.
Heap Methods
•Max-heapify
•Build-max-heap
Max-heapify

-Maintains max-heap property


• value of i ≤ value of parent
• value of i ≥ value of children
It is a fundamental operation used in maintaining the max-
heap property in a heap data structure, specifically in the
context of a binary tree representing a max-heap.
Build-max-heap

-Wrapper function
that calls max-heapify
It is an algorithm used to construct a max-heap from
an array of elements. The goal is to rearrange the
elements in the array so that they satisfy the max-heap
property.

You might also like