160 likes | 248 Views
COMP206-08S General Programming 2. Lectures. Today – more ADTs, implementation and testing Priority Queues Heaps What’s in the test? KG.07 from 11-1pm (Friday 25 th Jan). Priority Queues. Recall stacks and queues (common abstract data types)
E N D
Lectures • Today – more ADTs, implementation and testing • Priority Queues • Heaps • What’s in the test? • KG.07 from 11-1pm (Friday 25th Jan) Department of Computer Science
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) Department of Computer Science
Code Example • PriorityQueue<Job> q = new PriorityQueue<Job>(); • 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 Department of Computer Science
Test program import java.util.PriorityQueue; publicclass TestPriorityQueue { publicstaticvoid main(String[] args) { PriorityQueue<Job> q = new PriorityQueue<Job>(); 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( )); } } Department of Computer Science
Job class publicclass Job implements Comparable { privateint priority; private String todo; public Job(int n, String s) { priority = n; todo = s; } public String toString() { return "priority = "+priority+" job = "+todo; } publicint compareTo(Object otherObject) { Job other = (Job) otherObject; if (priority < other.priority) return -1; if (priority > other.priority) return 1; return 0; } } Department of Computer Science
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 Department of Computer Science
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 Department of Computer Science
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) Department of Computer Science
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. Department of Computer Science
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 Department of Computer Science
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 2k-1 nodes but less than 2k nodes – so if n is the number of nodes 2k-1 <= n < 2k or k-1 <= log2(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. Department of Computer Science
Further advantages • Consider the following array: [0] [1] [2] [3] [4] [5] [6] [7] [8] … 20 75 43 84 90 57 71 93 … • 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 Department of Computer Science
Array implementation • Very efficient • No inner class of nodes needed – no links needed for children • Use index and child index computations Department of Computer Science
Heap class • See implementation • See HeapUtil, PQUtil • See TestStopWatch • Heap implementation is faster than the Java PriorityQueue implementation Department of Computer Science
Test on Friday 25th 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 Department of Computer Science