Tables and Priority Queues Tables Previously: each node stored one - - PowerPoint PPT Presentation
Tables and Priority Queues Tables Previously: each node stored one - - PowerPoint PPT Presentation
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
Binary searching & introduction to trees
2 CMPS 12B, UC Santa Cruz
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
3 CMPS 12B, UC Santa Cruz
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
- rder
Binary searching & introduction to trees
4 CMPS 12B, UC Santa Cruz
SearchKeys
public abstract class KeyedItem { private Comparable searchKey; public KeyedItem(Comparable key) { searchKey = key; } public Comparable getKey() { return searchKey; } }
Binary searching & introduction to trees
5 CMPS 12B, UC Santa Cruz
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
6 CMPS 12B, UC Santa Cruz
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
7 CMPS 12B, UC Santa Cruz
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
8 CMPS 12B, UC Santa Cruz
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
9 CMPS 12B, UC Santa Cruz
Comparison of Implementation Alternatives
O(n) O(n) O(n) O(1) Unsorted array Traversal Retrieval Deletion Insertion O(n) O(logn) O(logn) O(logn) Binary search tree O(n) O(n) O(n) O(n) Sorted linked list O(n) O(logn) O(n) O(n) Sorted array O(n) O(n) O(n) O(1) Unsorted linked list
Binary searching & introduction to trees
10 CMPS 12B, UC Santa Cruz
See Table Code
Binary searching & introduction to trees
11 CMPS 12B, UC Santa Cruz
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
12 CMPS 12B, UC Santa Cruz
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
13 CMPS 12B, UC Santa Cruz
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
- f 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
14 CMPS 12B, UC Santa Cruz
Array-based Implementation of a Heap
10 9 6 5 2 3
Breadth-first order in the array Complete tree ⇒ no gaps in array
2 5 3 6 9 10
Binary searching & introduction to trees
15 CMPS 12B, UC Santa Cruz
Inserting into the Heap
10 9 6 5 2 3
Breadth-first order in the array Complete tree ⇒ no gaps in array
1 2 5 3 6 9 10 1
Binary searching & introduction to trees
16 CMPS 12B, UC Santa Cruz
Deleting from the Heap (1)
10 9 6 5 2 3
Always delete the root
It has the highest priority
1 2 5 3 6 9 10 1
Binary searching & introduction to trees
17 CMPS 12B, UC Santa Cruz
Deleting from the Heap (2)
9 6 5 2 3
Oops, now we have two heaps
1 2 5 3 6 9 1
Binary searching & introduction to trees
18 CMPS 12B, UC Santa Cruz
Deleting from the Heap (3)
9 6 5 2 3
Repairing the heap: Move the last element to the top
1 2 5 3 6 9 1
Binary searching & introduction to trees
19 CMPS 12B, UC Santa Cruz
Deleting from the Heap (4)
9 6 5 2 3
Repairing the heap: Now “trickle down” by comparing
and swapping until heap is restored
2 5 3 6 9 1 1
Binary searching & introduction to trees
20 CMPS 12B, UC Santa Cruz
Deleting from the Heap (5)
1 6 5 2 3
Repairing the heap: Now “trickle down” by comparing
and swapping until heap is restored
2 5 3 6 1 9 9
Binary searching & introduction to trees
21 CMPS 12B, UC Santa Cruz
Deleting from the Heap (6)
3 6 5 2 1
Calculating indices:
leftchild = 2*parent+1 Rightchild = 2*parent+2
2 5 1 6 3 9 9
Binary searching & introduction to trees
22 CMPS 12B, UC Santa Cruz
Deleting from the Heap (7)
6 3 5 2 1
Now it’s a heap again Total time O(logn)
2 5 1 3 6 9 9
Binary searching & introduction to trees
23 CMPS 12B, UC Santa Cruz
Inserting to a heap
The opposite of deleting Insert at the bottom, then “trickle up”
Binary searching & introduction to trees
24 CMPS 12B, UC Santa Cruz
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
25 CMPS 12B, UC Santa Cruz
Heapsort
Make it a heap by doing
heapRebuild to each node
Starting with leaf nodes
2 5 1 3 6 9
Binary searching & introduction to trees
26 CMPS 12B, UC Santa Cruz
Heapsort
Swap first and “last”
2 9 1 3 6 5
LAST
Binary searching & introduction to trees
27 CMPS 12B, UC Santa Cruz
Heapsort
last = last – 1 heapRebuild()
2 9 1 3 5 6
LAST
Binary searching & introduction to trees
28 CMPS 12B, UC Santa Cruz
Heapsort
Swap first and “last”
6 9 1 3 5 2
LAST
Binary searching & introduction to trees
29 CMPS 12B, UC Santa Cruz
Heapsort
last = last - 1 heapRebuild()
6 9 1 3 2 5
LAST
Binary searching & introduction to trees
30 CMPS 12B, UC Santa Cruz
Heapsort
Swap first and “last”
6 9 5 3 2 1
LAST
Binary searching & introduction to trees
31 CMPS 12B, UC Santa Cruz
Heapsort
last = last - 1 heapRebuild()
6 9 5 1 2 3
LAST
Binary searching & introduction to trees
32 CMPS 12B, UC Santa Cruz
Heapsort
Swap first and “last”
6 9 5 3 2 1
LAST
Binary searching & introduction to trees
33 CMPS 12B, UC Santa Cruz
Heapsort
last = last – 1 heapRebuild()
6 9 5 3 1 2
LAST
Binary searching & introduction to trees
34 CMPS 12B, UC Santa Cruz
Heapsort
Swap first and “last” Done!
6 9 5 3 2 1
LAST