Heap Chapter 9 Objectives Upon completion you will be able to:

Slides:



Advertisements
Similar presentations
The Heap ADT In this section of notes you will learn about a new abstract data type, the heap, as well how heaps can be used.
Advertisements

CMPT 225 Priority Queues and Heaps. Priority Queues Items in a priority queue have a priority The priority is usually numerical value Could be lowest.
Heapsort. 2 Why study Heapsort? It is a well-known, traditional sorting algorithm you will be expected to know Heapsort is always O(n log n) Quicksort.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L12 (Chapter 20) Lists, Stacks,
Binary Search Trees Chapter 7 Objectives
Maps A map is an object that maps keys to values Each key can map to at most one value, and a map cannot contain duplicate keys KeyValue Map Examples Dictionaries:
Heapsort Based off slides by: David Matuszek
1 HEAPS & PRIORITY QUEUES Array and Tree implementations.
Trees. Tree Terminology Chapter 8: Trees 2 A tree consists of a collection of elements or nodes, with each node linked to its successors The node at the.
9/17/20151 Chapter 12 - Heaps. 9/17/20152 Introduction ► Heaps are largely about priority queues. ► They are an alternative data structure to implementing.
ADT Table and Heap Ellen Walker CPSC 201 Data Structures Hiram College.
Heapsort CSC Why study Heapsort? It is a well-known, traditional sorting algorithm you will be expected to know Heapsort is always O(n log n)
Binary Heap.
Priority Queues and Binary Heaps Chapter Trees Some animals are more equal than others A queue is a FIFO data structure the first element.
Chapter 21 Priority Queue: Binary Heap Saurav Karmakar.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of CHAPTER 12: Multi-way Search Trees Java Software Structures: Designing.
Priority Queue. Priority Queues Queue (FIFO). Priority queue. Deletion from a priority queue is determined by the element priority. Two kinds of priority.
Ceng-112 Data Structures I Figure 9-1 The heap is guaranteed to hold the largest node of the tree in the root. The smaller nodes of a heap can be.
Priority Queues and Heaps. October 2004John Edgar2  A queue should implement at least the first two of these operations:  insert – insert item at the.
CPSC 252 Binary Heaps Page 1 Binary Heaps A complete binary tree is a binary tree that satisfies the following properties: - every level, except possibly.
1 Chapter 7 Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation of the binary search tree.
Lecture 15 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
1 Heap Sort. A Heap is a Binary Tree Height of tree = longest path from root to leaf =  (lgn) A heap is a binary tree satisfying the heap condition:
Data Structures: A Pseudocode Approach with C, Second Edition 1 Chapter 7 Objectives Create and implement binary search trees Understand the operation.
Lecture on Data Structures(Trees). Prepared by, Jesmin Akhter, Lecturer, IIT,JU 2 Properties of Heaps ◈ Heaps are binary trees that are ordered.
Priority Queues and Heaps. John Edgar  Define the ADT priority queue  Define the partially ordered property  Define a heap  Implement a heap using.
Priority Queues and Heaps Tom Przybylinski. Maps ● We have (key,value) pairs, called entries ● We want to store and find/remove arbitrary entries (random.
Binary Search Trees Chapter 7 Objectives
Partially Ordered Data ,Heap,Binary Heap
CSE373: Data Structures & Algorithms
Heap Chapter 9 Objectives Define and implement heap structures
Chapter 11 Heap.
Heaps (8.3) CSE 2011 Winter May 2018.
Binary Search Tree (BST)
B+ Tree.
Bohyung Han CSE, POSTECH
Heap Sort Example Qamar Abbas.
Phil Tayco Slide version 1.0 May 7, 2018
7/23/2009 Many thanks to David Sun for some of the included slides!
original list {67, 33,49, 21, 25, 94} pass { } {67 94}
Chapter 8 – Binary Search Tree
Dr. David Matuszek Heapsort Dr. David Matuszek
Draw pictures to indicate the subproblems middleMax solves at each level and the resulting maxPtr and PrevPtr for each on this linked list:
A Kind of Binary Tree Usually Stored in an Array
B-Tree Insertions, Intro to Heaps
Graphs Chapter 11 Objectives Upon completion you will be able to:
Tree Representation Heap.
CS Data Structures Chapter 17 Heaps Mehmet H Gunes
Heaps Chapter 10 has several programming projects, including a project that uses heaps. This presentation shows you what a heap is, and demonstrates two.
CS Data Structures Chapter 17 Heaps Mehmet H Gunes
Heaps Chapter 10 has several programming projects, including a project that uses heaps. This presentation shows you what a heap is, and demonstrates two.
Heaps Chapter 11 has several programming projects, including a project that uses heaps. This presentation shows you what a heap is, and demonstrates.
A Robust Data Structure
Heapsort.
Heaps Chapter 11 has several programming projects, including a project that uses heaps. This presentation shows you what a heap is, and demonstrates.
Advanced Implementation of Tables
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Binary Search Trees Chapter 7 Objectives
Heapsort.
Heaps By JJ Shepherd.
Heapsort.
Chapter 9 The Priority Queue ADT
Priority Queue and Heap
Heaps & Multi-way Search Trees
Heapsort.
CO 303 Algorithm Analysis and Design
Heaps Chapter 10 has several programming projects, including a project that uses heaps. This presentation shows you what a heap is, and demonstrates two.
Introduction to Trees Chapter 6 Objectives
Heaps.
CMPT 225 Lecture 16 – Heap Sort.
Presentation transcript:

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

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

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

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

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

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

Data Structures: A Pseudocode Approach with C, Second Edition

Heap Structure Data Structures: A Pseudocode Approach with C, Second Edition

Data Structures: A Pseudocode Approach with C, Second Edition

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

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

Data Structures: A Pseudocode Approach with C, Second Edition

Data Structures: A Pseudocode Approach with C, Second Edition

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

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

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

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

Data Structures: A Pseudocode Approach with C, Second Edition

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

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

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

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

Data Structures: A Pseudocode Approach with C, Second Edition

Heap Data Structure (3) The index of 32 is 2, - so the index of its left child, 23, is 2 * 2 + 1 = 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

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

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

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 9.2. Basic functions, such as heap full, are not provided. Heap Structure Heap Algorithms 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

(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

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

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

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

Data Structures: A Pseudocode Approach with C, Second Edition

Data Structures: A Pseudocode Approach with C, Second Edition

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

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

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

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

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

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

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

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