ADT Heap data structure

Slides:



Advertisements
Similar presentations
Heaps1 Part-D2 Heaps Heaps2 Recall Priority Queue ADT (§ 7.1.3) A priority queue stores a collection of entries Each entry is a pair (key, value)
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.
Version TCSS 342, Winter 2006 Lecture Notes Priority Queues Heaps.
1 TCSS 342, Winter 2005 Lecture Notes Priority Queues and Heaps Weiss Ch. 21, pp
PQ, binary heaps G.Kamberova, Algorithms Priority Queue ADT Binary Heaps Gerda Kamberova Department of Computer Science Hofstra University.
CSE 373 Data Structures and Algorithms Lecture 13: Priority Queues (Heaps)
1 CSC 427: Data Structures and Algorithm Analysis Fall 2010 transform & conquer  transform-and-conquer approach  balanced search trees o AVL, 2-3 trees,
Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07.
PRIORITY QUEUES (HEAPS). Queues are a standard mechanism for ordering tasks on a first-come, first-served basis However, some tasks may be more important.
Heaps, Heapsort, Priority Queues. Sorting So Far Heap: Data structure and associated algorithms, Not garbage collection context.
data ordered along paths from root to leaf
Chapter 21 Priority Queue: Binary Heap Saurav Karmakar.
Sorting Dr. Yingwu Zhu. Heaps A heap is a binary tree with properties: 1. It is complete Each level of tree completely filled Except possibly bottom level.
Chapter 2: Basic Data Structures. Spring 2003CS 3152 Basic Data Structures Stacks Queues Vectors, Linked Lists Trees (Including Balanced Trees) Priority.
1 Joe Meehean.  We wanted a data structure that gave us... the smallest item then the next smallest then the next and so on…  This ADT is called a priority.
Priority Queues and Heaps. October 2004John Edgar2  A queue should implement at least the first two of these operations:  insert – insert item at the.
Lecture 15 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
Chapter 12 Heaps & HeapSort © John Urrutia 2014, All Rights Reserved1.
Sorting Dr. Yingwu Zhu. Heaps A heap is a binary tree with properties: 1. It is complete Each level of tree completely filled Except possibly bottom level.
HEAPS. Review: what are the requirements of the abstract data type: priority queue? Quick removal of item with highest priority (highest or lowest key.
FALL 2005CENG 213 Data Structures1 Priority Queues (Heaps) Reference: Chapter 7.
Heaps © 2010 Goodrich, Tamassia. Heaps2 Priority Queue ADT  A priority queue (PQ) stores a collection of entries  Typically, an entry is a.
1 CSC 421: Algorithm Design Analysis Spring 2013 Transform & conquer  transform-and-conquer approach  presorting  balanced search trees, heaps  Horner's.
Course: Programming II - Abstract Data Types HeapsSlide Number 1 The ADT Heap So far we have seen the following sorting types : 1) Linked List sort by.
1 Priority Queues (Heaps). 2 Priority Queues Many applications require that we process records with keys in order, but not necessarily in full sorted.
Priority Queues and Heaps. John Edgar  Define the ADT priority queue  Define the partially ordered property  Define a heap  Implement a heap using.
Heaps and Priority Queues What is a heap? A heap is a binary tree storing keys at its internal nodes and satisfying the following properties:
Sorting With Priority Queue In-place Extra O(N) space
Priority Queues A priority queue is an ADT where:
"Teachers open the door, but you must enter by yourself. "
ADT description Implementations
Heaps (8.3) CSE 2011 Winter May 2018.
Priority Queues © 2010 Goodrich, Tamassia Priority Queues 1
Heaps 8/2/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser,
October 30th – Priority QUeues
Heaps © 2010 Goodrich, Tamassia Heaps Heaps
Bohyung Han CSE, POSTECH
Heaps 9/13/2018 3:17 PM Heaps Heaps.
Heap Sort Example Qamar Abbas.
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
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}
CMSC 341: Data Structures Priority Queues – Binary Heaps
Part-D1 Priority Queues
Data Structures & Algorithms Priority Queues & HeapSort
Draw pictures to indicate the subproblems middleMax solves at each level and the resulting maxPtr and PrevPtr for each on this linked list:
CSE 373: Data Structures and Algorithms
B-Tree Insertions, Intro to Heaps
Tree Representation Heap.
© 2013 Goodrich, Tamassia, Goldwasser
Heaps 12/4/2018 5:27 AM Heaps /4/2018 5:27 AM Heaps.
Ch. 8 Priority Queues And Heaps
Hassan Khosravi / Geoffrey Tien
"Teachers open the door, but you must enter by yourself. "
CS Data Structure: Heaps.
Tables and Priority Queues
Heaps © 2014 Goodrich, Tamassia, Goldwasser Heaps Heaps
Priority Queues & Heaps
Sorting Dr. Yingwu Zhu.
CSE 373 Priority queue implementation; Intro to heaps
Priority Queues & Heaps
CO4301 – Advanced Games Development Week 4 Binary Search Trees
Data Structures and Algorithm Analysis Priority Queues (Heaps)
Priority Queue and Heap
Heaps & Multi-way Search Trees
Heaps 9/29/2019 5:43 PM Heaps Heaps.
Heaps.
CS210- Lecture 13 June 28, 2005 Agenda Heaps Complete Binary Tree
CMPT 225 Lecture 16 – Heap Sort.
Presentation transcript:

ADT Heap data structure Priority Queues ADT Heap data structure March 14, 2018 Cinda Heeren / Geoffrey Tien

Cinda Heeren / Geoffrey Tien Priority queues? Suppose Geoff has made a to-do list (with priority values): 7 – Grade MT2 2 – Vacuum home 8 – Replace bicycle flat tire 1 – Sleep 9 – Extinguish computer on fire 2 – Eat 8 – Lecture prep 1 – Bathe We are interested in quickly finding the task with the highest priority March 14, 2018 Cinda Heeren / Geoffrey Tien

Cinda Heeren / Geoffrey Tien Priority queue ADT A collection organised so as to allow fast access to and removal of the largest (or smallest) element Prioritisation is a weaker condition than ordering Order of insertion is irrelevant Element with the highest priority (whatever that means) is the first element to be removed Not really a queue: not FIFO! March 14, 2018 Cinda Heeren / Geoffrey Tien

Cinda Heeren / Geoffrey Tien Priority queue ADT Priority queue operations create destroy insert removeMin (or removeMax) isEmpty F(7) E(5) D(100) A(4) B(6) insert C(3) removeMax G(9) D(100) G(9) Priority queue property For two elements 𝑥 and 𝑦 in the queue, if 𝑥 has a higher priority value than 𝑦, 𝑥 will be removed before 𝑦 Note that in most definitions, a single priority queue structure supports only removeMin, or only removeMax, and not both March 14, 2018 Cinda Heeren / Geoffrey Tien

Priority queue properties A priority queue is an ADT that maintains a multiset of items vs set: a multiset allows duplicate entries Two or more distinct items in a priority queue may have the same priority If all items have the same priority, will it behave FIFO like a queue? not necessarily! Due to implementation details March 14, 2018 Cinda Heeren / Geoffrey Tien

Priority queue applications Hold jobs for a printer in order of size Manage limited resources such as bandwidth on a transmission line from a network router Sort numbers Anything greedy: an algorithm that makes the "locally best choice" at each step March 14, 2018 Cinda Heeren / Geoffrey Tien

Data structures for priority queues Worst case complexities Structure insert removeMin Unordered array 𝑂 1 𝑂 𝑛 Ordered array Unordered list Ordered list Binary search tree AVL tree 𝑂 log 𝑛 Binary heap 𝑂 log 𝑛 Heap has asymptotically same performance as AVL tree, but MUCH simpler to implement March 14, 2018 Cinda Heeren / Geoffrey Tien

Binary heap A heap is binary tree with two properties A complete, partially-ordered binary tree A heap is binary tree with two properties Heaps are complete All levels, except the bottom, must be completely filled in The leaves on the bottom level are as far to the left as possible Heaps are partially ordered For a max heap – the value of a node is at least as large as its children’s values For a min heap – the value of a node is no greater than its children’s values March 14, 2018 Cinda Heeren / Geoffrey Tien

Review: complete binary trees incomplete binary trees March 14, 2018 Cinda Heeren / Geoffrey Tien

Partially ordered tree max heap example 98 86 41 13 65 32 29 9 10 44 23 21 Heaps are not fully ordered – an in-order traversal would result in: 9, 13, 10, 86, 44, 65, 23, 98, 21, 32, 32, 41, 29 March 14, 2018 Cinda Heeren / Geoffrey Tien

Duplicate priority values Min heap examples It is important to realise that two binary heaps can contain the same data, but items may appear in different positions in the structure 2 2 5 7 5 5 5 7 8 7 7 8 March 14, 2018 Cinda Heeren / Geoffrey Tien

Cinda Heeren / Geoffrey Tien Heap implementation Using an array Heaps can be implemented using arrays There is a natural method of indexing tree nodes Index nodes from top to bottom and left to right as shown on the right Because heaps are complete binary trees there can be no gaps in the array 1 2 3 4 5 6 1 2 3 4 5 6 ... March 14, 2018 Cinda Heeren / Geoffrey Tien

Cinda Heeren / Geoffrey Tien Referencing nodes To move around in the tree, it will be necessary to find the index of the parents of a node or the children of a node The array is indexed from 0 to 𝑛−1 Each level’s nodes are indexed from: 2 𝑙𝑒𝑣𝑒𝑙 −1 to 2 𝑙𝑒𝑣𝑒𝑙+1 −2 (where the root is level 0) The children of a node 𝑖, are the array elements indexed at 2𝑖+1 and 2𝑖+2 The parent of a node 𝑖, is the array element indexed at 𝑖−1 2 March 14, 2018 Cinda Heeren / Geoffrey Tien

Cinda Heeren / Geoffrey Tien Array heap example 98 86 41 13 65 32 29 9 10 44 23 21 Heap 1 2 3 4 5 6 7 8 9 10 11 12 Underlying array 98 86 41 13 65 32 29 9 10 1 2 3 4 5 6 44 23 21 11 12 7 8 value index March 14, 2018 Cinda Heeren / Geoffrey Tien

Cinda Heeren / Geoffrey Tien Heap implementation class MinHeap { private: int size; // number of stored elements int capacity; // maximum capacity of array int* arr; // array in dynamic memory public: ... }; MinHeap::MinHeap(int initcapacity) { size = 0; capacity = initcapacity; arr = new int[capacity]; } March 14, 2018 Cinda Heeren / Geoffrey Tien

Cinda Heeren / Geoffrey Tien Heap insertion On insertion the heap properties have to be maintained, remember that A heap is a complete binary tree and A partially ordered binary tree There are two general strategies that could be used to maintain the heap properties Make sure that the tree is complete and then fix the ordering or Make sure the ordering is correct first Which is better? March 14, 2018 Cinda Heeren / Geoffrey Tien

Cinda Heeren / Geoffrey Tien Heap insertion sketch On insertion the heap properties have to be maintained A heap is a complete binary tree and A partially ordered binary tree The insertion algorithm first ensures that the tree is complete new item is the first available (left-most) leaf on the bottom level i.e. the first free element in the underlying array Fix the partial ordering Compare the new value to its parent Swap them if the new value is greater than the parent Repeat until this is not the case Referred to as heapify up, percolate up, or trickle up, bubble up, etc. March 14, 2018 Cinda Heeren / Geoffrey Tien

Heap insertion example max heap Insert 81 98 86 41 13 65 32 29 9 10 44 23 21 98 86 41 13 65 32 29 9 10 1 2 3 4 5 6 44 23 21 11 12 7 8 value index March 14, 2018 Cinda Heeren / Geoffrey Tien

Heap insertion example max heap Insert 81 98 86 41 13 65 32 29 9 10 44 23 21 81 29 81 parent: 13−1 2 =6 98 86 41 13 65 32 29 9 10 1 2 3 4 5 6 44 23 21 11 12 7 8 value index 81 29 81 13 March 14, 2018 Cinda Heeren / Geoffrey Tien

Heap insertion example max heap 81 is less than 98 so finished Insert 81 98 86 41 13 65 32 81 9 10 44 23 21 81 41 29 parent: 6−1 2 =2 98 86 41 13 65 32 81 9 10 1 2 3 4 5 6 44 23 21 11 12 7 8 value index 81 41 29 13 March 14, 2018 Cinda Heeren / Geoffrey Tien

Heap insertion complexity Item is inserted at the bottom level in the first available space this can be tracked using the heap size attribute 𝑂 1 access using array index Repeated heapify-up operations. How many? each heapify-up operation moves the inserted value up one level in the tree Upper limit on the number of levels in a complete tree? 𝑂 log 𝑛 Heap insertion has worst-case performance of 𝑂 log 𝑛 March 14, 2018 Cinda Heeren / Geoffrey Tien

Building a heap (an heap?) A heap can be constructed by repeatedly inserting items into an empty heap. Complexity? 𝑂 log 𝑛 per item, 𝑂 𝑛 items 𝑂 𝑛 log 𝑛 total More about this next class March 14, 2018 Cinda Heeren / Geoffrey Tien

Removing the priority item Heap properties must be satisfied after removal Make a temporary copy of the root’s data Similarly to the insertion algorithm, first ensure that the heap remains complete Replace the root node with the right-most leaf i.e. the highest (occupied) index in the array Swap the new root with its largest valued child until the partially ordered property holds i.e. heapifyDown Return the copied root’s data March 14, 2018 Cinda Heeren / Geoffrey Tien

Cinda Heeren / Geoffrey Tien Heap removal example 98 86 41 13 65 32 29 9 10 44 23 21 17 removeMax 98 86 41 13 65 32 29 9 10 1 2 3 4 5 6 44 23 21 17 11 12 7 8 value index March 14, 2018 Cinda Heeren / Geoffrey Tien

Cinda Heeren / Geoffrey Tien Heap removal example Replace root with rightmost leaf 98 86 41 13 65 32 29 9 10 44 23 21 17 removeMax 17 98 86 41 13 65 32 29 9 10 1 2 3 4 5 6 44 23 21 11 7 8 value index 17 17 12 March 14, 2018 Cinda Heeren / Geoffrey Tien

Cinda Heeren / Geoffrey Tien Heap removal example Swap with largest child 17 86 41 13 65 32 29 9 10 44 23 21 86 removeMax ? ? 17 children of root: 2∙0+1, 2∙0+2=1, 2 17 86 41 13 65 32 29 9 10 1 2 3 4 5 6 44 23 21 11 7 8 value index 86 17 12 March 14, 2018 Cinda Heeren / Geoffrey Tien

Cinda Heeren / Geoffrey Tien Heap removal example Swap with largest child 86 17 41 13 65 32 29 9 10 44 23 21 removeMax 65 ? ? 17 children of 1: 2∙1+1, 2∙1+2=3, 4 86 17 41 13 65 32 29 9 10 1 2 3 4 5 6 44 23 21 11 7 8 value index 65 17 12 March 14, 2018 Cinda Heeren / Geoffrey Tien

Cinda Heeren / Geoffrey Tien Heap removal example Swap with largest child 86 65 41 13 17 32 29 9 10 44 23 21 removeMax 44 ? ? 17 children of 4: 2*4+1, 2*4+2 = 9, 10 86 65 41 13 17 32 29 9 10 1 2 3 4 5 6 44 23 21 11 7 8 value index 44 17 12 March 14, 2018 Cinda Heeren / Geoffrey Tien

Complexity of removeMin / removeMax Analysis is similar to insertion Replace root with last element – 𝑂 1 Repeated heapify-down operations starting from root level each heapify-down moves one level closer to the bottom of the tree How many levels? 𝑂 log 𝑛 Removing the priority item from a heap is also 𝑂 log 𝑛 in the worst case March 14, 2018 Cinda Heeren / Geoffrey Tien

Array implementation and insertion Note that like other array-based structures, we have a limited capacity at creation time What to do when the array is full? Expand it! Since array indices correspond exactly to node positions in the tree, and nodes should remain in their original positions after expanding the array, we can simply copy into the same indices 3 27 9 31 46 12 1 2 4 5 value index 3 27 9 31 46 12 1 2 4 5 6 10 11 7 8 value index 62 March 14, 2018 Cinda Heeren / Geoffrey Tien

Cinda Heeren / Geoffrey Tien Exercise Insert the following sequence of items into an initially empty max heap: 34, 76, 11, 32, 73, 9, 50, 65, 41, 27, 3, 88 Draw the underlying array after all insertions have completed Insert the same sequence of items into an initially empty min heap and draw the array after all insertions have been completed. Implement recursive and iterative implementations: void MinHeap::heapifyUp(int index); March 14, 2018 Cinda Heeren / Geoffrey Tien

Readings for this lesson Carrano & Henry Chapter 13.3 (ADT priority queue) Chapter 17.1 – 17.3 (Heap) Next class: Carrano & Henry, Chapter 17.4 (Heapsort) March 14, 2018 Cinda Heeren / Geoffrey Tien