Priority Queues Please snarf the code for todays

  • Slides: 9
Download presentation
Priority Queues Please snarf the code for today’s class and grab a handout

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

Correction from last class private int size(Tree. Node root){ if (root == null) return

Correction from last class private int size(Tree. Node root){ if (root == null) return 0; return 1 + size(root. left) + size(root. right); } public String find. Kth(Tree. Node root, int k){ if (root == null) return null; int left. Count = size(root. left); if (left. Count == k) { return root. info; } else if (k < left. Count){ return find. Kth(root. left, k); } else { return find. Kth(root. right, k-left. Count - 1); } }

A New Interesting Structure 1. Priority Queues – it works like a queue…mostly 2.

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

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

Priority Queue What does this print out? Priority. Queue<Integer> ex = new Priority. Queue<Integer>();

Priority Queue What does this print out? Priority. Queue<Integer> ex = new Priority. Queue<Integer>(); ex. add(2); ex. add(13); ex. add(9); ex. add(75); ex. add(4); while(!ex. is. Empty()) { 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: Best. Price • Snarf the code • Imagine we’ve got a

Priority Queue Problem: Best. Price • Snarf the code • Imagine we’ve got a class Best. Price, 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 buy. Cheapest(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

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

Efficient implementation in arrays Layer 1 20 75 84 96 90 91 20 75 Layer 2 43 57 71 Layer 3 Layer 4 93 43 Layer 1 Layer 2 84 90 57 Layer 3 71 96 91 93 Layer 4

Greedy Tree Score Imagine a binary tree where each node gives you a particular

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 greedy. Tree. Score 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