Priority Queues Please snarf the code for today’s class and grab a handout.

Slides:



Advertisements
Similar presentations
Binary Trees CSC 220. Your Observations (so far data structures) Array –Unordered Add, delete, search –Ordered Linked List –??
Advertisements

Recurrence Relations As you arrive: Get out a piece of paper and and pen. We’re gonna do some math in class today and you’d want to follow along. Put your.
PRIORITY QUEUES AND HEAPS Lecture 19 CS2110 Spring
CS 332: Algorithms Binary Search Trees. Review: Dynamic Sets ● Next few lectures will focus on data structures rather than straight algorithms ● In particular,
CS 206 Introduction to Computer Science II 03 / 23 / 2009 Instructor: Michael Eckmann.
Priority Queues. 2 Priority queue A stack is first in, last out A queue is first in, first out A priority queue is least-first-out The “smallest” element.
Advanced Data Structures Chapter 16. Priority Queues Collection of elements each of which has a priority. Does not maintain a first-in, first-out discipline.
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.
Trees Chapter 8. Chapter 8: Trees2 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information To learn how.
© 2004 Goodrich, Tamassia Greedy Method and Compression1 The Greedy Method and Text Compression.
CS 206 Introduction to Computer Science II 11 / 04 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 20 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 14 / 2009 Instructor: Michael Eckmann.
Chapter 10 Heaps Anshuman Razdan Div of Computing Studies
Heaps & Priority Queues Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Priority Queues. Priority queue A stack is first in, last out A queue is first in, first out A priority queue is least-first-out –The “smallest” element.
1 CS211, Lecture 20 Priority queues and Heaps Readings: Weiss, sec. 6.9, secs When they've got two queues going, there's never any queue!
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.
Source: Muangsin / Weiss1 Priority Queue (Heap) A kind of queue Dequeue gets element with the highest priority Priority is based on a comparable value.
CST 230 Razdan et alhttp://dcst2.east.asu.edu/~razdan/cst230/ Razdan with contribution from others 1 Tree Traversals Pre-order traversal –process root.
David Luebke 1 7/2/2015 ITCS 6114 Binary Search Trees.
Week 10: Heap and Priority queue. Any feature here?
CS 46B: Introduction to Data Structures July 30 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak.
Priority Queues and Heaps Bryce Boe 2013/11/20 CS24, Fall 2013.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
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.
1 EE 355 Unit 14 A-Star Algorithm & Heaps/Priority Queues Mark Redekopp.
Lecture Objectives  To learn how to use a Huffman tree to encode characters using fewer bytes than ASCII or Unicode, resulting in smaller files and reduced.
COMP 103 Priority Queues, Partially Ordered Trees and Heaps.
CS 146: Data Structures and Algorithms June 23 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
The Binary Heap. Binary Heap Looks similar to a binary search tree BUT all the values stored in the subtree rooted at a node are greater than or equal.
Priority Queues and Binary Heaps Chapter Trees Some animals are more equal than others A queue is a FIFO data structure the first element.
1 Heaps and Priority Queues Starring: Min Heap Co-Starring: Max Heap.
CSC 213 – Large Scale Programming Lecture 15: Heap-based Priority Queue.
1 Heaps and Priority Queues v2 Starring: Min Heap Co-Starring: Max Heap.
CE 221 Data Structures and Algorithms Chapter 6: Priority Queues (Binary Heaps) Text: Read Weiss, §6.1 – 6.3 1Izmir University of Economics.
Week 13 - Friday.  What did we talk about last time?  Sorting  Insertion sort  Merge sort  Started quicksort.
CS223 Advanced Data Structures and Algorithms 1 Priority Queue and Binary Heap Neil Tang 02/09/2010.
Heaps & Priority Queues
Lecture 8 : Priority Queue Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University.
Trees As you arrive: 1.Snarf the code for today’s class 2.Pick up a handout 3.Go ahead and start on the first page of the handout.
Intro. to Data Structures Chapter 6 Priority Queue (Heap) Veera Muangsin, Dept. of Computer Engineering, Chulalongkorn University 1 Priority Queue.
AVL Trees and Heaps. AVL Trees So far balancing the tree was done globally Basically every node was involved in the balance operation Tree balancing can.
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,
Properties: -The value in each node is greater than all values in the node’s subtrees -Complete tree! (fills up from left to right) Max 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.
Data Structures and Algorithms for Information Processing
CS 201 Data Structures and Algorithms
The Greedy Method and Text Compression
CSCE 3100 Data Structures and Algorithm Analysis
- Alan Perlis Heaps "You think you know when you can learn,
CSCI2100 Data Structures Tutorial 7
Chapter 8 – Binary Search Tree
Priority Queues.
Priority Queues.
CSCE 3110 Data Structures and Algorithm Analysis
CE 221 Data Structures and Algorithms
A Heap Implementation Chapter 26 Adapted from Pearson Education, Inc.
Priority Queues & Heaps
CSCE 3110 Data Structures and Algorithm Analysis
Heaps and Priority Queues
CSCE 3110 Data Structures and Algorithm Analysis
Computer Science 2 More Trees.
1 Lecture 10 CS2013.
Presentation transcript:

Priority Queues Please snarf the code for today’s class and grab a handout

Correction from last class private int size(TreeNode root){ if (root == null) return 0; return1 + size(root.left) + size(root.right); } public String findKth(TreeNode root, int k){ if (root == null) return null; int leftCount = size(root.left); if (leftCount == k) { return root.info; } else if (k < leftCount){ return findKth(root.left,k); } else { return findKth(root.right,k-leftCount - 1); }

A New Interesting Structure 1.Priority Queues – it works like a queue…mostly 2.Heaps, an efficient structure for priority queues 3.Another trickier priority queue problem

Priority Queue You add to it like a normal queue But when you remove, it gets removed in order of size (smallest first) PriorityQueue ex = new PriorityQueue (); ex.add("hello"); ex.add("cs100"); ex.add("class"); while(!ex.isEmpty()) { System.out.println(ex.remove()); } // Prints: // class // cs100 // hello

Priority Queue What does this print out? PriorityQueue ex = new PriorityQueue (); ex.add(2); ex.add(13); ex.add(9); ex.add(75); ex.add(4); while(!ex.isEmpty()) { System.out.println(ex.remove()); } Remember that you can add to priority queues in any order, but you always remove smallest first

Priority Queue Problem: BestPrice Snarf the code Imagine we’ve got a class BestPrice, that tracks the best price of an item we’re looking to buy. Everytime we find a new price we call add to add the price we’re looking for. Then when someone calls buyCheapest(n) we buy n items, using the cheapest offer we have then the second cheapest, etc. We return the total cost to buy n items. Take a look at the sample code in main if this in unclear. Hint: make 1 instance variable – a priority queue

How Heaps Work Read the handout and follow the examples Then solve problems 34 and 35 at the end

Efficient implementation in arrays Layer 1 Layer 2 Layer 3 Layer 4 Layer 1Layer 3Layer 4Layer 2

Greedy Tree Score Imagine a binary tree where each node gives you a particular score. Your goal is to get a high score by selecting nodes. But you’re only allowed to select a node if you’ve already selected its parent. (so to get the 9 in the lower left, you must also select 2,0, and 6 first) One algorithm to get a pretty good score is to always select the node that has the highest value of the nodes you can currently select. So first you would select 2. Then you would select 5 (best of [0,5]). Then you would select 3 [best of [0,3,2]. Write the function greedyTreeScore that computes your overall score using this approach, given a tree and the number of nodes you are allowed to select. If you finish early write a function that computes the best possible score given a tree and a number of nodes you are allowed to select. When you are finished submit via ambient. This should be the tree that your example generates