Download presentation
Presentation is loading. Please wait.
Published byBerniece McLaughlin Modified over 6 years ago
1
Heap Chapter 9 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
2
9-1 Basic Concepts Definition Maintenance Operations
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
3
Introduction heaps have a meaning for the left and right subtrees
the root of a heap is guaranteed to hold the largest node in the tree its subtrees contain data that have lesser values unlike the binary search tree, the smaller nodes of a heap can be placed on either the right or left subtree Data Structures: A Pseudocode Approach with C, Second Edition
4
Introduction (2) therefore, both the left and right branches of the tree have the same meaning heaps has another interesting facet: they are often implemented in an array rather than a linked list when we implement a binary tree in an array, we can calculate the location of the right and left subtrees conversely, given the address of a node, we can calculate the address of its parent Data Structures: A Pseudocode Approach with C, Second Edition
5
Heap 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 Data Structures: A Pseudocode Approach with C, Second Edition
6
Heap Definition (2) Sometimes heap structure called a max-heap
The second property of a heap, the key values is greater than the keys of the subtrees, can be reversed to create a min-heap Data Structures: A Pseudocode Approach with C, Second Edition
7
Data Structures: A Pseudocode Approach with C, Second Edition
8
Heap Structure Data Structures: A Pseudocode Approach with C, Second Edition
9
Data Structures: A Pseudocode Approach with C, Second Edition
10
Basic Heap Algorithms There are 2 basic maintenance operations that are performed on a heap Insert a node Delete a node Although the heap structure is a tree, it is meaningless to traverse it, search it, or print it out To implement the insert and delete operations, we need 2 basic algorithms reheapUp reheapDown Data Structures: A Pseudocode Approach with C, Second Edition
11
ReheapUp Imagine that we have nearly complete binary tree with N elements whose first N-1 elements satisfy the order property of heaps, but the last element does not In other words, the structure would be a heap if the last element were not there To reheap up operation repairs 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
12
Data Structures: A Pseudocode Approach with C, Second Edition
13
Data Structures: A Pseudocode Approach with C, Second Edition
14
ReheapUp (2) Like the binary search tree, inserts into heaps take place at a leaf Furthermore, because the heap is a complete or nearly complete tree, the node must be placed in the last leaf level at the first (leftmost) empty position This creates the situation we see in figure 9-4 Data Structures: A Pseudocode Approach with C, Second Edition
15
We then exchange the nodes’ data
At the beginning, 25 > 12 we therefore exchange 25 and 12 and call reheapUp to test its current position in the heap Once again 25 > 21 We then exchange the nodes’ data This time, when reheapUP is called, the value of he current node’s key is less than the value of its parent indicating we have located the correct position and the operation and the operation stops Figure 9-5: rehaepUp Example Data Structures: A Pseudocode Approach with C, Second Edition
16
ReheapDown Imagine we have a nearly complete binary tree that satisfies the heap order property except in the root position This situation occurs when the root is deleted from the tree, leaving two disjoint heaps To correct the situation, we move the data in the last tree node to the root Obviously this action destroys the tree’s heap porperties Data Structures: A Pseudocode Approach with C, Second Edition
17
ReheapDown (2) To restore the heap property we need an operation that well sink the root down until it ends up in a position where the heap ordering property is satisfied (reheapDown) Reheap down repairs 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
18
Data Structures: A Pseudocode Approach with C, Second Edition
19
When we start the root (10) is smaller than its subtree
We examine them and select the larger of the two of exchange with the root (32) Having made the exchange (figure b), we check the subtrees to see if we are done and see that 10 is smaller than their keys Once again we exchange 10 with the larger of the subtree (30) At this point we have reached a leaf and are done Figure 9-7: reheapDown Data Structures: A Pseudocode Approach with C, Second Edition
20
Heap Data Structure Although a heap can be built in a dynamic tree structure, it is most often implemented in an array The relationship between a node and its children is fixed and can be calculate as follows: Data Structures: A Pseudocode Approach with C, Second Edition
21
Heap Data Structure (2) For a node located at index, i, its children are found at: a. left child: 2i + 1 b. right child: 2i +2 For a node located at index i, its parent is located at (i-1) / 2 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, is left sibling, which must exist, is found at k – 1 Given the size, n, of a complete heap, the location of the first leaf is n / 2. 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
22
9-2 Heap Implementation Reheap Up Reheap Down Build a Heap
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
23
Data Structures: A Pseudocode Approach with C, Second Edition
24
Heap Data Structure (3) The index of 32 is 2,
- so the index of its left child, 23, is 2 * = 5 - The index of its right child, 19, is 2 * 2 +2 = 6 The index of 8 is 4, so the index ofiits parent, 56, is (4-1) / 2 = 1 In Relationship 1 we found the address of the left and right child. To find the right child, we could have also used the location of the left child (5) and added 1 The total number of elements is 7, so the index of the first leaf element, 45, is (7-1)/2 = 3 The location of the last nonleaf element, 32, is 3-1 = 2 Data Structures: A Pseudocode Approach with C, Second Edition
25
Heap Algorithms There are 2 ways to build a heap
We can start with an empty array and insert elements into the array one at a time After reheapUp and reheapDown are examined, then follow by examination of the logic for deleting data from heap Data Structures: A Pseudocode Approach with C, Second Edition
26
Data Structures: A Pseudocode Approach with C, Second Edition
27
Data Structures: A Pseudocode Approach with C, Second Edition
28
(continued) Data Structures: A Pseudocode Approach with C, Second Edition
29
Data Structures: A Pseudocode Approach with C, Second Edition
30
Data Structures: A Pseudocode Approach with C, Second Edition
31
Data Structures: A Pseudocode Approach with C, Second Edition
32
Data Structures: A Pseudocode Approach with C, Second Edition
33
Data Structures: A Pseudocode Approach with C, Second Edition
34
Data Structures: A Pseudocode Approach with C, Second Edition
35
9-3 Heap ADT Heap Structure Heap Algorithms
We begin with a discussion of the heap ADT design and then develop the C code for the five major functions developed in Section Basic functions, such as heap full, are not provided. Heap Structure Heap Algorithms Data Structures: A Pseudocode Approach with C, Second Edition
36
Data Structures: A Pseudocode Approach with C, Second Edition
37
Data Structures: A Pseudocode Approach with C, Second Edition
38
Data Structures: A Pseudocode Approach with C, Second Edition
39
Data Structures: A Pseudocode Approach with C, Second Edition
40
Data Structures: A Pseudocode Approach with C, Second Edition
41
Data Structures: A Pseudocode Approach with C, Second Edition
42
Data Structures: A Pseudocode Approach with C, Second Edition
43
Data Structures: A Pseudocode Approach with C, Second Edition
44
Data Structures: A Pseudocode Approach with C, Second Edition
45
Data Structures: A Pseudocode Approach with C, Second Edition
46
(continued) Data Structures: A Pseudocode Approach with C, Second Edition
47
Data Structures: A Pseudocode Approach with C, Second Edition
48
Data Structures: A Pseudocode Approach with C, Second Edition
49
Data Structures: A Pseudocode Approach with C, Second Edition
50
Data Structures: A Pseudocode Approach with C, Second Edition
51
9-4 Heap Applications Selection Algorithms Priority Queues
We discuss two of the three common heap applications-selection and priority queues. For selection applications, we develop a high-level algorithm. For priorityh queues, we develop the C code. Selection Algorithms Priority Queues Data Structures: A Pseudocode Approach with C, Second Edition
52
Selection Algorithms Given the problem to determine the kth element in an unsorted list We have 2 solutions We could first sort the list and select the element at location k or we could create a heap and deleted k –1 elements form it, leaving the desired element at the top Because we are studying heaps, let’s look at the second solution Data Structures: A Pseudocode Approach with C, Second Edition
53
Selection Algorithms (2)
The heap is first created Then we go about selecting the desired element To get to step 2, rather than simply discarding the elements at the top of heap, a better solution would be to place the deleted element at the end of the heap and reduce the heap size by one After the kth element has been processed, the temporarily removed elements can then be reinserted into the heap Data Structures: A Pseudocode Approach with C, Second Edition
54
Data Structures: A Pseudocode Approach with C, Second Edition
55
Data Structures: A Pseudocode Approach with C, Second Edition
56
Priority Queues The queue structure in chapter 4 uses a linear list in which each node travels serially through the queue It is not possible for any element to advance faster than the others While this may be very equitable, it is often not very realistic Often times, for various reasons, we want to give priority to one element over the others Data Structures: A Pseudocode Approach with C, Second Edition
57
Priority Queues (2) Example: consider the line waiting for show seats in one of the large Las Vegas casinos While most people wait in one long line, there is usually a celebrity line that is very short if not empty This line is reserved for special customers of the casino Data Structures: A Pseudocode Approach with C, Second Edition
58
Priority Queues (3) The heap is an excellent structure to use for a priority queue As an event enters the queue, it is given a priority number that determines its position relative to the other events already in the queue If it has the highest priority, it rises to the tip of the heap and becomes the next event to be processed If is has a low priority, it remains relatively low in the heap, waiting its turn Data Structures: A Pseudocode Approach with C, Second Edition
59
Priority Queues (4) The key in a priority queue must be carefully constructed to ensure that the queue works properly One common technique uses an encoded priority number that consists of the priority plus a sequential number representing the event’s place within the queue Data Structures: A Pseudocode Approach with C, Second Edition
60
Priority Queues (5) Example: given a queue with 5 priority classes,
we could construct a key in which the first digit of the priority number represented the queue priority, 1 through 5, and the rest of the number represented the serial placement within the priority However, because we are using a priority heap, the serial number must be in descending order, that is 999 down to 0 within each priority Data Structures: A Pseudocode Approach with C, Second Edition
61
Priority Queues (6) If we assume that there will be a maximum of 1000 events for any priority at any one time, then we could assign the lowest priority sequential numbers in the range 1999 down to 1000 The second lowest priority sequential numbers in the range 2999 to 2000 numbers in the third lowest priority numbers in the range 3999 to 3000, And so forth Data Structures: A Pseudocode Approach with C, Second Edition
62
Data Structures: A Pseudocode Approach with C, Second Edition
63
Data Structures: A Pseudocode Approach with C, Second Edition
64
Data Structures: A Pseudocode Approach with C, Second Edition
65
Data Structures: A Pseudocode Approach with C, Second Edition
66
Data Structures: A Pseudocode Approach with C, Second Edition
67
Data Structures: A Pseudocode Approach with C, Second Edition
68
Data Structures: A Pseudocode Approach with C, Second Edition
69
Data Structures: A Pseudocode Approach with C, Second Edition
70
Data Structures: A Pseudocode Approach with C, Second Edition
71
Data Structures: A Pseudocode Approach with C, Second Edition
72
Data Structures: A Pseudocode Approach with C, Second Edition
73
Data Structures: A Pseudocode Approach with C, Second Edition
74
Data Structures: A Pseudocode Approach with C, Second Edition
75
Data Structures: A Pseudocode Approach with C, Second Edition
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.