Binary Heapshilltop.bradley.edu › ~young › CIS210old › topic11.pdf · A Binary Max-Heap? A...

78
CIS210 1 Topic Binary Heaps

Transcript of Binary Heapshilltop.bradley.edu › ~young › CIS210old › topic11.pdf · A Binary Max-Heap? A...

Page 1: Binary Heapshilltop.bradley.edu › ~young › CIS210old › topic11.pdf · A Binary Max-Heap? A special complete binary tree s.t. The search key contained in any node is greater

CIS210 1

Topic

Binary Heaps

Page 2: Binary Heapshilltop.bradley.edu › ~young › CIS210old › topic11.pdf · A Binary Max-Heap? A special complete binary tree s.t. The search key contained in any node is greater

CIS210 2

Binary Heaps

as Non-linear Data Structures

Page 3: Binary Heapshilltop.bradley.edu › ~young › CIS210old › topic11.pdf · A Binary Max-Heap? A special complete binary tree s.t. The search key contained in any node is greater

CIS210 3

A Binary Heap?

A binary tree that satisfies two conditions:

The shape condition

The (partial) order condition between every

node and the nodes in its left and right

subtrees.

Heap order property

Page 4: Binary Heapshilltop.bradley.edu › ~young › CIS210old › topic11.pdf · A Binary Max-Heap? A special complete binary tree s.t. The search key contained in any node is greater

CIS210 4

A Binary Heap

TrightTleft

root

+ +Heap

Order

Condition Shape

Condition

Page 5: Binary Heapshilltop.bradley.edu › ~young › CIS210old › topic11.pdf · A Binary Max-Heap? A special complete binary tree s.t. The search key contained in any node is greater

CIS210 5

1. The Shape Condition of Heaps

The binary tree must be a complete binary

tree.

Page 6: Binary Heapshilltop.bradley.edu › ~young › CIS210old › topic11.pdf · A Binary Max-Heap? A special complete binary tree s.t. The search key contained in any node is greater

CIS210 6

2. The Order Condition of Heaps

There is a necessary ordering relationship

between a node and its children.

Each node in a tree has a key which is more

extreme (greater or less) than or equal to the

key of its parent.

Note:

There is no necessary ordering relationship

between a node and its siblings!

Page 7: Binary Heapshilltop.bradley.edu › ~young › CIS210old › topic11.pdf · A Binary Max-Heap? A special complete binary tree s.t. The search key contained in any node is greater

CIS210 7

The Order Condition of Heaps

For every node in the binary tree, the value

stored in that node must be greater than or

equal to the value in each of its children.

A max-heap

For every node in the binary tree, the value

stored in that node must be less than or equal

to the value in each of its children.

A min-heap

Page 8: Binary Heapshilltop.bradley.edu › ~young › CIS210old › topic11.pdf · A Binary Max-Heap? A special complete binary tree s.t. The search key contained in any node is greater

CIS210 8

A Binary Max-Heap?

A special complete binary tree s.t.

The search key contained in any node is greater

(larger) than or equal to the search key in each of its

children.

The search key contained in any node is never less

(smaller) than the search key in each of its children.

Every level except the deepest must contain as many

nodes as possible, and at the deepest level, all the

nodes are as far left as possible.

The root node has the largest key!

Page 9: Binary Heapshilltop.bradley.edu › ~young › CIS210old › topic11.pdf · A Binary Max-Heap? A special complete binary tree s.t. The search key contained in any node is greater

CIS210 9

Logical Structure of Binary Max-Heaps

TrightTleft

root

Page 10: Binary Heapshilltop.bradley.edu › ~young › CIS210old › topic11.pdf · A Binary Max-Heap? A special complete binary tree s.t. The search key contained in any node is greater

CIS210 10

Example: Binary Max Heap?

10

9 6

3 2 5

10

9 6

3

10

9

Page 11: Binary Heapshilltop.bradley.edu › ~young › CIS210old › topic11.pdf · A Binary Max-Heap? A special complete binary tree s.t. The search key contained in any node is greater

CIS210 11

A Binary Min-Heap?

A special complete binary tree s.t.

The search key contained in any node is less (smaller)

than or equal to the search key in each of its children.

The search key contained in any node is never greater

(larger) than the search key in each of its children.

Every level except the deepest must contain as many

nodes as possible, and at the deepest level, all the

nodes are as far left as possible.

The root node has the smallest key!

Page 12: Binary Heapshilltop.bradley.edu › ~young › CIS210old › topic11.pdf · A Binary Max-Heap? A special complete binary tree s.t. The search key contained in any node is greater

CIS210 12

Logical Structure of Binary Min-Heaps

TrightTleft

root

Page 13: Binary Heapshilltop.bradley.edu › ~young › CIS210old › topic11.pdf · A Binary Max-Heap? A special complete binary tree s.t. The search key contained in any node is greater

CIS210 13

Example:

2

3 6

10 5 9

2

3 6

10

2

3

Page 14: Binary Heapshilltop.bradley.edu › ~young › CIS210old › topic11.pdf · A Binary Max-Heap? A special complete binary tree s.t. The search key contained in any node is greater

CIS210 14

The World of Binary Heaps

Binary Trees

Complete

Binary Trees

(Binary)

HeapsHeap-order

Property

Shape PropertyMin-Heaps

Max-Heaps

Page 15: Binary Heapshilltop.bradley.edu › ~young › CIS210old › topic11.pdf · A Binary Max-Heap? A special complete binary tree s.t. The search key contained in any node is greater

CIS210 15

How to Represent a Binary Heap?

A heap is a complete binary tree!

An efficient array-based representation

Represent a node in the complete binary tree

as

A data

Represent the complete binary tree as an array.

Page 16: Binary Heapshilltop.bradley.edu › ~young › CIS210old › topic11.pdf · A Binary Max-Heap? A special complete binary tree s.t. The search key contained in any node is greater

CIS210 16

An Array-Based Representation of a Binary Heap

0

1

2

3

4

5

.

.

.

Data

10

9

6

3

2

5

Root

Heap[0]

Heap

10

9 6

3 2 5

Page 17: Binary Heapshilltop.bradley.edu › ~young › CIS210old › topic11.pdf · A Binary Max-Heap? A special complete binary tree s.t. The search key contained in any node is greater

CIS210 17

An Array-Based Representation of a Binary Heap

const int MAX_NODES = 100;

typedef string DataType;

DataType Heap[MAX_NODES];

int Root;

Page 18: Binary Heapshilltop.bradley.edu › ~young › CIS210old › topic11.pdf · A Binary Max-Heap? A special complete binary tree s.t. The search key contained in any node is greater

CIS210 18

Finding Parent and Children in a Binary Heap

For any node Heap[ i ]

Its left child =

Heap [ 2 * i + 1 ]

Its right child =

Heap [ 2 * i + 2 ]

Its parent =

Heap [ (i -1) / 2 ]

Page 19: Binary Heapshilltop.bradley.edu › ~young › CIS210old › topic11.pdf · A Binary Max-Heap? A special complete binary tree s.t. The search key contained in any node is greater

CIS210 19

Operations on Binary Max-heaps

Create an empty heap.

Destroy a heap.

Insert (add) a new item to the heap.

Retrieve and then delete a heap’s root item.

(This item has the largest search key.)

Determine whether a heap empty?

...

Page 20: Binary Heapshilltop.bradley.edu › ~young › CIS210old › topic11.pdf · A Binary Max-Heap? A special complete binary tree s.t. The search key contained in any node is greater

CIS210 20

Insert an Item Into a Binary Max-heap

9

5 6

3 2

9

5 6

3 2 15

Insert 15

Heap:

Page 21: Binary Heapshilltop.bradley.edu › ~young › CIS210old › topic11.pdf · A Binary Max-Heap? A special complete binary tree s.t. The search key contained in any node is greater

CIS210 21

Example: Insert 15

9

5 15

3 2 6

9

5 6

3 2 15

15

5 9

3 2 6

Heapify-Up Heapify-Up

Page 22: Binary Heapshilltop.bradley.edu › ~young › CIS210old › topic11.pdf · A Binary Max-Heap? A special complete binary tree s.t. The search key contained in any node is greater

CIS210 22

Heapify-Up

Heapify-Up (Heap, root, bottom):

The heap order may be violated only at the

bottom node.

Restores the heap order between the root and

the bottom.

Page 23: Binary Heapshilltop.bradley.edu › ~young › CIS210old › topic11.pdf · A Binary Max-Heap? A special complete binary tree s.t. The search key contained in any node is greater

CIS210 23

Heapify-Up

Heapify-Up (Heap, root, bottom):

If bottom > root then

Set Parent to the index of bottom node;

If heap[Parent] < heap[bottom] then

• Swap heap[Parent] with heap[bottom];

• Heapify-Up (Heap, root, Parent);

Page 24: Binary Heapshilltop.bradley.edu › ~young › CIS210old › topic11.pdf · A Binary Max-Heap? A special complete binary tree s.t. The search key contained in any node is greater

CIS210 24

Insert an Item Into a Binary Max-heap

Insert (Heap, item):

Increment the size of the heap;

Put item in the next available position;

Heapify-Up (Heap, 0, size-1);

Page 25: Binary Heapshilltop.bradley.edu › ~young › CIS210old › topic11.pdf · A Binary Max-Heap? A special complete binary tree s.t. The search key contained in any node is greater

CIS210 25

Example: Insert 15

9

5 6

3 2

9

5 6

3 2 150

1

2

3

4

5

.

.

.

95632

0

1

2

3

4

5

.

.

.

9563215

Page 26: Binary Heapshilltop.bradley.edu › ~young › CIS210old › topic11.pdf · A Binary Max-Heap? A special complete binary tree s.t. The search key contained in any node is greater

CIS210 26

Example: Insert 15

9

5 15

3 2 6

9

5 6

3 2 15

15

5 9

3 2 6

0

1

2

3

4

5

.

.

.

9563215

0

1

2

3

4

5

.

.

.

9515326

0

1

2

3

4

5

.

.

.

1559326

Heapify-Up Heapify-Up

Page 27: Binary Heapshilltop.bradley.edu › ~young › CIS210old › topic11.pdf · A Binary Max-Heap? A special complete binary tree s.t. The search key contained in any node is greater

CIS210 27

Retrieve & Delete a heap’s root item

10

9 6

3 2 5

9 6

3 2 5

Delete the root

Page 28: Binary Heapshilltop.bradley.edu › ~young › CIS210old › topic11.pdf · A Binary Max-Heap? A special complete binary tree s.t. The search key contained in any node is greater

CIS210 28

Example: Delete the root

9 6

3 2 5

5

9 6

3 2 5

9

5 6

3 2

Heapify-Down

Page 29: Binary Heapshilltop.bradley.edu › ~young › CIS210old › topic11.pdf · A Binary Max-Heap? A special complete binary tree s.t. The search key contained in any node is greater

CIS210 29

Heapify-Down

Heapify-Down (Heap, root, bottom):

The heap order may be violated only at the

root node.

Restores the heap order between the root and

the bottom.

Page 30: Binary Heapshilltop.bradley.edu › ~young › CIS210old › topic11.pdf · A Binary Max-Heap? A special complete binary tree s.t. The search key contained in any node is greater

CIS210 30

Heapify-Down

Heapify-Down (Heap, root, bottom):

If root is not a leaf then

Set BiggerChild to the index of child with

larger value.

If heap[root] < heap[BiggerChild] then

• Swap heap[root] with heap[BiggerChild];

• Heapify-Down (BiggerChild, bottom);

Page 31: Binary Heapshilltop.bradley.edu › ~young › CIS210old › topic11.pdf · A Binary Max-Heap? A special complete binary tree s.t. The search key contained in any node is greater

CIS210 31

Retrieve & Delete a heap’s root item

DeleteTheRoot (Heap):

Delete the root value;

Move the last leaf value into the root position;

Decrement the size of the heap;

Heapify-Down (Heap, 0, size-1);

Page 32: Binary Heapshilltop.bradley.edu › ~young › CIS210old › topic11.pdf · A Binary Max-Heap? A special complete binary tree s.t. The search key contained in any node is greater

CIS210 32

Example: Delete the root

0

1

2

3

4

5

.

.

.

96325

10

9 6

3 2 5

9 6

3 2 50

1

2

3

4

5

.

.

.

1096325

Page 33: Binary Heapshilltop.bradley.edu › ~young › CIS210old › topic11.pdf · A Binary Max-Heap? A special complete binary tree s.t. The search key contained in any node is greater

CIS210 33

Example: Delete the root

0

1

2

3

4

5

.

.

.

95632

9 6

3 2 5

5

9 6

3 2 5

9

5 6

3 2

0

1

2

3

4

5

.

.

.

96325

0

1

2

3

4

5

.

.

.

596325

Heapify-Down

Page 34: Binary Heapshilltop.bradley.edu › ~young › CIS210old › topic11.pdf · A Binary Max-Heap? A special complete binary tree s.t. The search key contained in any node is greater

CIS210 34

Heap ADT

//--------------------------------------------------------------------

// heap.h

// Class declaration for the array implementation of the Heap ADT

//--------------------------------------------------------------------

using namespace std;

const int defMaxHeapSize = 10; // Default maximum heap size

Page 35: Binary Heapshilltop.bradley.edu › ~young › CIS210old › topic11.pdf · A Binary Max-Heap? A special complete binary tree s.t. The search key contained in any node is greater

CIS210 35

Heap ADT

template < class DT >

class Heap

{

public:

// Constructor

Heap ( int maxNumber = defMaxHeapSize );

// Destructor

~Heap ();

Page 36: Binary Heapshilltop.bradley.edu › ~young › CIS210old › topic11.pdf · A Binary Max-Heap? A special complete binary tree s.t. The search key contained in any node is greater

CIS210 36

Heap ADT

// Heap manipulation operations

void insert ( const DT &newElement ); // Insert element

DT removeMax (); // Remove max pty element

void clear (); // Clear heap

// Heap status operations

bool isEmpty () const; // Heap is empty

bool isFull () const; // Heap is full

Page 37: Binary Heapshilltop.bradley.edu › ~young › CIS210old › topic11.pdf · A Binary Max-Heap? A special complete binary tree s.t. The search key contained in any node is greater

CIS210 37

Heap ADT

// Output the heap structure -- used in testing/debugging

void showStructure () const;

void writeLevels () const; // Output in level order

Page 38: Binary Heapshilltop.bradley.edu › ~young › CIS210old › topic11.pdf · A Binary Max-Heap? A special complete binary tree s.t. The search key contained in any node is greater

CIS210 38

Heap ADT

private:

// Data members

int maxSize, // Maximum number of elements in the heap

size; // Actual number of elements in the heap

DT *dataItems; // Array containing the heap elements

};

Page 39: Binary Heapshilltop.bradley.edu › ~young › CIS210old › topic11.pdf · A Binary Max-Heap? A special complete binary tree s.t. The search key contained in any node is greater

CIS210 39

Binary Heap Operations -Analysis

The height of a complete binary tree with N nodes is

O(log N)

Worst-case running time:

Heapify-Up

O(log N)

Insert

O(log N)

Heapify-Down

O(log N)

DeletetheRoot

O(log N)

Page 40: Binary Heapshilltop.bradley.edu › ~young › CIS210old › topic11.pdf · A Binary Max-Heap? A special complete binary tree s.t. The search key contained in any node is greater

CIS210 40

Transform an Array To a

Binary (Max- or Min-) Heap

Page 41: Binary Heapshilltop.bradley.edu › ~young › CIS210old › topic11.pdf · A Binary Max-Heap? A special complete binary tree s.t. The search key contained in any node is greater

CIS210 41

Build a Binary Max-Heap

Convert an array into a max-heap.

6 3 5 9 2 10

0 1 2 3 4 5

10 9 6 3 2 5

0 1 2 3 4 5

Build a max-heap

10 6 9 3 2 5

0 1 2 3 4 5

Page 42: Binary Heapshilltop.bradley.edu › ~young › CIS210old › topic11.pdf · A Binary Max-Heap? A special complete binary tree s.t. The search key contained in any node is greater

CIS210 42

Building a Binary Max-Heap -Approach 1

Approach 1:

N inserts (Heapify-Up)

Top-down heap building

Page 43: Binary Heapshilltop.bradley.edu › ~young › CIS210old › topic11.pdf · A Binary Max-Heap? A special complete binary tree s.t. The search key contained in any node is greater

CIS210 43

Example: Transform an Array into a Binary Max-Heap -

Approach 1

6 3 5 9 2 10

0 1 2 3 4 5

10 6 9 3 2 5

0 1 2 3 4 5

?

Page 44: Binary Heapshilltop.bradley.edu › ~young › CIS210old › topic11.pdf · A Binary Max-Heap? A special complete binary tree s.t. The search key contained in any node is greater

CIS210 44

Example: Approach 1

6 3 5 9 2 10

0 1 2 3 4 5

6 3 5 9 2 10

0 1 2 3 4 5

6

Insert

Insert

Page 45: Binary Heapshilltop.bradley.edu › ~young › CIS210old › topic11.pdf · A Binary Max-Heap? A special complete binary tree s.t. The search key contained in any node is greater

CIS210 45

Example: Approach 1

6 3 5 9 2 10

0 1 2 3 4 5

6 3 5 9 2 10

0 1 2 3 4 5

6

3 5

Insert

6

3

Insert

Page 46: Binary Heapshilltop.bradley.edu › ~young › CIS210old › topic11.pdf · A Binary Max-Heap? A special complete binary tree s.t. The search key contained in any node is greater

CIS210 46

Example: Approach 1

9

6 5

3

9 6 5 3 2 10

0 1 2 3 4 5

Insert

2

9

6 5

3

9 6 5 3 2 10

0 1 2 3 4 5

Insert

Page 47: Binary Heapshilltop.bradley.edu › ~young › CIS210old › topic11.pdf · A Binary Max-Heap? A special complete binary tree s.t. The search key contained in any node is greater

CIS210 47

Example: Approach 1

10

6 9

3 2 5

10 6 9 3 2 5

0 1 2 3 4 5

Page 48: Binary Heapshilltop.bradley.edu › ~young › CIS210old › topic11.pdf · A Binary Max-Heap? A special complete binary tree s.t. The search key contained in any node is greater

CIS210 48

Quiz: Top-down Construction of a Binary Min-Heap?

6 3 5 9 2 10

0 1 2 3 4 5

2 3 5 9 6 10

0 1 2 3 4 5

?

Page 49: Binary Heapshilltop.bradley.edu › ~young › CIS210old › topic11.pdf · A Binary Max-Heap? A special complete binary tree s.t. The search key contained in any node is greater

CIS210 49

Building a Binary Heap-Approach 2

Approach 2:

Using Heapify-Down

Bottom-up heap building

Page 50: Binary Heapshilltop.bradley.edu › ~young › CIS210old › topic11.pdf · A Binary Max-Heap? A special complete binary tree s.t. The search key contained in any node is greater

CIS210 50

Example: Transform an Array into a Binary Max-Heap -

Approach 2

6 3 5 9 2 10

0 1 2 3 4 5

10 9 6 3 2 5

0 1 2 3 4 5

?

Page 51: Binary Heapshilltop.bradley.edu › ~young › CIS210old › topic11.pdf · A Binary Max-Heap? A special complete binary tree s.t. The search key contained in any node is greater

CIS210 51

Example: Transform an Array into a Binary Max-Heap -

Approach 2

6 3 5 9 2 10

0 1 2 3 4 5

6 3 5 9 2 10

0 1 2 3 4 5

6

3 5

9 2 10

6

3 5

9 2 10

Heapify-Down

Page 52: Binary Heapshilltop.bradley.edu › ~young › CIS210old › topic11.pdf · A Binary Max-Heap? A special complete binary tree s.t. The search key contained in any node is greater

CIS210 52

Example: Approach 2

6 3 5 9 2 10

0 1 2 3 4 5

6 3 5 9 2 10

0 1 2 3 4 5

6

3 5

9 2 10

6

3 5

9 2 10

Heapify-Down

Page 53: Binary Heapshilltop.bradley.edu › ~young › CIS210old › topic11.pdf · A Binary Max-Heap? A special complete binary tree s.t. The search key contained in any node is greater

CIS210 53

Example: Approach 2

6 3 5 9 2 10

0 1 2 3 4 5

6 3 5 9 2 10

0 1 2 3 4 5

6

3 5

9 2 10

6

3 5

9 2 10

Heapify-Down

Page 54: Binary Heapshilltop.bradley.edu › ~young › CIS210old › topic11.pdf · A Binary Max-Heap? A special complete binary tree s.t. The search key contained in any node is greater

CIS210 54

Example: Approach 2

6 3 5 9 2 10

0 1 2 3 4 5

6 3 10 9 2 5

0 1 2 3 4 5

6

3 5

9 2 10

6

3 10

9 2 5

Heapify-Down

Page 55: Binary Heapshilltop.bradley.edu › ~young › CIS210old › topic11.pdf · A Binary Max-Heap? A special complete binary tree s.t. The search key contained in any node is greater

CIS210 55

Example: Approach 2

6 3 10 9 2 5

0 1 2 3 4 5

6 9 10 3 2 5

0 1 2 3 4 5

6

3 10

9 2 5

6

9 10

3 2 5

Heapify-Down

Page 56: Binary Heapshilltop.bradley.edu › ~young › CIS210old › topic11.pdf · A Binary Max-Heap? A special complete binary tree s.t. The search key contained in any node is greater

CIS210 56

Example: Approach 2

6 9 10 3 2 5

0 1 2 3 4 5

10 9 6 3 2 5

0 1 2 3 4 5

6

9 10

9 2 5

10

9 6

3 2 5

Heap!

Heapify-Down

Page 57: Binary Heapshilltop.bradley.edu › ~young › CIS210old › topic11.pdf · A Binary Max-Heap? A special complete binary tree s.t. The search key contained in any node is greater

CIS210 57

Quiz: Bottom-up Construction of a Binary Min-Heap?

6 3 5 9 2 10

0 1 2 3 4 5

2 3 5 9 6 10

0 1 2 3 4 5

?

Page 58: Binary Heapshilltop.bradley.edu › ~young › CIS210old › topic11.pdf · A Binary Max-Heap? A special complete binary tree s.t. The search key contained in any node is greater

CIS210 58

Build a Binary Heap - Analysis

Top-down Approach 1:

N inserts (Heapify-Up)

Worst-case running time

O(N log N)

Page 59: Binary Heapshilltop.bradley.edu › ~young › CIS210old › topic11.pdf · A Binary Max-Heap? A special complete binary tree s.t. The search key contained in any node is greater

CIS210 59

Build a Binary Heap - Analysis

Bottom-up Approach 2:

N Heapify-Down

Worst-case running time

O(N log N)

O(N) linear time!

Page 60: Binary Heapshilltop.bradley.edu › ~young › CIS210old › topic11.pdf · A Binary Max-Heap? A special complete binary tree s.t. The search key contained in any node is greater

CIS210 60

Applications of

Heaps

Page 61: Binary Heapshilltop.bradley.edu › ~young › CIS210old › topic11.pdf · A Binary Max-Heap? A special complete binary tree s.t. The search key contained in any node is greater

CIS210 61

Applications of Heaps

Priority queues

Sorting

Page 62: Binary Heapshilltop.bradley.edu › ~young › CIS210old › topic11.pdf · A Binary Max-Heap? A special complete binary tree s.t. The search key contained in any node is greater

CIS210 62

A Better Data Structure for PQ?

Worst-case time complexity of a heap-based

implementation of PQ:

Enqueue

O(log N)

Dequeue

O(log N)

Page 63: Binary Heapshilltop.bradley.edu › ~young › CIS210old › topic11.pdf · A Binary Max-Heap? A special complete binary tree s.t. The search key contained in any node is greater

CIS210 63

What is Heap Sort?

A sorting algorithm using an advanced data

structure called a heap!

Page 64: Binary Heapshilltop.bradley.edu › ~young › CIS210old › topic11.pdf · A Binary Max-Heap? A special complete binary tree s.t. The search key contained in any node is greater

CIS210 64

Heap Sort

Convert an array of unsorted data into a max-heap.

Take the root element from the heap and put it into its place.

Re-heap the remaining elements

via the reheapfication downward, i.e. Heapify-Down!

Repeat until all elements are in the correct positions.

Page 65: Binary Heapshilltop.bradley.edu › ~young › CIS210old › topic11.pdf · A Binary Max-Heap? A special complete binary tree s.t. The search key contained in any node is greater

CIS210 65

Heap Sort

Sorted

In an ascending orderHeapify-Down

A diminishing heap & A growing sorted array

An unsorted array

A Max-Heap

Page 66: Binary Heapshilltop.bradley.edu › ~young › CIS210old › topic11.pdf · A Binary Max-Heap? A special complete binary tree s.t. The search key contained in any node is greater

CIS210 66

Example: Heap Sort

6 3 5 9 2 10

0 1 2 3 4 5

2 3 5 6 9 10

0 1 2 3 4 5

Sorting?

Page 67: Binary Heapshilltop.bradley.edu › ~young › CIS210old › topic11.pdf · A Binary Max-Heap? A special complete binary tree s.t. The search key contained in any node is greater

CIS210 67

Example: Transform an Array into a Heap

6 9 10 3 2 5

0 1 2 3 4 5

10 9 6 3 2 5

0 1 2 3 4 5

6

9 10

9 2 5

10

9 6

3 2 5

Max-Heap

Page 68: Binary Heapshilltop.bradley.edu › ~young › CIS210old › topic11.pdf · A Binary Max-Heap? A special complete binary tree s.t. The search key contained in any node is greater

CIS210 68

Example: Heap Sort

10 9 6 3 2 5

5 9 6 3 2 10

9 5 6 3 2 10

Max-Heap

Page 69: Binary Heapshilltop.bradley.edu › ~young › CIS210old › topic11.pdf · A Binary Max-Heap? A special complete binary tree s.t. The search key contained in any node is greater

CIS210 69

Example: Heap Sort

9 5 6 3 2 10

2 5 6 3 9 10

6 5 2 3 9 10

Page 70: Binary Heapshilltop.bradley.edu › ~young › CIS210old › topic11.pdf · A Binary Max-Heap? A special complete binary tree s.t. The search key contained in any node is greater

CIS210 70

Example: Heap Sort

6 5 2 3 9 10

3 5 2 6 9 10

5 3 2 6 9 10

Page 71: Binary Heapshilltop.bradley.edu › ~young › CIS210old › topic11.pdf · A Binary Max-Heap? A special complete binary tree s.t. The search key contained in any node is greater

CIS210 71

Example: Heap Sort

5 3 2 6 9 10

2 3 5 6 9 10

3 2 5 6 9 10

Page 72: Binary Heapshilltop.bradley.edu › ~young › CIS210old › topic11.pdf · A Binary Max-Heap? A special complete binary tree s.t. The search key contained in any node is greater

CIS210 72

Example: Heap Sort

3 2 5 6 9 10

2 3 5 6 9 10

2 3 5 6 9 10

Page 73: Binary Heapshilltop.bradley.edu › ~young › CIS210old › topic11.pdf · A Binary Max-Heap? A special complete binary tree s.t. The search key contained in any node is greater

CIS210 73

Example: Heap Sort

2 3 5 6 9 10

2 3 5 6 9 10

Sorted!

Page 74: Binary Heapshilltop.bradley.edu › ~young › CIS210old › topic11.pdf · A Binary Max-Heap? A special complete binary tree s.t. The search key contained in any node is greater

CIS210 74

Quiz: Heap Sort?

6 3 5 9 2 10

0 1 2 3 4 5

10 9 6 5 3 2

0 1 2 3 4 5

Sorting?

Page 75: Binary Heapshilltop.bradley.edu › ~young › CIS210old › topic11.pdf · A Binary Max-Heap? A special complete binary tree s.t. The search key contained in any node is greater

CIS210 75

Heap Sort

template < class DT >

void heapSort ( DT dataItems [], int size )

// Heap sort routine. Sorts the data items in the array in ascending

// order based on priority.

{

DT temp; // Temporary storage

int j; // Loop counter

// Build successively larger heaps within the array until the

// entire array is a heap.

for ( j = (size-1)/2 ; j >= 0 ; j-- )

moveDown(dataItems,j,size);

Page 76: Binary Heapshilltop.bradley.edu › ~young › CIS210old › topic11.pdf · A Binary Max-Heap? A special complete binary tree s.t. The search key contained in any node is greater

CIS210 76

Heap Sort

// Swap the root data item from each successively smaller heap with

// the last unsorted data item in the array. Restore the heap after

// each exchange.

for ( j = size-1 ; j > 0 ; j-- )

{

temp = dataItems[j];

dataItems[j] = dataItems[0];

dataItems[0] = temp;

moveDown(dataItems,0,j);

}

}

Page 77: Binary Heapshilltop.bradley.edu › ~young › CIS210old › topic11.pdf · A Binary Max-Heap? A special complete binary tree s.t. The search key contained in any node is greater

CIS210 77

Heap Sort

template < class DT >

void moveDown ( DT dataItems [], int root, int size )

// Restores the binary tree that is rooted at root to a heap by moving

// dataItems[root] downward until the tree satisfies the heap property.

// Parameter size is the number of data items in the array.

{

DT insertDataItem; // Data item to be inserted -- dataItems[root]

int stop, // (Re)insertion point found

j; // Array index that moves down the heap

insertDataItem = dataItems[root];

stop = 0;

j = 2*root+1;

while ( j < size && !stop )

{

if ( j < size-1 &&

dataItems[j].pty() < dataItems[j+1].pty() )

j++;

if ( insertDataItem.pty() >= dataItems[j].pty() )

stop = 1;

else

{

dataItems[(j-1)/2] = dataItems[j];

j = 2*j+1;

}

}

dataItems[(j-1)/2] = insertDataItem;

}

Page 78: Binary Heapshilltop.bradley.edu › ~young › CIS210old › topic11.pdf · A Binary Max-Heap? A special complete binary tree s.t. The search key contained in any node is greater

CIS210 78

Heap Sort - Analysis

The worst-case running time

O(n log n)

The best-case running time

O(n log n)

The average-case running time

O(n log n)