Presentation is loading. Please wait.

Presentation is loading. Please wait.

Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.

Similar presentations


Presentation on theme: "Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2."— Presentation transcript:

1 Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2

2 Department of Computer Science2 Lectures - Today – more ADTs, implementation and testing - Priority Queues - Heaps - What’s in the test? - KG.07 from 11-1pm (Friday 25 th Jan)

3 Department of Computer Science3 Priority Queues Recall stacks and queues (common abstract data types) Priority queue is similar (print jobs, work requests) Queues use FIFO – here we retrieve elements according to their priority (low is high) Insert – any order Remove – highest priority (take minimum element)

4 Department of Computer Science4 Code Example PriorityQueue q = new PriorityQueue (); q.add(new Job(5, “Clean pool”)); q.add(new Job(3, “Clean windows”)); q.add(new Job(3, “Clean carpets”)); q.add(new Job(1, “Clean self”)); Removes in order self, windows, carpets, pool (q.remove()) Note that priority numbers determine order of removal

5 Department of Computer Science5 Test program import java.util.PriorityQueue; public class TestPriorityQueue { public static void main(String[] args) { PriorityQueue q = new PriorityQueue (); q.add(new Job(5, "Clean pool")); q.add(new Job(3, "Clean windows")); q.add(new Job(3, "Clean carpets")); q.add(new Job(1, "Clean self")); while (q.size( ) > 0) System.out.println(q.remove( )); }

6 Department of Computer Science6 Job class public class Job implements Comparable { private int priority; private String todo; public Job(int n, String s) { priority = n; todo = s; } public String toString() { return "priority = "+priority+" job = "+todo; } public int compareTo(Object otherObject) { Job other = (Job) otherObject; if (priority < other.priority) return -1; if (priority > other.priority) return 1; return 0; }

7 Department of Computer Science7 Implementation Could use a linked list (elements are Jobs) Add new elements to list at front Remove elements according to priority Add would be fast Remove would be slow Could use a BinarySearchTree – easy to locate elements for add, remove but the tree might require major rearrangement

8 Department of Computer Science8 Heaps – most elegant structure invented? Heap is a binary tree with two properties  A heap is almost complete – all nodes are filled in except the last level may have some missing towards the right  The tree has the heap property – all nodes store values that are at most as large as the values stored in their descendants

9 Department of Computer Science9 The heap property => smallest element is at the root Binary Search Trees are very similar but they can have arbitrary shape In a heap the left and right subtrees both store elements that are larger than the root (not smaller and larger – as per BST)

10 Department of Computer Science10 Heap Insertion Insert 60 into tree  Add slot (find by scanning lowest level from the left to right) to the end of the tree  Demote parents – if parents are larger than new value – keep demoting as long as parent is larger than inserted value.  We are now at the root or at a parent node which has a parent smaller than the inserted value. Insert the value at this node.

11 Department of Computer Science11 Heap Removal Only need to consider a single case! Example remove 20  Remove the element at the root  Move the last element (largest) into the root and remove the last node (the heap could be a non- heap at this point).  Promote the smaller child of the root - repeatedly

12 Department of Computer Science12 Complexity of operations Both operations visit at most k nodes (k is the height of the tree – number of levels) For k nodes we have a tree of at least 2 k-1 nodes but less than 2 k nodes – so if n is the number of nodes 2 k-1 <= n < 2 k or k-1 <= log 2 (n) < k For n nodes the insert and removal steps take O(log n) steps Degenerate BST is a linked list so operations are O(n) in this case.

13 Department of Computer Science13 Further advantages Consider the following array: [0][1][2][3][4][5][6][7][8] … 2075438490577193 … Store the levels of the tree from left to right in an array Indices of children are easy to find – for index i the children are at 2i and 2i + 1

14 Department of Computer Science14 Array implementation Very efficient No inner class of nodes needed – no links needed for children Use index and child index computations

15 Department of Computer Science15 Heap class See implementation See HeapUtil, PQUtil See TestStopWatch Heap implementation is faster than the Java PriorityQueue implementation

16 Department of Computer Science16 Test on Friday 25 th Jan KG.07 – spread yourselves out 2 hours so be prompt at 11am (11am-1pm) Topics  Java multi-choice  Assignment and Lecture topics  Arrays, ArrayLists, Inheritance, Defining Classes  BST, Stacks, Sorting  HashSet, TreeSet, PQs, Heap


Download ppt "Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2."

Similar presentations


Ads by Google