Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Priority Queues Please snarf the code for today’s class and grab a handout."— 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; 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); }

3 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

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 ex = new PriorityQueue (); 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 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

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 20758496919093435771 20754384905771969193 Layer 1 Layer 2 Layer 3 Layer 4 Layer 1Layer 3Layer 4Layer 2

9 Greedy Tree Score 206957475396257 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


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

Similar presentations


Ads by Google