{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Review #17 Heaps

Review #17 Heaps - Java Advanced Review#17 Heaps Hash...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Java Advanced Review #17 - Heaps, Hash Tables, and Priority Queues - Briefly Heap - A heap is a complete binary tree with the property that every parent has a value that is greater or smaller than its children. If the parent's value is greater than the children's values, the heap is called a maxheap . If the parent's value is less than the children's values, the heap is called a minheap . Add a node to a heap - Big O is O(log n) Delete a node from a heap - Big O is O(log n) Heapsort - Big O is O(n log n) Implementing heaps - usually done in an array: root is at a[0] the children of the node at a[i] are at a[2i+1] and a[2i+2] HashTables hashing - a hash function is any function that maps a data element to an index in an array need to know how many cells in the array create a hash function that will spread the elements evenly into the array hashtable example: hash a date into an array of 10 items using the function (year+month+day)%10 December 25, 2004 would hash to (12+25+2004)%10 or 1 July 4, 1776 would hash to (7+4+1776)%10 or 7 May 1, 1981 would hash to (5+1+1981)%10 or 7 - OOPS! a collision! handling collisions: 1. could move it forward to the next empty cell % number of cells - called linear probing 2. could store them in an array of linked lists - called chaining or placing the items in buckets 3. rehashing or quadratic processing - add some constant 2 to the index and rehash using the same function 4. plus many others HashTable Class public class HashTable { // chaining private int size; private ListNode[] table; public HashTable(int numSlots) { size = numSlots; table = new ListNode[size]; } public void add(Object obj) { ListNode element = new ListNode(obj, null); int index = obj.hashCode() % size; if(table[index] == null) table[index] = element; else { ListNode current = table[index]; while(current.getNext() != null) current = current.getNext(); current.setNext(element); } } public String toString() { String str = ""; for(int i=0; i<size; i++) { str += i + ": "; ListNode current = table[i]; while(current != null) { str += current.getValue() + " "; current = current.getNext(); } str += "\n"; } return str; } } © Charlotte Scroggs http://www.cistests.com Java Review Review 17 - Heaps, Hash Tables and Priority Queues - Page 1 // code from main HashTable table = new HashTable(7); table.add(new Integer(17)); table.add(new Integer(22)); table.add(new Integer(14)); table.add(new Integer(29)); table.add(new Integer(35)); table.add(new Integer(12)); System.out.print(table); // output 0: 14 35 1: 22 29 2: 3: 17 4: 5: 12 6:
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Priority Queues Priority queues are not really queues at all. A priority queue is a collection of items, all of the same type, that contain a data field and priority. Items in a priority queue are ordered first by priority and then by the ordered entered. An example of a priority queue is when students sign up for parking at the beginning of school. Seniors have first priority in the
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 11

Review #17 Heaps - Java Advanced Review#17 Heaps Hash...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon bookmark
Ask a homework question - tutors are online