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<String> ex = new PriorityQueue<String>(); 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<Integer> ex = new PriorityQueue<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 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 20 75 84 96 90 91 20 75 Layer 2 43 57 Layer 3 71 Layer 4 93 43 Layer 1 Layer 2 84 90 57 Layer 3 71 96 91 Layer 4 93 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. 2 0 6 9 7 5 This should be the tree that your example generates 5 4 3 7 9 2 6 5 7