Chapter 9

33
Data Structures: A Pseudocode Approach with C, Second Edition 1 Chapter 9 Chapter 9 Objectives Define and implement heap structures Understand the operation and use of the heap ADT Heap Heap

description

Chapter 9. Heap. Objectives. Define and implement heap structures Understand the operation and use of the heap ADT. 9-1 Basic Concepts. - PowerPoint PPT Presentation

Transcript of Chapter 9

Page 1: Chapter   9

Data Structures: A Pseudocode Approach with C, Second Edition 1

Chapter 9Chapter 9

Objectives

• Define and implement heap structures• Understand the operation and use of the heap ADT

HeapHeap

Page 2: Chapter   9

Data Structures: A Pseudocode Approach with C, Second Edition 2

9-1 Basic Concepts

•A heap is a binary tree whose left and right subtrees have values less than A heap is a binary tree whose left and right subtrees have values less than their parents. The root of a heap is guaranteed to hold the largest node in their parents. The root of a heap is guaranteed to hold the largest node in the tree.the tree.

•Both the left and the right branches of the tree have the same properties. Both the left and the right branches of the tree have the same properties.

•Heaps are often implemented in an array rather than a linked list. When Heaps are often implemented in an array rather than a linked list. When arrays are used, we are able to calculate the location of the left and the arrays are used, we are able to calculate the location of the left and the right subtrees. Conversely, we can calculate the address of it’s parent. right subtrees. Conversely, we can calculate the address of it’s parent.

•The tree is complete or nearly complete. The key value of each node is The tree is complete or nearly complete. The key value of each node is greater than or equal to the key value in each of its descendents. This greater than or equal to the key value in each of its descendents. This structure is also called max- heap. structure is also called max- heap.

Page 3: Chapter   9

Data Structures: A Pseudocode Approach with C, Second Edition 3

Page 4: Chapter   9

Data Structures: A Pseudocode Approach with C, Second Edition 4

Page 5: Chapter   9

Data Structures: A Pseudocode Approach with C, Second Edition 5

Maintenance OperationsTwo basic maintenance operations are performed on a heap.

• Insert a heap

• Delete a heap

•Two basic algorithms are – Reheap Up and Reheap Down

Page 6: Chapter   9

Data Structures: A Pseudocode Approach with C, Second Edition 6

•The reheap up operation repairs the structure so that it is a heap by floating the last element up the tree until that element is in its correct location.

•Insertion takes place at a leaf at the first empty position. This may create a situation where the new node’s key is larger than that of its parent. If it is, the node is floated up the tree by exchanging the child and parent keys and data.

Page 7: Chapter   9

Data Structures: A Pseudocode Approach with C, Second Edition 7

Page 8: Chapter   9

Data Structures: A Pseudocode Approach with C, Second Edition 8

When we have a nearly complete binary tree that satisfies heap order property except in the root position. Reheap down operation reorders a broken heap by pushing the root down the tree until it is in correct position at the heap.

Page 9: Chapter   9

Data Structures: A Pseudocode Approach with C, Second Edition 9

Page 10: Chapter   9

Data Structures: A Pseudocode Approach with C, Second Edition 10

9-2 Heap Implementation

Heaps are usually implemented in an array structure. In this Heaps are usually implemented in an array structure. In this section we discuss and develop five heap algorithms.section we discuss and develop five heap algorithms.

• Reheap Up• Reheap Down• Build a Heap• Insert a Node into a Heap• Delete a Node from a Heap

Page 11: Chapter   9

Data Structures: A Pseudocode Approach with C, Second Edition 11

Page 12: Chapter   9

Data Structures: A Pseudocode Approach with C, Second Edition 12

Page 13: Chapter   9

Data Structures: A Pseudocode Approach with C, Second Edition 13

Page 14: Chapter   9

Data Structures: A Pseudocode Approach with C, Second Edition 14

(continued)

Page 15: Chapter   9

Data Structures: A Pseudocode Approach with C, Second Edition 15

Page 16: Chapter   9

Data Structures: A Pseudocode Approach with C, Second Edition 16

Page 17: Chapter   9

Data Structures: A Pseudocode Approach with C, Second Edition 17

Page 18: Chapter   9

Data Structures: A Pseudocode Approach with C, Second Edition 18

Page 19: Chapter   9

Data Structures: A Pseudocode Approach with C, Second Edition 19

Page 20: Chapter   9

Data Structures: A Pseudocode Approach with C, Second Edition 20

Page 21: Chapter   9

Data Structures: A Pseudocode Approach with C, Second Edition 21

9-3 Heap ADT

We begin with a discussion of the heap ADT design and then We begin with a discussion of the heap ADT design and then develop the C code for the five major functions developed in develop the C code for the five major functions developed in Section 9.2.Section 9.2.

• Heap Structure• Heap Algorithms

Page 22: Chapter   9

Data Structures: A Pseudocode Approach with C, Second Edition 22

Page 23: Chapter   9

Data Structures: A Pseudocode Approach with C, Second Edition 23

Page 24: Chapter   9

Data Structures: A Pseudocode Approach with C, Second Edition 24

Page 25: Chapter   9

Data Structures: A Pseudocode Approach with C, Second Edition 25

Page 26: Chapter   9

Data Structures: A Pseudocode Approach with C, Second Edition 26

Page 27: Chapter   9

Data Structures: A Pseudocode Approach with C, Second Edition 27

Page 28: Chapter   9

Data Structures: A Pseudocode Approach with C, Second Edition 28

Page 29: Chapter   9

Data Structures: A Pseudocode Approach with C, Second Edition 29

(continued)

Page 30: Chapter   9

Data Structures: A Pseudocode Approach with C, Second Edition 30

Page 31: Chapter   9

Data Structures: A Pseudocode Approach with C, Second Edition 31

Page 32: Chapter   9

Data Structures: A Pseudocode Approach with C, Second Edition 32

Page 33: Chapter   9

Data Structures: A Pseudocode Approach with C, Second Edition 33