SlideShare uma empresa Scribd logo
1 de 30
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 ,[object Object],[object Object],[object Object]
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

Mais conteúdo relacionado

Mais procurados (20)

Heap sort
Heap sortHeap sort
Heap sort
 
Heap sort
Heap sort Heap sort
Heap sort
 
Data Structure and Algorithms Heaps and Trees
Data Structure and Algorithms Heaps and TreesData Structure and Algorithms Heaps and Trees
Data Structure and Algorithms Heaps and Trees
 
05 heap 20161110_jintaeks
05 heap 20161110_jintaeks05 heap 20161110_jintaeks
05 heap 20161110_jintaeks
 
Binary Heap Tree, Data Structure
Binary Heap Tree, Data Structure Binary Heap Tree, Data Structure
Binary Heap Tree, Data Structure
 
Heaps
HeapsHeaps
Heaps
 
Heapsort quick sort
Heapsort quick sortHeapsort quick sort
Heapsort quick sort
 
Heap and heapsort
Heap and heapsortHeap and heapsort
Heap and heapsort
 
Heap_Sort1.pptx
Heap_Sort1.pptxHeap_Sort1.pptx
Heap_Sort1.pptx
 
Heapsort ppt
Heapsort pptHeapsort ppt
Heapsort ppt
 
Heaps
HeapsHeaps
Heaps
 
Presentation on Heap Sort
Presentation on Heap Sort Presentation on Heap Sort
Presentation on Heap Sort
 
Heap Sort in Design and Analysis of algorithms
Heap Sort in Design and Analysis of algorithmsHeap Sort in Design and Analysis of algorithms
Heap Sort in Design and Analysis of algorithms
 
lecture 5
lecture 5lecture 5
lecture 5
 
Heap tree
Heap treeHeap tree
Heap tree
 
3.7 heap sort
3.7 heap sort3.7 heap sort
3.7 heap sort
 
Heap Sort Algorithm
Heap Sort Algorithm Heap Sort Algorithm
Heap Sort Algorithm
 
Cis435 week05
Cis435 week05Cis435 week05
Cis435 week05
 
Algorithm chapter 6
Algorithm chapter 6Algorithm chapter 6
Algorithm chapter 6
 
Heapify algorithm
Heapify algorithmHeapify algorithm
Heapify algorithm
 

Destaque (20)

Heapsort
HeapsortHeapsort
Heapsort
 
Quick Sort , Merge Sort , Heap Sort
Quick Sort , Merge Sort ,  Heap SortQuick Sort , Merge Sort ,  Heap Sort
Quick Sort , Merge Sort , Heap Sort
 
Heap sort
Heap sort Heap sort
Heap sort
 
Heap sort
Heap sortHeap sort
Heap sort
 
Gc algorithm inside_dot_net
Gc algorithm inside_dot_netGc algorithm inside_dot_net
Gc algorithm inside_dot_net
 
Heaps
HeapsHeaps
Heaps
 
Heapify
HeapifyHeapify
Heapify
 
Graphs In Data Structure
Graphs In Data StructureGraphs In Data Structure
Graphs In Data Structure
 
Merge sort
Merge sortMerge sort
Merge sort
 
Chapter 11 - Sorting and Searching
Chapter 11 - Sorting and SearchingChapter 11 - Sorting and Searching
Chapter 11 - Sorting and Searching
 
Quick Sort
Quick SortQuick Sort
Quick Sort
 
lecture 6
lecture 6lecture 6
lecture 6
 
Leftist heap
Leftist heapLeftist heap
Leftist heap
 
Heapsort
HeapsortHeapsort
Heapsort
 
Heapsort
HeapsortHeapsort
Heapsort
 
Priority queues and heap sorting
Priority queues and heap sortingPriority queues and heap sorting
Priority queues and heap sorting
 
Heap property
Heap propertyHeap property
Heap property
 
HeapSort
HeapSortHeapSort
HeapSort
 
Data Structure Sorting
Data Structure SortingData Structure Sorting
Data Structure Sorting
 
B tree &amp;
B tree &amp;B tree &amp;
B tree &amp;
 

Mais de Tareq Hasan

Grow Your Career with WordPress
Grow Your Career with WordPressGrow Your Career with WordPress
Grow Your Career with WordPressTareq Hasan
 
Caching in WordPress
Caching in WordPressCaching in WordPress
Caching in WordPressTareq Hasan
 
How to Submit a plugin to WordPress.org Repository
How to Submit a plugin to WordPress.org RepositoryHow to Submit a plugin to WordPress.org Repository
How to Submit a plugin to WordPress.org RepositoryTareq Hasan
 
Composer - The missing package manager for PHP
Composer - The missing package manager for PHPComposer - The missing package manager for PHP
Composer - The missing package manager for PHPTareq Hasan
 
WordPress Theme & Plugin development best practices - phpXperts seminar 2011
WordPress Theme & Plugin development best practices - phpXperts seminar 2011WordPress Theme & Plugin development best practices - phpXperts seminar 2011
WordPress Theme & Plugin development best practices - phpXperts seminar 2011Tareq Hasan
 
08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.pptTareq Hasan
 
02 c++ Array Pointer
02 c++ Array Pointer02 c++ Array Pointer
02 c++ Array PointerTareq Hasan
 
01 c++ Intro.ppt
01 c++ Intro.ppt01 c++ Intro.ppt
01 c++ Intro.pptTareq Hasan
 
Algorithm: priority queue
Algorithm: priority queueAlgorithm: priority queue
Algorithm: priority queueTareq Hasan
 
Algorithm: Quick-Sort
Algorithm: Quick-SortAlgorithm: Quick-Sort
Algorithm: Quick-SortTareq Hasan
 
Java: Inheritance
Java: InheritanceJava: Inheritance
Java: InheritanceTareq Hasan
 
Java: Introduction to Arrays
Java: Introduction to ArraysJava: Introduction to Arrays
Java: Introduction to ArraysTareq Hasan
 
Java: Class Design Examples
Java: Class Design ExamplesJava: Class Design Examples
Java: Class Design ExamplesTareq Hasan
 

Mais de Tareq Hasan (20)

Grow Your Career with WordPress
Grow Your Career with WordPressGrow Your Career with WordPress
Grow Your Career with WordPress
 
Caching in WordPress
Caching in WordPressCaching in WordPress
Caching in WordPress
 
How to Submit a plugin to WordPress.org Repository
How to Submit a plugin to WordPress.org RepositoryHow to Submit a plugin to WordPress.org Repository
How to Submit a plugin to WordPress.org Repository
 
Composer - The missing package manager for PHP
Composer - The missing package manager for PHPComposer - The missing package manager for PHP
Composer - The missing package manager for PHP
 
WordPress Theme & Plugin development best practices - phpXperts seminar 2011
WordPress Theme & Plugin development best practices - phpXperts seminar 2011WordPress Theme & Plugin development best practices - phpXperts seminar 2011
WordPress Theme & Plugin development best practices - phpXperts seminar 2011
 
08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt
 
02 c++ Array Pointer
02 c++ Array Pointer02 c++ Array Pointer
02 c++ Array Pointer
 
01 c++ Intro.ppt
01 c++ Intro.ppt01 c++ Intro.ppt
01 c++ Intro.ppt
 
chapter22.ppt
chapter22.pptchapter22.ppt
chapter22.ppt
 
Algorithm.ppt
Algorithm.pptAlgorithm.ppt
Algorithm.ppt
 
chapter-8.ppt
chapter-8.pptchapter-8.ppt
chapter-8.ppt
 
chapter23.ppt
chapter23.pptchapter23.ppt
chapter23.ppt
 
chapter24.ppt
chapter24.pptchapter24.ppt
chapter24.ppt
 
Algorithm: priority queue
Algorithm: priority queueAlgorithm: priority queue
Algorithm: priority queue
 
Algorithm: Quick-Sort
Algorithm: Quick-SortAlgorithm: Quick-Sort
Algorithm: Quick-Sort
 
Java: GUI
Java: GUIJava: GUI
Java: GUI
 
Java: Inheritance
Java: InheritanceJava: Inheritance
Java: Inheritance
 
Java: Exception
Java: ExceptionJava: Exception
Java: Exception
 
Java: Introduction to Arrays
Java: Introduction to ArraysJava: Introduction to Arrays
Java: Introduction to Arrays
 
Java: Class Design Examples
Java: Class Design ExamplesJava: Class Design Examples
Java: Class Design Examples
 

chapter - 6.ppt