Download Heap1

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
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
1
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
2
Basic Concepts
Definition
A 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
3
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
4
Data Structures: A Pseudocode Approach with C, Second Edition
5
Data Structures: A Pseudocode Approach with C, Second Edition
6
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
7
Data Structures: A Pseudocode Approach with C, Second Edition
8
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
9
Data Structures: A Pseudocode Approach with C, Second Edition
10
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
11
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
12
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
(continued)
Data Structures: A Pseudocode Approach with C, Second Edition
16
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
Related documents