0% found this document useful (0 votes)
106 views25 pages

2.7 Heaps

NF NFGN

Uploaded by

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

2.7 Heaps

NF NFGN

Uploaded by

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

Department of Computer Science and Engineering (CSE)

UNIVERSITY INSTITUTE OF
ENGINEERING
COMPUTER SCIENCE ENGINEERING
Bachelor of Engineering
Design and Analysis of
Algorithms(CSH-311/ITH-311)
Heaps (CO2)

Topic: Heaps DISCOVER . LEARN . EMPOWER

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Learning Objectives & Outcomes


Objective:
• To understand the Heap and its types.
• To understand heapsort

Outcome:
• Student will understand
 Heap and its types
 Heap sort

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

POLL Question

1) In a max-heap, element with the greatest key is always in


the which node?

a) Leaf node
b) First node of left sub tree
c) root node
d) First node of right sub tree

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Heap

• A Heap is a special Tree-based data structure in which


the tree is a complete binary tree. Generally, Heaps can be
of two types:
• Max-Heap: In a Max-Heap the key present at the root
node must be greatest among the keys present at all of
it’s children. The same property must be recursively true
for all sub-trees in that Binary Tree.
• Min-Heap: In a Min-Heap the key present at the root
node must be minimum among the keys present at all of
it’s children. The same property must be recursively true
for all sub-trees in that Binary Tree.

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Methods or Operations of Heap

• find - find an item in a heap.


• insert - add an item in a heap ensuring the heap property
is maintained min-heap and max-heap property.
• delete - remove an item in a heap.
• extract - return the value of an item and then delete from
heap.
• replace - extract or pop the root and insert or push a
new item in a heap ensuring the heap property has
maintained min-heap and max-heap property.

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Methods or Operations of Heap (Contd..)

• Apart from the above mentioned basic operations, there


are other operations such as:
• size - returns the size of heap.
• is-empty - returns 'true' if heap is empty or 'false' if it
has value.
• merge - joining or union of two heaps, all the values from
both heaps are included but the original heaps are
preserved.
• meld - joining of two heaps where the values from both
heaps are included but the original heaps are destroyed.

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Max-Heap & Array Representation

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Maintaining the heap property

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Example of maintaining Max Heap:

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Building a heap

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Example of Building Max Heap

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Example of Building Max Heap (Contd..)

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Heap Sort
• Heaps can be used in sorting an array. In max-heaps, maximum
element will always be at the root. Heap Sort uses this
property of heap to sort the array.
• Consider an array Arr which is to be sorted using Heap Sort.
1. Initially build a max heap of elements in Arr.
2. The root element, that is Arr[1], will contain maximum
element of Arr. After that, swap this element with the last
element of Arr and heapify the max heap excluding the last
element which is already in its correct position and then
decrease the length of heap by one.
3. Repeat the step 2, until all the elements are in their correct
position

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Heap Sort Algorithm

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Example:

In the diagram below, initially there is an unsorted array Arr


having 6 elements and then max-heap will be built.

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Example (Contd.)

After building max-heap, the elements in the array Arr will


be:

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Step 1: 8 is swapped with 5.


Step 2: 8 is disconnected from heap as 8 is in correct
position now and.
Step 3: Max-heap is created and 7 is swapped with 3.
Step 4: 7 is disconnected from heap.
Step 5: Max heap is created and 5 is swapped with 1.
Step 6: 5 is disconnected from heap.
Step 7: Max heap is created and 4 is swapped with 3.
Step 8: 4 is disconnected from heap.
Step 9: Max heap is created and 3 is swapped with 1.
Step 10: 3 is disconnected.

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

After all the steps, we will get a sorted array.

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)
EXAMPLE 2 : HEAP SORT

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

REFERENCES
Text books:
•Cormen, Leiserson, Rivest, Stein, “Introduction to Algorithms”, Prentice Hall of
India, 3rd edition 2012. problem, Graph coloring.
•Horowitz, Sahni and Rajasekaran, “Fundamentals of ComputerAlgorithms”,
University Press (India), 2nd edition

Websites:
1.https://www.tutorialspoint.com/data_structures_algorithms/
heap_data_structure.htm
2.https://www.geeksforgeeks.org/

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Summary
Introduction to Heaps
• Basic terms

Types of Heaps
• Max-heap
• Min-heap

• Heap Sort

University Institute of Engineering (UIE)


THANK YOU

University Institute of Engineering (UIE)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy