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.

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

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  To get into club, must get past bouncer  Recorded correct responses of those before you  Guard said five & response was four  Guard.
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.
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.
Fall 2007CS 2251 Trees Chapter 8. Fall 2007CS 2252 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information.
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.
Binary Trees A binary tree is made up of a finite set of nodes that is either empty or consists of a node called the root together with two binary trees,
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.
CSC 213 – Large Scale Programming Lecture 14: Sequence-based Priority Queues.
1 Chapter 8 Priority Queues. 2 Implementations Heaps Priority queues and heaps Vector based implementation of heaps Skew heaps Outline.
1 CSC 427: Data Structures and Algorithm Analysis Fall 2010 transform & conquer  transform-and-conquer approach  balanced search trees o AVL, 2-3 trees,
CSC 172 DATA STRUCTURES. Priority Queues Model Set with priorities associatedwith elements Priorities are comparable by a < operator Operations Insert.
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:
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.
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.
ADT Table and Heap Ellen Walker CPSC 201 Data Structures Hiram College.
Priority Queues and Binary Heaps Chapter Trees Some animals are more equal than others A queue is a FIFO data structure the first element.
CSC 213 – Large Scale Programming Lecture 17: Binary Search Trees.
Chapter 21 Binary Heap.
Chapter 11 Heap. Overview ● The heap is a special type of binary tree. ● It may be used either as a priority queue or as a tool for sorting.
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.
CSC 212 – Data Structures.  Fri., Dec. 14 th from 2:45 – 4:45PM in SH1028  Plan on exam taking full 2 hours  If major problem, come talk to me ASAP.
PRIORITY QUEUES AND HEAPS CS16: Introduction to Data Structures & Algorithms Tuesday, February 24,
HEAPS • Heaps • Properties of Heaps • HeapSort
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.
1 Heaps and Priority Queues v2 Starring: Min Heap Co-Starring: Max Heap.
A Heap Implementation Chapter 26 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Heaps & Priority Queues
Heaps A heap is a binary tree that satisfies the following properties: Structure property: It is a complete binary tree Heap-order property: Each node.
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.
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.
1 Heaps II Implementation HeapSort Bottom-Up Heap Construction Locators.
Priority Queues and Heaps Tom Przybylinski. Maps ● We have (key,value) pairs, called entries ● We want to store and find/remove arbitrary entries (random.
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:
Partially Ordered Data ,Heap,Binary Heap
Priority Queues and Heaps
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
Section 9.2b Adding and Removing from a Heap.
Part-D1 Priority Queues
Heaps © 2010 Goodrich, Tamassia Heaps Heaps
Bohyung Han CSE, POSTECH
Heaps 9/13/2018 3:17 PM Heaps Heaps.
- Alan Perlis Heaps "You think you know when you can learn,
Priority Queues and Heaps
Priority Queues.
Heaps 11/27/ :05 PM Heaps Heaps.
Priority Queues.
© 2013 Goodrich, Tamassia, Goldwasser
Ch. 8 Priority Queues And Heaps
Heaps A heap is a binary tree that satisfies the following properties:
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Heaps © 2014 Goodrich, Tamassia, Goldwasser Heaps Heaps
A Heap Implementation Chapter 26 Adapted from Pearson Education, Inc.
Priority Queues & Heaps
1 Lecture 10 CS2013.
Heaps By JJ Shepherd.
Heaps 9/29/2019 5:43 PM Heaps Heaps.
Heaps.
CS210- Lecture 13 June 28, 2005 Agenda Heaps Complete Binary Tree
Presentation transcript:

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 do you light 1 st ?

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 do you light 1 st ?

Problem of the Day  You are trapped alone in a dark room with:  Candle;  Wood stove; and  Gas lamp (with full tank). one match  You only have one match; what do you light 1 st ?

Problem of the Day  You are trapped alone in a dark room with:  Candle;  Wood stove; and  Gas lamp (with full tank). one match  You only have one match; what do you light 1 st ? The match!

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()

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  Can not use any tree; must form complete binary tree

Implement CompleteBinaryTree  Already know basics, this extends BinaryTree  Use existing tree implementation to make easier  2 implementations of BinaryTree : which to use?  Sequence -based possible should be 1 st question?  Continue relying on trick of mapping children to indices  FullPQException not helpful, so can this work?  Even if so, how much extra space would this require?

Sequence -based BinaryTree  Node at index specified for location in T REE  Root node stored at index 0  Root’s left child at index 1  Right child of root at index 2  Left child’s right child at index 4  Right child’s left child at index 5  Node at index n ’s left child is at index 2n + 1  Node at index n ’s right child is at index 2n + 2

Sequence -based BinaryTree  Node at index specified for location in T REE  Root node stored at index 0  Root’s left child at index 1  Right child of root at index 2  Left child’s right child at index 4  Right child’s left child at index 5  Node at index n ’s left child is at index 2n + 1  Node at index n ’s right child is at index 2n + 2 But how much space will this need for to hold a heap?

Sequence to Implement Heap

`

`

Sequence to Implement Heap ` Add nodes to end of the Sequence Similarly, remove node at end NO space is wasted for this! Add nodes to end of the Sequence Similarly, remove node at end NO space is wasted for this!

Swapping Data in a Heap  Two ways to swap Entry s in a heap  Preserve the nodes, but exchange their elements  Move nodes within tree & leave the elements

Swapping Data in a Heap  Two ways to swap Entry s in a heap  Preserve the nodes, but exchange their elements  Move nodes within tree & leave the elements  Preserving nodes & swapping Entry s means

Swapping Data in a Heap  Two ways to swap Entry s in a heap  Preserve the nodes, but exchange their elements  Move nodes within tree & leave the elements  Preserving nodes & swapping Entry s means  Setting two references (one in each node)  If decide you want to move nodes around instead

Swapping Data in a Heap  Two ways to swap Entry s in a heap  Preserve the nodes, but exchange their elements  Move nodes within tree & leave the elements  Preserving nodes & swapping Entry s means  Setting two references (one in each node)  If decide you want to move nodes around instead  Set two references for parents (one in each node)

Swapping Data in a Heap  Two ways to swap Entry s in a heap  Preserve the nodes, but exchange their elements  Move nodes within tree & leave the elements  Preserving nodes & swapping Entry s means  Setting two references (one in each node)  If decide you want to move nodes around instead  Set two references for parents (one in each node)  Set another two references for left children (1 in each)

Swapping Data in a Heap  Two ways to swap Entry s in a heap  Preserve the nodes, but exchange their elements  Move nodes within tree & leave the elements  Preserving nodes & swapping Entry s means  Setting two references (one in each node)  If decide you want to move nodes around instead  Set two references for parents (one in each node)  Set another two references for left children (1 in each)  Swap right children in each node (2 more references)

Swapping Data in a Heap  Two ways to swap Entry s in a heap  Preserving nodes & swapping Entry s means  Setting two references (one in each node)  If decide you want to move nodes around instead  Set two references for parents (one in each node)  Set another two references for left children (1 in each)  Swap right children in each node (2 more references) Either way CAN work; Which do you choose?

Swapping Data in a Heap

Keys In a PriorityQueue  Must order keys used within PriorityQueue  What types could be used to achieve this ordering?

What Type For This Priority?

For Everything Else There Are…

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