hashing-102-sp10

# hashing-102-sp10 - Hashing Text Read Weiss, 5.1 5.5 Goal...

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

Hashing Text Read Weiss, §5.1 – 5.5 Goal Perform inserts, deletes, and finds in constant average time Topics Hash table, hash function, collisions Collision handling Separate chaining Open addressing: linear probing , quadratic probing , double hashing Rehashing Load factor

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

View Full Document
Tree Structures Binary Search Trees AVL Trees
Tree Structures insert / delete / find worst average Binary Search Trees N log N AVL Trees log N

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

View Full Document
Goal Develop a structure that will allow user to insert/delete/find records in constant average time structure will be a table (relatively small) table completely contained in memory implemented by an array capitalizes on ability to access any element of the array in constant time
Hash Function Determines position of key in the array. Assume table (array) size is N Function f(x) maps any key x to an int between 0 and N− 1 For example, assume that N= 15, that key x is a non-negative integer between 0 and MAX_INT, and hash function f(x) = x % 15. (Hash functions for strings aggregate the character values --- see Weiss §5.2.)

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

View Full Document
Hash Function Let f(x) = x % 15. Then, if x = 25 129 35 2501 47 36 f(x) = 10 9 5 11 2 6 Storing the keys in the array is straightforward: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 _ _ 47 _ _ 35 36 _ _ 129 25 2501 _ _ _ Thus, delete and find can be done in O(1), and
Hash Function What happens when you try to insert: x = 65 ? x = 65 f(x) = 5 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 _ _ 47 _ _ 35 36 _ _ 129 25 2501 _ _ _ 65(?) This is called a collision .

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

View Full Document
Handling Collisions Separate Chaining Open Addressing Linear Probing Quadratic Probing Double Hashing
Handling Collisions Separate Chaining

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

View Full Document
Separate Chaining Let each array element be the head of a chain. 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 47 65 36 129 25 2501 35 Where would you store: 29, 16, 14, 99, 127 ?
Separate Chaining Let each array element be the head of a chain: Where would you store: 29, 16, 14, 99, 127 ? 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 16 47 65 36 127 99 25 2501 14 35 129 29 New keys go at the front of the relevant chain.

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

View Full Document
Separate Chaining: Disadvantages Parts of the array might never be used. As chains get longer, search time increases to O(n) in the worst case. Constructing new chain nodes is relatively
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 05/06/2010 for the course COMPUTER S 101 taught by Professor Sanaodeh during the Spring '08 term at NYU.

### Page1 / 46

hashing-102-sp10 - Hashing Text Read Weiss, 5.1 5.5 Goal...

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

View Full Document
Ask a homework question - tutors are online