Tables and Priority Queues
Tables � Previously: each node stored one item � Now: Groups of related information in records � Records indexed by key � Key is just one of the pieces of information – the one you want to be able to search on � Support fast searching by key – O(logn) � Also support scanning for other information in records � But not as fast – O(n) instead of O(logn) � Examples � Student records � List of pirated MP3s Binary searching & introduction to trees CMPS 12B, UC Santa Cruz 2
Table ADT – Operations 1. Create an empty table 2. Determine whether a table is empty 3. Determine the number of items in a table 4. Insert a new item into the table 5. Delete the item with a given search key from the table 6. Retrieve the item with a given search key from the table 7. Traverse the items in a table in sorted search-key order Binary searching & introduction to trees CMPS 12B, UC Santa Cruz 3
SearchKeys public abstract class KeyedItem { private Comparable searchKey; public KeyedItem(Comparable key) { searchKey = key; } public Comparable getKey() { return searchKey; } } Binary searching & introduction to trees CMPS 12B, UC Santa Cruz 4
City extends KeyedItem public class City extends KeyedItem { private String country; private int population; public City(String theCity, String theCountry, int pop) { super(theCity); country = theCountry; population = pop; } public String toString() { return getKey() + ", " + country + " " + population; } public void setPopulation(int pop) { population = pop; } public int getPopulation() { return population; } public String getCountry() { return Country; } } Binary searching & introduction to trees CMPS 12B, UC Santa Cruz 5
TableInterface import searchkeys.*; public interface TableInterface { public boolean tableIsEmpty(); public int tableLength(); public void tableInsert(KeyedItem newItem) throws TableException; public boolean tableDelete(Comparable searchKey); public KeyedItem tableRetrieve( Comparable searchKey); } Binary searching & introduction to trees CMPS 12B, UC Santa Cruz 6
Implementing the Table � Linear implementations � Unsorted array � Unsorted linked list � Sorted (by key) array � Sorted (by key) linked list � Non-linear implementations � Binary search tree � Criteria � What operations are needed? � How often will different operations be used? � How important are the different operations? Binary searching & introduction to trees CMPS 12B, UC Santa Cruz 7
Examples � Small, unsorted data, fixed size � Array � Small, unsorted data, variable size � Linked list � Small, sorted data, variable size � Linked list � Large, unsorted data, variable size � Linked list � Large, sorted data, variable size � Binary search tree Binary searching & introduction to trees CMPS 12B, UC Santa Cruz 8
Comparison of Implementation Alternatives Insertion Deletion Retrieval Traversal Unsorted O(1) O(n) O(n) O(n) array Unsorted O(1) O(n) O(n) O(n) linked list Sorted O(n) O(n) O(logn) O(n) array Sorted O(n) O(n) O(n) O(n) linked list Binary O(logn) O(logn) O(logn) O(n) search tree Binary searching & introduction to trees CMPS 12B, UC Santa Cruz 9
See Table Code Binary searching & introduction to trees CMPS 12B, UC Santa Cruz 10
Priority Queue � It is often useful to assign a priority to different data items � so that urgent items can be processed first � Examples � Time in a todo list � Importance in a list of phone calls to make � … Binary searching & introduction to trees CMPS 12B, UC Santa Cruz 11
Priority Queue ADT 1. Create an empty priority queue 2. Determine whether a priority queue is empty 3. Insert a new item into a priority queue 4. Retrieve and then delete the item in a priority queue with the highest priority Binary searching & introduction to trees CMPS 12B, UC Santa Cruz 12
Implementing Priority Queues: Heaps � A complete binary tree that is � Empty, or � Whose root has heaps as its subtrees, and � whose root contains a key greater than or equal to the key of each of its children � Heaps are always balanced � There is no order on the values of the keys of the two children of a node � Unlike binary search trees Binary searching & introduction to trees CMPS 12B, UC Santa Cruz 13
Array-based Implementation of a Heap 10 10 9 6 9 6 3 2 5 3 2 5 � Breadth-first order in the array � Complete tree ⇒ no gaps in array Binary searching & introduction to trees CMPS 12B, UC Santa Cruz 14
Inserting into the Heap 10 10 9 6 9 6 3 2 5 1 5 3 2 1 � Breadth-first order in the array � Complete tree ⇒ no gaps in array Binary searching & introduction to trees CMPS 12B, UC Santa Cruz 15
Deleting from the Heap (1) 10 10 9 6 9 6 3 2 5 1 5 3 2 1 � Always delete the root � It has the highest priority Binary searching & introduction to trees CMPS 12B, UC Santa Cruz 16
Deleting from the Heap (2) 9 6 9 6 3 2 5 1 5 3 2 1 � Oops, now we have two heaps Binary searching & introduction to trees CMPS 12B, UC Santa Cruz 17
Deleting from the Heap (3) 9 6 9 6 3 2 5 1 5 3 2 1 � Repairing the heap: � Move the last element to the top Binary searching & introduction to trees CMPS 12B, UC Santa Cruz 18
Deleting from the Heap (4) 1 1 9 6 9 6 3 2 5 5 3 2 � Repairing the heap: � Now “trickle down” by comparing and swapping until heap is restored Binary searching & introduction to trees CMPS 12B, UC Santa Cruz 19
Deleting from the Heap (5) 9 9 1 6 1 6 3 2 5 5 3 2 � Repairing the heap: � Now “trickle down” by comparing and swapping until heap is restored Binary searching & introduction to trees CMPS 12B, UC Santa Cruz 20
Deleting from the Heap (6) 9 9 3 6 3 6 1 2 5 5 1 2 � Calculating indices: � leftchild = 2*parent+1 � Rightchild = 2*parent+2 Binary searching & introduction to trees CMPS 12B, UC Santa Cruz 21
Deleting from the Heap (7) 9 9 6 3 6 3 1 2 5 5 1 2 � Now it’s a heap again � Total time O(logn) Binary searching & introduction to trees CMPS 12B, UC Santa Cruz 22
Inserting to a heap � The opposite of deleting � Insert at the bottom, then “trickle up” Binary searching & introduction to trees CMPS 12B, UC Santa Cruz 23
Heapsort One way � Insert everything into the heap, then � Take everything back out � Faster way � 1. Make it a heap for(int index = n/2; n >= 0; n--) { heapRebuild(array, index, n); } 2. Swap the first item (largest) with the last (last--) 3. heapRebuild(array, index, last); 4. Repeat until all items are in the right place Binary searching & introduction to trees CMPS 12B, UC Santa Cruz 24
Heapsort � Make it a heap by doing 9 heapRebuild to each node 6 � Starting with leaf nodes 3 1 2 5 Binary searching & introduction to trees CMPS 12B, UC Santa Cruz 25
Heapsort � Swap first and “last” 5 6 3 1 2 9 � LAST Binary searching & introduction to trees CMPS 12B, UC Santa Cruz 26
Heapsort � last = last – 1 6 � heapRebuild() 5 3 1 2 � LAST 9 Binary searching & introduction to trees CMPS 12B, UC Santa Cruz 27
Heapsort � Swap first and “last” 2 5 3 1 6 � LAST 9 Binary searching & introduction to trees CMPS 12B, UC Santa Cruz 28
Heapsort � last = last - 1 5 � heapRebuild() 2 3 1 � LAST 6 9 Binary searching & introduction to trees CMPS 12B, UC Santa Cruz 29
Heapsort � Swap first and “last” 1 2 3 5 � LAST 6 9 Binary searching & introduction to trees CMPS 12B, UC Santa Cruz 30
Heapsort � last = last - 1 3 � heapRebuild() 2 1 � LAST 5 6 9 Binary searching & introduction to trees CMPS 12B, UC Santa Cruz 31
Heapsort � Swap first and “last” 1 2 3 � LAST 5 6 9 Binary searching & introduction to trees CMPS 12B, UC Santa Cruz 32
Heapsort � last = last – 1 2 � heapRebuild() 1 � LAST 3 5 6 9 Binary searching & introduction to trees CMPS 12B, UC Santa Cruz 33
Heapsort � Swap first and “last” 1 2 � LAST � Done! 3 5 6 9 Binary searching & introduction to trees CMPS 12B, UC Santa Cruz 34
Recommend
More recommend