Download Dictionary ADT Dictionary ADTs

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Corecursion wikipedia , lookup

Post-quantum cryptography wikipedia , lookup

Radix sort wikipedia , lookup

Binary search algorithm wikipedia , lookup

Lempel–Ziv–Welch wikipedia , lookup

Transcript
EECS 281: Data Structures and Algorithms
Dictionary Abstract Data Type
Dictionary ADT
„
„
„
Store elements so that they can be
quickly located using keys
Typically, useful additional information
in addition to key
Examples
– bank accounts with SSN as key
– student records with UMID or uniqname as
key
Dictionary ADTs
Types
„
„
„
„
Log File
Ordered Dictionary
Hash Table
Skip List
Operations
„
„
„
Search
Insertion
Removal
1
Dictionary ADTs
Stores items by key – element pairs
„ (k,e)
„ k and e may be of any type
„ k and e may be the same
„ In general, items with the same key
may be stored in same Dictionary
Unordered vs Ordered
Ordered
„
„
Relative order determined by comparator
between keys
Total Order relation defined on keys
Unordered
„
„
No order relation is assumed on keys
Only equality testing between keys
Dictionary ADT: Log File
Defn: implementation of Dictionary ADT
using a sequence to store items in
arbitrary order
Obviously, Unordered Dictionary
Useful implementation for case with many
insertions and few searches
Implemented as array (vector) or linked
list
2
Dictionary ADT: Log File
Search
„ Look through entire data set
„ O( )
Insertion
„ Always insert at end
„ O( )
Removal
„ First must find, then remove
„ O( )
Space, proportional to size of log file
„ O( )
Applications of Log Files
„
„
„
Database systems
File systems
Security audit trails
Dict. ADT: Ordered Dictionary
Defn: implementation of Dictionary ADT
in which usual operations may be used
and there exists an order relationship
between keys
Useful implementation for few insertions /
removals, but many searches
3
Binary Search
Iterative
int search(int a[], int v, int left, int right)
while (right >= left) {
int mid = (left + right)/2;
if (v == a[mid]) return mid;
if (v < a[mid])
right = mid - 1;
else
left = mid + 1;
}
return -1;
Binary Search
Recursive
int searchR(int a[], int v, int left, int right)
if (left > right) return -1;
int mid = (left + right)/2;
if (v == a[mid]) return mid;
if (v < a[mid])
return searchR(a[], v, left, mid - 1);
else
return searchR(a[], v, mid + 1, right);
}
Dict. ADT: Ordered Dictionary
Search
„
Binary Search (iterative or recursive)
„
O( )
Insertion
„
„
Always insert in correct location
O( ) to find location and O( ) to move subsequent
items
Removal
„
„
First must find, then remove
O( ) to find and O( ) to move subsequent items
4
Notes
„
„
„
„
Binary search is an example of divide-andconquer technique
Rule: An algorithm is O(log n) if each
constant time operation (e.g., CMP) can cut
the problem size by a fraction (e.g., half).
Corollary: If constant time is required to
reduce the problem size by a constant
amount, the algorithm is O(N).
Divide and conquer doesn’t work with linked
list, unfortunately
Summary: Dictionary ADT
„
Many implementations
– Log File (today)
– Ordered Dictionary (today)
– Hash Table (soon)
„
Different styles lead to different O( ) for
– search
– insertion
– removal
5