- 106 Views
- Uploaded on
- Presentation posted in: General

Objectives

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

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

Data Structures: A Pseudocode Approach with C, Second Edition

Basic Concepts

- 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 heap structure and its two primary operations, reheap up and reheap down.
- Definition
- Maintenance Operations

Data Structures: A Pseudocode Approach with C, Second Edition

Basic Concepts

- Definition
- A heap is a binary tree structure with the following properties:
- 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.
- 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

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

Data Structures: A Pseudocode Approach with C, Second Edition

Data Structures: A Pseudocode Approach with C, Second Edition

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

Data Structures: A Pseudocode Approach with C, Second Edition

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

Data Structures: A Pseudocode Approach with C, Second Edition

Heap Implementation

- Heaps are usually implemented in an array structure. In this 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

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 +2

2. The parent of a node located at index i is located at (i -1)/2

3. 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

Data Structures: A Pseudocode Approach with C, Second Edition

Data Structures: A Pseudocode Approach with C, Second Edition

Data Structures: A Pseudocode Approach with C, Second Edition

(continued)

Data Structures: A Pseudocode Approach with C, Second Edition

Data Structures: A Pseudocode Approach with C, Second Edition

Data Structures: A Pseudocode Approach with C, Second Edition

Data Structures: A Pseudocode Approach with C, Second Edition

Data Structures: A Pseudocode Approach with C, Second Edition

Data Structures: A Pseudocode Approach with C, Second Edition

Data Structures: A Pseudocode Approach with C, Second Edition

Data Structures: A Pseudocode Approach with C, Second Edition

Data Structures: A Pseudocode Approach with C, Second Edition

Data Structures: A Pseudocode Approach with C, Second Edition

Data Structures: A Pseudocode Approach with C, Second Edition