CS 225 Data Structures Oc October 31 He Heaps and Priority Qu - - PowerPoint PPT Presentation

cs 225
SMART_READER_LITE
LIVE PREVIEW

CS 225 Data Structures Oc October 31 He Heaps and Priority Qu - - PowerPoint PPT Presentation

CS 225 Data Structures Oc October 31 He Heaps and Priority Qu Queues G G Carl Evans Ru Running T Times Hash Table AVL Linked List SUHA: Find Worst Case: SUHA: Insert Worst Case: Storage Space Se Secr cret, M Mystery D


slide-1
SLIDE 1

CS 225

Data Structures

Oc October 31 – He Heaps and Priority Qu Queues

G G Carl Evans

slide-2
SLIDE 2

Ru Running T Times

Hash Table AVL Linked List

Find

SUHA: Worst Case:

Insert

SUHA: Worst Case:

Storage Space

slide-3
SLIDE 3

Se Secr cret, M Mystery D Data St Stru ruct cture

ADT: insert remove isEmpty

slide-4
SLIDE 4

Pr Priority Queue Implementat ation

insert removeMin unsorted sorted sorted unsorted

slide-5
SLIDE 5

An Another p r possibly s stru ructure…

5 15 9 25 4 6 7 20 11 16 12 14

slide-6
SLIDE 6

(m (min)He n)Heap

5 15 9 25 4 6 7 20 11 16 12 14

A complete binary tree T is a min-heap if:

  • T = {} or
  • T = {r, TL, TR}, where r is

less than the roots of {TL, TR} and {TL, TR} are min-heaps.

slide-7
SLIDE 7

(m (min)He n)Heap

5 15 9 25 4 6 7 20 11 16 12 14

4 5 6 15 9 7 20 16 25 14 12 11

slide-8
SLIDE 8

in insert

5 15 9 25 4 6 7 20 11 16 12 14

4 5 6 15 9 7 20 16 25 14 12 11

slide-9
SLIDE 9

template <class T> void Heap<T>::_insert(const T & key) { // Check to ensure there’s space to insert an element // ...if not, grow the array if ( size_ == capacity_ ) { _growArray(); } // Insert the new element at the end of the array item_[++size] = key; // Restore the heap property _heapifyUp(size); } 1 2 3 4 5 6 7 8 9 10 11 12

in insert

5 15 9 25 4 6 7 20 11 16 12 14

4 5 6 15 9 7 20 16 25 14 12 11

slide-10
SLIDE 10

gr growA wArray

5 15 9 25 4 6 7 20 11 16 12 14

slide-11
SLIDE 11

template <class T> void Heap<T>::_heapifyUp( _________________ ) { if ( index > _________ ) { if ( item_[index] < item_[ parent(index) ] ) { std::swap( item_[index], item_[ parent(index) ] ); _heapifyUp( ________________ ); } } } 1 2 3 4 5 6 7 8 9

in insert t - he heapi pifyUp yUp

template <class T> void Heap<T>::_insert(const T & key) { // Check to ensure there’s space to insert an element // ...if not, grow the array if ( size_ == capacity_ ) { _growArray(); } // Insert the new element at the end of the array item_[++size] = key; // Restore the heap property _heapifyUp(size); } 1 2 3 4 5 6 7 8 9 10 11 12

slide-12
SLIDE 12

template <class T> void Heap<T>::_heapifyUp( _________________ ) { if ( index > _________ ) { if ( item_[index] < item_[ parent(index) ] ) { std::swap( item_[index], item_[ parent(index) ] ); _heapifyUp( ________________ ); } } } 1 2 3 4 5 6 7 8 9

he heapi pifyUp yUp

5 15 9 25 4 6 7 20 11 16 12 14

4 5 6 15 9 7 20 16 25 14 12 11 7

slide-13
SLIDE 13

re removeMin

5 15 9 25 4 6 7 20 11 16 12 14

4 5 6 15 9 7 20 16 25 14 12 11

slide-14
SLIDE 14

template <class T> void Heap<T>::_removeMin() { // Swap with the last value T minValue = item_[1]; item_[1] = item_[size_]; size--; // Restore the heap property heapifyDown(); // Return the minimum value return minValue; } 1 2 3 4 5 6 7 8 9 10 11 12 13

re removeMin

5 15 9 25 4 6 7 20 11 16 12 14

4 5 6 15 9 7 20 16 25 14 12 11

slide-15
SLIDE 15

template <class T> void Heap<T>::_removeMin() { // Swap with the last value T minValue = item_[1]; item_[1] = item_[size_]; size--; // Restore the heap property _heapifyDown(); // Return the minimum value return minValue; } template <class T> void Heap<T>::_heapifyDown(int index) { if ( !_isLeaf(index) ) { T minChildIndex = _minChild(index); if ( item_[index] ___ item_[minChildIndex] ) { std::swap( item_[index], item_[minChildIndex] ); _heapifyDown( ________________ ); } } } 1 2 3 4 5 6 7 8 9 10

re removeMin - he heapi pifyD yDown wn

1 2 3 4 5 6 7 8 9 10 11 12 13

slide-16
SLIDE 16

template <class T> void Heap<T>::_heapifyDown(int index) { if ( !_isLeaf(index) ) { T minChildIndex = _minChild(index); if ( item_[index] ___ item_[minChildIndex] ) { std::swap( item_[index], item_[minChildIndex] ); _heapifyDown( ________________ ); } } } 1 2 3 4 5 6 7 8 9 10

re removeMin

5 15 9 25 6 7 20 16 12 14

4 5 6 15 9 7 20 16 25 14 12 11 11

slide-17
SLIDE 17

bui buildHe dHeap

U L D P B I H E W A O N

B U I L D H E A P N O W

slide-18
SLIDE 18

bui buildHe dHeap – so sorted d array

B E H O A D I L W N U P

B U I L D H E A P N O W A B D E H I L N O P U W

slide-19
SLIDE 19

bui buildHe dHeap - he heapi pifyUp yUp

U L D P B I H E W A O N

B U I L D H E A P N O W

slide-20
SLIDE 20

bui buildHe dHeap - he heapi pifyD yDown wn

U L D P B I H E W A O N

B U I L D H E A P N O W

slide-21
SLIDE 21

bui buildHe dHeap

U L D P B I H E W A O N

B U I L D H E A P N O W

  • 1. Sort the array – it’s a heap!

2. 3.

template <class T> void Heap<T>::buildHeap() { for (unsigned i = 2; i <= size_; i++) { heapifyUp(i); } } 1 2 3 4 5 6 template <class T> void Heap<T>::buildHeap() { for (unsigned i = parent(size); i > 0; i--) { heapifyDown(i); } } 1 2 3 4 5 6

slide-22
SLIDE 22

Pr Proving bui buildHe dHeap Ru Running T Time

Theorem: The running time of buildHeap on array of size n is: _________. Strategy:

slide-23
SLIDE 23

Pr Proving bui buildHe dHeap Ru Running T Time

S(h): Sum of the heights of all nodes in a complete tree of height h. S(0) = S(1) = S(h) =

U L D P B I H E W A O N

slide-24
SLIDE 24

Pr Proving bui buildHe dHeap Ru Running T Time

Proof the recurrence: Base Case: General Case:

slide-25
SLIDE 25

Pr Proving bui buildHe dHeap Ru Running T Time

From S(h) to RunningTime(n): S(h): Since h ≤ lg(n): RunningTime(n) ≤

slide-26
SLIDE 26

Hea Heap p Sort

Running Time? Why do we care about another sort?

5 15 9 25 4 6 7 20 11 16 12 14

4 5 6 15 9 7 20 16 25 14 12 11

1. 2. 3.

slide-27
SLIDE 27

A( A(no nothe her) ) thr hrowba wback k to CS 173…

Let R be an equivalence relation on us where (s, t) ∈ R if s and t have the same favorite among: { ___, ___, ____, ___, ____, }