COP3502_30_HashTables

# COP3502_30_HashTables - Hash Tables Computer Science...

This preview shows pages 1–9. Sign up to view the full content.

Computer Science Department University of Central Florida Hash Tables COP 3502 – Computer Science I

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

View Full Document
Hash Tables page 2 Terminology Table An abstract data type that stores & retrieves records according to their search key values Record Each individual row in the table Example: A database of student records So each record will have a pid, first name, last name, SSN, address, phone, email, etc.
Hash Tables page 3 Record Example 9903030 0056789 0012345 0033333 tom david andy betty 73 56.8 81.5 90 sid (key) name score 9908080 bill 49 ... Consider this problem. We want to store 1,000 student records and search them by student id. ... This is an example of a table. Each individual row is a record.

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

View Full Document
Hash Tables page 4 Motivation Problem: Given this table of records We need to be able to: Add new records Delete records Search for records What’s the most efficient way of doing this?
Hash Tables page 5 Motivation Problem: What’s the most efficient way of doing this? Use an array to store the records, in unsorted order Running time: Adding a record: O(1) since we simply add at the end of the unsorted array Deleting a record: Very slow, or O(n), since we have to search through the entire array to find the desired record to delete We then have a “hole” in the array. We can quickly fill that hole by moving the last element into it, which can happen in O(1) time. Search for a record: Very slow, or O(n), since we search through the entire table

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

View Full Document
Hash Tables page 6 Motivation Problem: What’s the most efficient way of doing this? Use an array to store the records, in sorted order Running time: Adding a record: Must insert at correct position And then ALL other records, after insertion spot, must be moved Very slow, or O(n) Deleting a record: Must find the record to delete, O(n) Must fill the “hole”, which means moving all other items, O(n) Search for a record: Binary search! Fast, or O(logn)
Hash Tables page 7 Motivation Problem: What’s the most efficient way of doing this? Use a binary search tree to store the records Running time: Adding a record: Inserting into proper position in BST Fast, or O(logn) Deleting a record: Must find correct position to delete Fast, or O(logn) Search for a record: Also Fast, or O(logn)

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

View Full Document
page 8 Motivation Problem: What’s the most efficient way of doing this? Use a binary search tree to store the records BSTs seem to be the best solution to this But there’s something that is WAAAAAY faster Adding, Deleting, and Searching are all O(1) : CONSTANT time A very simple, naive solution that you could come up with before even taking this class Just use an array! But a special type of an array. Specially, use an array that is SOOOOO large that every
This is the end of the preview. Sign up to access the rest of the document.

## This document was uploaded on 09/21/2011.

### Page1 / 53

COP3502_30_HashTables - Hash Tables Computer Science...

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

View Full Document
Ask a homework question - tutors are online