p p chapter 10 has several programming projects, including a project that uses heaps. p p this...

31
Chapter 10 has several programming projects, including a project that uses heaps . This presentation shows you what a heap is, and demonstrates two of the important heap algorithms. Heaps Data Structures Data Structures and Other Objects and Other Objects Using Java Using Java

Post on 19-Dec-2015

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: P p Chapter 10 has several programming projects, including a project that uses heaps. p p This presentation shows you what a heap is, and demonstrates

Chapter 10 has several programming projects, including a project that uses heaps.

This presentation shows you what a heap is, and demonstrates two of the important heap algorithms.

HeapsHeaps

Data StructuresData Structuresand Other Objectsand Other ObjectsUsing JavaUsing Java

Page 2: P p Chapter 10 has several programming projects, including a project that uses heaps. p p This presentation shows you what a heap is, and demonstrates

HeapsHeaps

A heap is a certain kind of complete binary tree.

Page 3: P p Chapter 10 has several programming projects, including a project that uses heaps. p p This presentation shows you what a heap is, and demonstrates

HeapsHeaps

A heap is a certain kind of complete binary tree.

When a completebinary tree is built,

its first node must bethe root.

When a completebinary tree is built,

its first node must bethe root.

Root

Page 4: P p Chapter 10 has several programming projects, including a project that uses heaps. p p This presentation shows you what a heap is, and demonstrates

HeapsHeaps

Complete binary tree.

Left childof theroot

The second node isalways the left child

of the root.

The second node isalways the left child

of the root.

Page 5: P p Chapter 10 has several programming projects, including a project that uses heaps. p p This presentation shows you what a heap is, and demonstrates

HeapsHeaps

Complete binary tree.

Right childof the

root

The third node isalways the right child

of the root.

The third node isalways the right child

of the root.

Page 6: P p Chapter 10 has several programming projects, including a project that uses heaps. p p This presentation shows you what a heap is, and demonstrates

HeapsHeaps

Complete binary tree.

The next nodesalways fill the next

level from left-to-right..

The next nodesalways fill the next

level from left-to-right..

Page 7: P p Chapter 10 has several programming projects, including a project that uses heaps. p p This presentation shows you what a heap is, and demonstrates

HeapsHeaps

Complete binary tree.

The next nodesalways fill the next

level from left-to-right.

The next nodesalways fill the next

level from left-to-right.

Page 8: P p Chapter 10 has several programming projects, including a project that uses heaps. p p This presentation shows you what a heap is, and demonstrates

HeapsHeaps

Complete binary tree.

The next nodesalways fill the next

level from left-to-right.

The next nodesalways fill the next

level from left-to-right.

Page 9: P p Chapter 10 has several programming projects, including a project that uses heaps. p p This presentation shows you what a heap is, and demonstrates

HeapsHeaps

Complete binary tree.

The next nodesalways fill the next

level from left-to-right.

The next nodesalways fill the next

level from left-to-right.

Page 10: P p Chapter 10 has several programming projects, including a project that uses heaps. p p This presentation shows you what a heap is, and demonstrates

HeapsHeaps

Complete binary tree.

Page 11: P p Chapter 10 has several programming projects, including a project that uses heaps. p p This presentation shows you what a heap is, and demonstrates

HeapsHeaps

A heap is a certain kind of complete binary tree.

Each node in a heapcontains a key thatcan be compared toother nodes' keys.

Each node in a heapcontains a key thatcan be compared toother nodes' keys.

19

4222127

23

45

35

Page 12: P p Chapter 10 has several programming projects, including a project that uses heaps. p p This presentation shows you what a heap is, and demonstrates

HeapsHeaps

A heap is a certain kind of complete binary tree.

The "heap property"requires that each

node's key is >= thekeys of its children

The "heap property"requires that each

node's key is >= thekeys of its children

19

4222127

23

45

35

Page 13: P p Chapter 10 has several programming projects, including a project that uses heaps. p p This presentation shows you what a heap is, and demonstrates

Adding a Node to a HeapAdding a Node to a Heap

Put the new node in the next available spot.

Push the new node upward, swapping with its parent until the new node reaches an acceptable location. 19

4222127

23

45

35

42

Page 14: P p Chapter 10 has several programming projects, including a project that uses heaps. p p This presentation shows you what a heap is, and demonstrates

Adding a Node to a HeapAdding a Node to a Heap

Put the new node in the next available spot.

Push the new node upward, swapping with its parent until the new node reaches an acceptable location. 19

4222142

23

45

35

27

Page 15: P p Chapter 10 has several programming projects, including a project that uses heaps. p p This presentation shows you what a heap is, and demonstrates

Adding a Node to a HeapAdding a Node to a Heap

Put the new node in the next available spot.

Push the new node upward, swapping with its parent until the new node reaches an acceptable location. 19

4222135

23

45

42

27

Page 16: P p Chapter 10 has several programming projects, including a project that uses heaps. p p This presentation shows you what a heap is, and demonstrates

Adding a Node to a HeapAdding a Node to a Heap

The parent has a key that is >= new node, or

The node reaches the root. The process of pushing the

new node upward is called reheapificationreheapification upward. 19

4222135

23

45

42

27

Page 17: P p Chapter 10 has several programming projects, including a project that uses heaps. p p This presentation shows you what a heap is, and demonstrates

Removing the Top of a HeapRemoving the Top of a Heap

Move the last node onto the root.

19

4222135

23

45

42

27

Page 18: P p Chapter 10 has several programming projects, including a project that uses heaps. p p This presentation shows you what a heap is, and demonstrates

Removing the Top of a HeapRemoving the Top of a Heap

Move the last node onto the root.

19

4222135

23

27

42

Page 19: P p Chapter 10 has several programming projects, including a project that uses heaps. p p This presentation shows you what a heap is, and demonstrates

Removing the Top of a HeapRemoving the Top of a Heap

Move the last node onto the root.

Push the out-of-place node downward, swapping with its larger child until the new node reaches an acceptable location.

19

4222135

23

27

42

Page 20: P p Chapter 10 has several programming projects, including a project that uses heaps. p p This presentation shows you what a heap is, and demonstrates

Removing the Top of a HeapRemoving the Top of a Heap

Move the last node onto the root.

Push the out-of-place node downward, swapping with its larger child until the new node reaches an acceptable location.

19

4222135

23

42

27

Page 21: P p Chapter 10 has several programming projects, including a project that uses heaps. p p This presentation shows you what a heap is, and demonstrates

Removing the Top of a HeapRemoving the Top of a Heap

Move the last node onto the root.

Push the out-of-place node downward, swapping with its larger child until the new node reaches an acceptable location.

19

4222127

23

42

35

Page 22: P p Chapter 10 has several programming projects, including a project that uses heaps. p p This presentation shows you what a heap is, and demonstrates

Removing the Top of a HeapRemoving the Top of a Heap

The children all have keys <= the out-of-place node, or

The node reaches the leaf.

The process of pushing the new node downward is called reheapificationreheapification downward.

19

4222127

23

42

35

Page 23: P p Chapter 10 has several programming projects, including a project that uses heaps. p p This presentation shows you what a heap is, and demonstrates

Implementing a HeapImplementing a Heap

We will store the We will store the data from the data from the nodes in a nodes in a partially-filled partially-filled array.array.

An array of dataAn array of data

2127

23

42

35

Page 24: P p Chapter 10 has several programming projects, including a project that uses heaps. p p This presentation shows you what a heap is, and demonstrates

Implementing a HeapImplementing a Heap

Data from the root Data from the root goes in thegoes in the first first location location of the of the array.array.

An array of dataAn array of data

2127

23

42

35

42

Page 25: P p Chapter 10 has several programming projects, including a project that uses heaps. p p This presentation shows you what a heap is, and demonstrates

Implementing a HeapImplementing a Heap

Data from the next Data from the next row goesrow goes in the in the next two array locations.

An array of dataAn array of data

2127

23

42

35

42 35 23

Page 26: P p Chapter 10 has several programming projects, including a project that uses heaps. p p This presentation shows you what a heap is, and demonstrates

Implementing a HeapImplementing a Heap

Data from the next Data from the next row goesrow goes in the in the next two array locations.

An array of dataAn array of data

2127

23

42

35

42 35 23 27 21

Page 27: P p Chapter 10 has several programming projects, including a project that uses heaps. p p This presentation shows you what a heap is, and demonstrates

Implementing a HeapImplementing a Heap

Data from the next Data from the next row goesrow goes in the in the next two array locations.

An array of dataAn array of data

2127

23

42

35

42 35 23 27 21

We don't care what's inWe don't care what's inthis part of the array.this part of the array.

Page 28: P p Chapter 10 has several programming projects, including a project that uses heaps. p p This presentation shows you what a heap is, and demonstrates

Important Points about the ImplementationImportant Points about the Implementation

The links between the tree's The links between the tree's nodes are not actually stored as nodes are not actually stored as pointers, or in any other way.pointers, or in any other way.

The only way we "know" that The only way we "know" that "the array is a tree" is from the "the array is a tree" is from the way we manipulate the data.way we manipulate the data.

An array of dataAn array of data

2127

23

42

35

42 35 23 27 21

Page 29: P p Chapter 10 has several programming projects, including a project that uses heaps. p p This presentation shows you what a heap is, and demonstrates

Important Points about the ImplementationImportant Points about the Implementation

If you know the index of a If you know the index of a node, then it is easy to figure node, then it is easy to figure out the indexes of that node's out the indexes of that node's parent and children. Formulas parent and children. Formulas are given in the book.are given in the book.

[0][0] [1] [2] [3] [4]

2127

23

42

35

42 35 23 27 21

Page 30: P p Chapter 10 has several programming projects, including a project that uses heaps. p p This presentation shows you what a heap is, and demonstrates

A heap is a complete binary tree, where the entry at each node is greater than or equal to the entries in its children.

To add an entry to a heap, place the new entry at the next available spot, and perform a reheapification upward.

To remove the biggest entry, move the last node onto the root, and perform a reheapification downward.

Summary Summary

Page 31: P p Chapter 10 has several programming projects, including a project that uses heaps. p p This presentation shows you what a heap is, and demonstrates

THE ENDTHE END

Presentation copyright 1999 Addison Wesley Longman,For use with Data Structures and Other Objects Using Javaby Michael Main.

Some artwork in the presentation is used with permission from Presentation Task Force(copyright New Vision Technologies Inc) and Corel Gallery Clipart Catalog (copyrightCorel Corporation, 3G Graphics Inc, Archive Arts, Cartesia Software, Image ClubGraphics Inc, One Mile Up Inc, TechPool Studios, Totem Graphics Inc).

Students and instructors who use Data Structures and Other Objects Using Java are welcometo use this presentation however they see fit, so long as this copyright notice remainsintact.