26
Data Structures: A Pseudocode Approach with C, Second Edition 1 Objectives Upon completion you will be able to: Define and implement heap structures Understand the operation and use of the heap ADT Design and implement selection applications using a heap Design and implement priority queues using a heap Heap Heap

Objectives

Embed Size (px)

DESCRIPTION

Heap. Objectives. Upon completion you will be able to: Define and implement heap structures Understand the operation and use of the heap ADT Design and implement selection applications using a heap Design and implement priority queues using a heap. Basic Concepts. - PowerPoint PPT Presentation

Citation preview

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

Objectives

Upon completion you will be able to:

• Define and implement heap structures• Understand the operation and use of the heap ADT• Design and implement selection applications using a heap• Design and implement priority queues using a heap

HeapHeap

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

Basic Concepts

A heap is a binary tree whose left and right subtrees have values A heap is a binary tree whose left and right subtrees have values less than their parents. We begin with a discussion of the basic less than their parents. We begin with a discussion of the basic heap structure and its two primary operations, reheap up and heap structure and its two primary operations, reheap up and reheap down.reheap down.

• Definition• Maintenance Operations

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

Basic Concepts

DefinitionA heap is a binary tree structure with the following

properties:1. The tree is complete or nearly complete.2. The key value of each node is greater than or equal

to the key value in each of its descendents.

• The root of a heap is guaranteed to hold the largest node in the tree; its subtrees contain data that have lesser values.

• Heaps are often implemented in an array rather than a linked list.

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

Sometimes this structure is called a max-heap.

A min-heap is to create a heap which the key value in a node is less than the key values in all of its subtrees.

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

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

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

The reheap up operation reorders a “broken” heap by floating the last element up the tree until it is in its correct location in the heap.

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

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

Reheap Down Operation reorders a “broken” heap by pushing the root down the tree until it is in its correct position in the heap.

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

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

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

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

Heap Implementation

The relationship between a node and its children is fixed and can be calculated as shown below:1. For a node located at index i, its children are found at: a. Left child: 2i + 1 b. Right child: 2i + 22. The parent of a node located at index i is located at (i -1)/23. Given the index for a left child, j, its right sibling, if any, is found at j + 1. Conversely, given the index for a right child, k, its left sibling, which must exist, is found at k - 1.4. Given the size, n, of a completed heap, the location of the first leaf is (n/2) 5. Given the location of the first leaf element, the location of the last nonleaf element is one less.

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

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

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

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

(continued)

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

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

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

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

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

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

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

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

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

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