





Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Notes; Professor: Howe; Class: Algorithms and Data Structures; Subject: Computer Science; University: Colorado State University; Term: Unknown 1989;
Typology: Study notes
1 / 9
This page cannot be seen from the preview
Don't miss anything!






// Determines whether a table is empty
// Determines the number of items in a table
// Traverses a table in sorted search key order.
//Inserts newItem into a table whose items have distinct
// search keys that differ from newItem’s search key.
// throws exception if unsuccessful.
// Deletes from a table the item whose search key equals searchKey.
// Returns true if successful, false if no item found.
// Returns item whose search key equals searchKey. Returns null
// if not there.
Table Items
Table or Dictionary Example
2 3/24/06 Unwritten Natasha Bedingfeld
4 3/24/06 Temperature Sean Paul
3 3/24/06 Move Along The All-American Rejects
5 3/24/06 Beep The Pussycat Dolls
1 3/24/06 Bad Day Daniel Powter
iTunes Top Songs
Table Items Example
Public class TopTunes extends KeyedItem {
private String title; // search key
private int rank; private String artist;
private String date;
public TopTunes(String theTitle, int theRank, …) {
super(theTitle); rank = theRank; …}
Table Interface
public interface TableInterface,
KT extends Comparable > {
// Precondition for all operations:
// No two items of the table have the same search key.
// The table's items are sorted by search key.
public boolean tableIsEmpty();
// Determines whether a table is empty.
// Postcondition: Returns true if the table is empty; false otherwise
public int tableLength();
// Determines the length of a table.
// Postcondition: Returns the number of items in the table.
Priority Queue ADT Operations
createPQueue()
pqIsEmpty():boolean {query}
pqInsert(in newItem:PQItemType) throws PQueueException
pqDelete():PQItemType
Priority Queue – Implementation
Sorted ArrayList
location of maximum element – position 0
add – find the insertion location
Unsorted ArrayList
add – at end of vector
find the maximum element (linear search)
Heaps
Characteristics
root of tree holds maximum value (global)
each node is greater than its two children (local)
(this is called: “the heap property”)
Heap Examples - Validity
Valid
Valid
Not Valid
Heap ADT
// determines if empty
// inserts newItem based on its search key. Throws
// exception if heap is full
// retrieves and then deletes heap’s root item, which has
// largest search key
Implementation:
ArrayList-based Heap
Addressing
ArrayList-based Binary Tree –
Priority Queue Example
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
30 5 25 20 15
Why is there so much wasted space?
ArrayList-based Heap –
Example
0 1 2 3 4 5 6
30 20 25 10 15 5
What is the property of a complete heap in an ArrayList?
Heap Operations - heapInsert
put a new value into first open position (maintaining
completeness)
percolate values up
Re-enforcing the heap property
swap with parent until in the right place
0 1 2 3 4 5 6
7 Add 36 to the heap
Heap Insert Pseudocode
// insert newItem into bottom of tree
items.add(newItem,items.size())
// trickle new item up to appropriate spot
place = items.size()-
parent = (place-1)/
while ((parent >= 0) and (items.get(place) > items.get(parent))) {
swap data at position place and position parent
place = parent
parent = (place-1)/
Heap operations – heapDelete
swap with maximum child as necessary
0 1 2 3 4 5 6
Delete from heap
Heap operations – heapDelete
swap with maximum child as necessary
0 1 2 3 4 5 6
Delete from heap
Save 36
Heap operations – heapDelete
swap with maximum child as necessary
0 1 2 3 4 5 6
Delete from heap
Return 36
heapDelete Pseudocode
heapRebuild Pseudocode
heapRebuild(items:ArrayList, root:integer, size:integer)
if (root is not a leaf) {
child = 2 * root + 1 // left child
if (root has right child) {
rightChild = child + 1
if (items.get(rightChild).getKey() > items.get(child).getKey()) {
child = rightChild } } // larger child
if (items.get(root).getKey() < items.get(child).getKey()) {
swap data at position root with position child
heapRebuild(items, child, size)
ArrayList-based Heaps -
Order of Complexity
delete
insert
Best Case Worst Case
O(c)
O(log n)
because tree is complete
O(c)
O(n)
because of ensure-capacity