1 / 9

Priority Queues

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

alton
Download Presentation

Priority Queues

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. 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; intleftCount = 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); } }

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

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

  5. Priority Queue What does this print out? PriorityQueue<Integer> ex = newPriorityQueue<Integer>(); 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

  6. 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

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

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

  9. 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

More Related