SlideShare a Scribd company logo
Heap Data Structure The heap data structure is an array object that can be viewed as a nearly complete binary tree.
Heap  1 2 3 4 5 6 7 8 9 10 (a) 16 2 9 8 3 10 7 14 4 1
Heap (b) 1 4 2 3 9 7 8 10 14 16 5 4 3 2 1 10 9 8 7 6
Heap There are two kinds of binary heaps max-heaps min heaps
Maintaining the Heap Property The function of MAX-HEAPIFY is to let the value at A[i] “float down” in the max-heap so that the sub-tree rooted at index i becomes  a max-heap
Maintaining the Heap Property MAX-HEAPIFY(A, i) l   LEFT(i) r    RIGHT(i) if l <= heap-size[A] and A[l] >A [i] then largest    l else largest    i
Maintaining the Heap Property If r<= heap-size[A] and A[r] > A[largest] then largest    r If largest  ≠ i then exchange A[i]    A[largest] MAX-HEAPIFY(A, largest)
Maintaining the Heap Property 1 2 3 4 5 6 7 8 9 10 i (a) 16 2 9 14 3 10 7 4 8 1
Maintaining the Heap Property 1 2 3 4 5 6 7 8 9 10 i (b) 16 2 9 4 3 10 7 14 8 1
Maintaining the Heap Property 1 2 3 4 5 6 7 8 9 10 i (c) 16 2 9 6 3 10 7 14 4 1
Building a Heap BUILD-MAX-HEAP(A) heap-size[A]    length[A] for i    length[A]/2 downto 1 do MAX-HEAPIFY(A, i)
Building a Heap 3 2 16 1 10 4 7 8 14 9 A
Building a Heap 1 2 3 4 5 6 7 8 9 10 i (a) 4 14 9 2 10 3 16 1 8 7
Building a Heap 1 2 3 4 5 6 7 8 9 10 i (b) 4 14 9 2 10 3 16 1 8 7
Building a Heap 1 2 3 4 5 6 7 8 9 10 i (c) 4 2 9 14 10 3 16 1 8 7
Building a Heap 1 2 3 4 5 6 7 8 9 10 i (d) 4 2 9 14 3 10 16 1 8 7
Building a Heap 1 2 3 4 5 6 7 8 9 10 i (e) 4 2 9 14 3 10 7 16 8 1
Building a Heap 1 2 3 4 5 6 7 8 9 10 (f) 16 2 9 8 3 10 7 14 4 1
Heapsort Algorithm HEAPSORT(A) BUILD-MAX-HEAP(A) for I    length[A] downto 2  do exchange A[1]       A[i] heap-size[A]    heap-size[A] -1 MAX-HEAPIFY(A,1)
Heapsort Algorithm (a) 16 2 9 8 3 10 7 14 4 1
Heapsort Algorithm (b) i 14 2 9 4 3 10 7 8 1 16
Heapsort Algorithm (c) i 10 2 1 4 3 9 7 8 16 14
Heapsort Algorithm (d) i 9 1 4 2 3 7 8 16 14 10
Heapsort Algorithm (e) i 8 1 4 3 2 7 16 14 10 19
Heapsort Algorithm (f) i 7 1 3 2 4 16 14 10 9 8
Heapsort Algorithm (g) i 4 1 3 2 16 14 10 9 8 7
Heapsort Algorithm (h) i 3 1 2 16 14 10 9 8 7 4
Heapsort Algorithm (i) i 2 1 16 14 10 9 8 7 4 3
Heapsort Algorithm (j) i 1 2 16 14 10 9 8 7 4 3
Heapsort Algorithm k 3 4 7 2 9 1 16 14 10 8 A

More Related Content

PPT
Lec 17 heap data structure
PPT
Heapsort
PPTX
Heapsort using Heap
PPTX
Heap sort
PPTX
heap Sort Algorithm
PDF
Heaps
PPT
Lec 17 heap data structure
Heapsort
Heapsort using Heap
Heap sort
heap Sort Algorithm
Heaps

What's hot (20)

PPT
Heap sort
PPT
Data Structure and Algorithms Heaps and Trees
PPTX
05 heap 20161110_jintaeks
PPTX
Binary Heap Tree, Data Structure
PDF
PDF
Heapsort quick sort
PDF
Heap and heapsort
PPTX
Heap_Sort1.pptx
PPT
Heapsort ppt
PPTX
Presentation on Heap Sort
PPTX
Heap Sort in Design and Analysis of algorithms
PPT
lecture 5
PPT
Heap tree
PPT
3.7 heap sort
PPTX
Heap Sort Algorithm
PPT
Cis435 week05
PDF
Algorithm chapter 6
PPTX
Heapify algorithm
Heap sort
Data Structure and Algorithms Heaps and Trees
05 heap 20161110_jintaeks
Binary Heap Tree, Data Structure
Heapsort quick sort
Heap and heapsort
Heap_Sort1.pptx
Heapsort ppt
Presentation on Heap Sort
Heap Sort in Design and Analysis of algorithms
lecture 5
Heap tree
3.7 heap sort
Heap Sort Algorithm
Cis435 week05
Algorithm chapter 6
Heapify algorithm
Ad

Viewers also liked (20)

PPT
Heapsort
PDF
Quick Sort , Merge Sort , Heap Sort
PPTX
Heap sort
PPTX
Gc algorithm inside_dot_net
PDF
Heaps
PDF
Heapify
PPT
Graphs In Data Structure
PPT
Merge sort
PPT
Chapter 11 - Sorting and Searching
PPT
Quick Sort
PPT
lecture 6
PPT
Leftist heap
PPTX
Heapsort
PPT
Heapsort
PPT
Priority queues and heap sorting
DOCX
Heap property
PDF
HeapSort
PPT
Data Structure Sorting
PPTX
B tree &amp;
Heapsort
Quick Sort , Merge Sort , Heap Sort
Heap sort
Gc algorithm inside_dot_net
Heaps
Heapify
Graphs In Data Structure
Merge sort
Chapter 11 - Sorting and Searching
Quick Sort
lecture 6
Leftist heap
Heapsort
Heapsort
Priority queues and heap sorting
Heap property
HeapSort
Data Structure Sorting
B tree &amp;
Ad

Similar to chapter - 6.ppt (20)

PPTX
Algorithms - "heap sort"
PDF
Heap Hand note
PDF
LEC 8-DS ALGO(heaps).pdf
PPTX
Lecture 07 - HeapSort.pptx
PPT
heapSortasdfghjklzxcvbnmqwertyuifghjkxcv
PPT
lect1-2LJGJHGJHGHJKHJHJHJHJJHHHHJJHJH.ppt
PPT
Analysis of Algorithms-Heapsort
PPTX
Lecture 5_ Sorting and order statistics.pptx
PDF
Heap, Types of Heap, Insertion and Deletion
PPTX
Heaps & its operation -Max Heap, Min Heap
PPT
Heapsortokkay
PPTX
comparisonbasedsortingalgorithmHeap Sort.pptx
PPTX
Data Structures and Agorithm: DS 18 Heap.pptx
PPTX
Heap Sort.ppx
PPT
thisisheapsortpptfilewhichyoucanuseanywhereanytim
PPT
Complete binary tree and heap
PPTX
week2.v2 dsfjue0owirewoifudsoufsoiuewrew.pptx
PDF
Heap Tree.pdf
Algorithms - "heap sort"
Heap Hand note
LEC 8-DS ALGO(heaps).pdf
Lecture 07 - HeapSort.pptx
heapSortasdfghjklzxcvbnmqwertyuifghjkxcv
lect1-2LJGJHGJHGHJKHJHJHJHJJHHHHJJHJH.ppt
Analysis of Algorithms-Heapsort
Lecture 5_ Sorting and order statistics.pptx
Heap, Types of Heap, Insertion and Deletion
Heaps & its operation -Max Heap, Min Heap
Heapsortokkay
comparisonbasedsortingalgorithmHeap Sort.pptx
Data Structures and Agorithm: DS 18 Heap.pptx
Heap Sort.ppx
thisisheapsortpptfilewhichyoucanuseanywhereanytim
Complete binary tree and heap
week2.v2 dsfjue0owirewoifudsoufsoiuewrew.pptx
Heap Tree.pdf

More from Tareq Hasan (20)

PPTX
Grow Your Career with WordPress
PDF
Caching in WordPress
PDF
How to Submit a plugin to WordPress.org Repository
PDF
Composer - The missing package manager for PHP
PDF
WordPress Theme & Plugin development best practices - phpXperts seminar 2011
PPT
08 c++ Operator Overloading.ppt
PPT
02 c++ Array Pointer
PPT
01 c++ Intro.ppt
PPT
chapter22.ppt
PPT
Algorithm.ppt
PPT
chapter-8.ppt
PPT
chapter23.ppt
PPT
chapter24.ppt
PPT
Algorithm: priority queue
PPT
Algorithm: Quick-Sort
PPT
Java: GUI
PPT
Java: Inheritance
PPT
Java: Exception
PPT
Java: Introduction to Arrays
PPT
Java: Class Design Examples
Grow Your Career with WordPress
Caching in WordPress
How to Submit a plugin to WordPress.org Repository
Composer - The missing package manager for PHP
WordPress Theme & Plugin development best practices - phpXperts seminar 2011
08 c++ Operator Overloading.ppt
02 c++ Array Pointer
01 c++ Intro.ppt
chapter22.ppt
Algorithm.ppt
chapter-8.ppt
chapter23.ppt
chapter24.ppt
Algorithm: priority queue
Algorithm: Quick-Sort
Java: GUI
Java: Inheritance
Java: Exception
Java: Introduction to Arrays
Java: Class Design Examples

chapter - 6.ppt