Problem of the Day  To get into club, must get past bouncer  Recorded correct responses of those before you  Guard said five & response was four  Guard.

Slides:



Advertisements
Similar presentations
© 2004 Goodrich, Tamassia Heaps © 2004 Goodrich, Tamassia Heaps2 Recall Priority Queue ADT (§ 7.1.3) A priority queue stores a collection of.
Advertisements

COL 106 Shweta Agrawal and Amit Kumar
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)
CSC 212 – Data Structures.  Fri., Dec. 17 th from 8AM – 10AM in OM 200  Plan on exam taking full 2 hours  If major problem, come talk to me ASAP 
Problem of the Day  At low tide, a ship docks in the harbor  Ship is 9’ above the water line when it docks  Over the side hangs a rope ladder w/ rungs.
Problem of the Day  You are trapped alone in a dark room with:  Candle;  Wood stove; and  Gas lamp (with full tank).  You only have one match; what.
Data Structures Lecture 7 Fang Yu Department of Management Information Systems National Chengchi University Fall 2010.
The Priority Queue Abstract Data Type. Heaps. Adaptable Priority Queue. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich,
Priority Queues. Container of elements where each element has an associated key A key is an attribute that can identify rank or weight of an element Examples.
© 2004 Goodrich, Tamassia Priority Queues1 Heaps: Tree-based Implementation of a Priority Queue.
© 2004 Goodrich, Tamassia Heaps © 2004 Goodrich, Tamassia Heaps2 Priority Queue Sorting (§ 8.1.4) We can use a priority queue to sort a set.
Priority Queues. Container of elements where each element has an associated key A key is an attribute that can identify rank or weight of an element Examples.
Priority Queues1 Part-D1 Priority Queues. Priority Queues2 Priority Queue ADT (§ 7.1.3) A priority queue stores a collection of entries Each entry is.
1 Priority Queues CPS212 Gordon College VIP. 2 Introduction to STL Priority Queues Adaptor container - underlying container may be either: – a template.
1 Chapter 8 Priority Queues. 2 Implementations Heaps Priority queues and heaps Vector based implementation of heaps Skew heaps Outline.
Heaps and Priority Queues Priority Queue ADT (§ 2.4.1) A priority queue stores a collection of items An item is a pair (key, element) Main.
1 HEAPS & PRIORITY QUEUES Array and Tree implementations.
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.
1 Heaps and Priority Queues Starring: Min Heap Co-Starring: Max Heap.
CSC 213 – Large Scale Programming Lecture 15: Heap-based Priority Queue.
Priority Queue. Priority Queues Queue (FIFO). Priority queue. Deletion from a priority queue is determined by the element priority. Two kinds of priority.
PRIORITY QUEUES AND HEAPS CS16: Introduction to Data Structures & Algorithms Tuesday, February 24,
1 Heaps A heap is a binary tree. A heap is best implemented in sequential representation (using an array). Two important uses of heaps are: –(i) efficient.
Chapter 2.4: Priority Queues and Heaps PriorityQueue ADT (§2.4.1) Total order relation (§2.4.1) Comparator ADT (§2.4.1) Sorting with a priority queue (§2.4.2)
HEAPS • Heaps • Properties of Heaps • HeapSort
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.
CSC 213 – Large Scale Programming Lecture 18: Zen & the Art of O (log n ) Search.
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.
CS 361 – Chapter 5 Priority Queue ADT Heap data structure –Properties –Internal representation –Insertion –Deletion.
Heaps and Heapsort Prof. Sin-Min Lee Department of Computer Science San Jose State University.
CS 2468: Assignment 2 (Due Week 9, Tuesday. Drop a hard copy in Mail Box 75 or hand in during the lecture) Use array representation (double a[]) to implement.
1 Heaps A heap is a binary tree. A heap is best implemented in sequential representation (using an array). Two important uses of heaps are: –(i) efficient.
Quotes “From each according to his ability, to each according to his needs” -- Karl Marx/Queue ADT “In America, first you get the sugar, then you get the.
Problem of the Day  You are trapped alone in a dark room with:  Candle;  Wood stove; and  Gas lamp (with full tank).  You only have one match; what.
CS 367 Introduction to Data Structures Lecture 8.
Priority Queues and Heaps Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University.
Priority Queues CS 110: Data Structures and Algorithms First Semester,
Heaps © 2010 Goodrich, Tamassia. Heaps2 Priority Queue ADT  A priority queue (PQ) stores a collection of entries  Typically, an entry is a.
1 Chapter 6 Heapsort. 2 About this lecture Introduce Heap – Shape Property and Heap Property – Heap Operations Heapsort: Use Heap to Sort Fixing heap.
Priority Queues CS /02/05 L7: PQs Slide 2 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved.
CSC 213 – Large Scale Programming. Priority Queue ADT  Prioritizes Entry s using their keys  For Entry s with equal priorities, order not specified.
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:
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,
Heaps © 2010 Goodrich, Tamassia Heaps Heaps
Bohyung Han CSE, POSTECH
Heaps 9/13/2018 3:17 PM Heaps Heaps.
Heaps and Priority Queues
Priority Queues and Heaps
Part-D1 Priority Queues
Heaps and Priority Queues
Heaps 11/27/ :05 PM Heaps Heaps.
Tree Representation Heap.
Heaps A heap is a binary tree.
Heaps and Priority Queues
© 2013 Goodrich, Tamassia, Goldwasser
Heaps 12/4/2018 5:27 AM Heaps /4/2018 5:27 AM Heaps.
Ch. 8 Priority Queues And Heaps
Heaps and Priority Queues
Heaps © 2014 Goodrich, Tamassia, Goldwasser Heaps Heaps
Priority Queues & Heaps
Heaps and Priority Queues
1 Lecture 10 CS2013.
Heaps By JJ Shepherd.
CS210- Lecture 14 July 5, 2005 Agenda Inserting into Heap
Heaps 9/29/2019 5:43 PM Heaps Heaps.
CS210- Lecture 13 June 28, 2005 Agenda Heaps Complete Binary Tree
Presentation transcript:

Problem of the Day  To get into club, must get past bouncer  Recorded correct responses of those before you  Guard said five & response was four  Guard said three & response was five  Guard said twelve & response was six  Guard said six & response was three  Guard now says to you ten, what do you say?

Problem of the Day  To get into club, must get past bouncer  Recorded correct responses of those before you  Guard said five & response was four  Guard said three & response was five  Guard said twelve & response was six  Guard said six & response was three  Guard now says to you ten, what do you say? Three!

CSC 212 – Data Structures

Priority Queue ADT  Prioritizes Entry s using their keys  For Entry s with equal priorities, order not specified  Priority given to each value when added to PQ  Normally, the priority not changeable while in PQ  Access single Entry : one with the lowest priority  Returns Entry using min() or removeMin()  Location are imaginary – only smallest matters

Heaps  Binary-tree based PQ implementation  Still structured using parent-child relationship  At most 2 children & 1 parent for each node in tree  Heaps must also satisfy 2 additional properties  Parent at least as important as its children  Structure must form a complete binary tree

BinaryTree   Picturing Linked BinaryTree B C A D   BACD BinaryTree root size  4

Legal CompleteBinaryTree  ADT which extends BinaryTree  Add & remove methods defined plus those inherited  For this ADT, trees must maintain specific shape  Fill lowest level first, then can start new level below it Illegal

CompleteBinaryTree  ADT which extends BinaryTree  Add & remove methods defined plus those inherited  For this ADT, trees must maintain specific shape  Fill lowest level first, then can start new level below it  Lowest level must be filled in from left-to-right Legal Illegal

What Is Purpose of a Heap?  Root has critical Entry that we always access  Entry at root always has smallest priority in Heap  O(1) access time in min() without any real effort  CompleteBinaryTree makes insert() easy  Create leftmost child on lowest level  When a level completes, start next one  Useful when:

Upheap  Insertion may violate heap-order property  Upheap immediately after adding new Entry  Goes from new node to restore heap’s order  Compare priority of node & its parent  If out of order, swap node's Entry s  Continue upheaping from parent node  Stop only when either case occurs:  Found properly ordered node & parent  Binary tree's root is reached

6 insert() in a Heap

6 1 Start your upheaping!

1 6 Upheaping Must Continue

2 6 Upheaping Sounds Icky

2 6 Check If We Should Continue

2 6 Stop At The Root

2 6 insert() Once Again

2 6 Upheaping Begins Anew

2 6 Maintain Heap Order Property

2 6 We Are Done With This Upheap!

Removing From a Heap  removeMin() must kill Entry at heap’s root  For a complete tree, must remove last node added  How to reconcile these two different demands?  Removed node's Entry moved to the root  Then remove node from the complete tree  Heap's order preserved by going down

Removing From a Heap  removeMin() must kill Entry at heap’s root  For a complete tree, must remove last node added  How to reconcile these two different demands?  Removed node's Entry moved to the root  Then remove node from the complete tree  Heap's order preserved by going down

Downheap  Restores heap’s order during removeMin()  Downheap work starts at root  Swap with smallest child, if at least out-of-order  Downheaping continues with old smallest child  Stop at leaf or when node is legal

5 Before removeMin() is called

5 Move Last Entry Up To Root

5 Compare Parent W/Smaller Child 9 2 7

5 Continue Downheaping W/Node 2 9 7

5 Swap If Out Of Order 2 7 9

5 Check If We Should Continue 2 7 9

5 Stop When We Reach a Leaf 2 7 9

Implementation Excuses  upheap & downheap travel height of tree  O (log n ) running time for each of these  Serves as bound for adding & removing from PQ  What drawbacks does heap have?  PriorityQueue can be faster using Sequence  Only for specific mix of operations, however  PriorityQueue using heaps are fastest overall

What Is Purpose of a Heap?  Root has critical Entry that we always access  Entry at root always has smallest priority in Heap  O(1) access time in min() without any real effort  CompleteBinaryTree makes insert() easy  Create leftmost child on lowest level  When a level completes, start next one  Useful when:  Will eventually add & remove everything  Equally (large) numbers of adds & removes

Final Exam Schedule  Lab Mastery Exam is: Tues., Dec. 14 th from 2:45PM – 3:45PM in OM 119  Final Exam is: Fri., Dec. 17 th from 8AM – 10AM in OM 200