1 / 16

COMP206-08S General Programming 2

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)

woods
Download Presentation

COMP206-08S General Programming 2

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. COMP206-08SGeneral Programming 2

  2. 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

  3. 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

  4. 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

  5. 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

  6. 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

  7. 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

  8. 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

  9. 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

  10. 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

  11. 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

  12. 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

  13. 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

  14. 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

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

  16. 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

More Related