cs171 introduction to computer science ii hash tablescheung/courses/171/cs171-2012-li/... · 2019....

25
CS171 Introduction to Computer Science II Hash Tables Hash Tables

Upload: others

Post on 30-Aug-2021

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS171 Introduction to Computer Science II Hash Tablescheung/Courses/171/CS171-2012-Li/... · 2019. 10. 27. · CS171 Introduction to Computer Science II Hash Tables. Review •Sequential

CS171 Introduction to Computer Science II

Hash TablesHash Tables

Page 2: CS171 Introduction to Computer Science II Hash Tablescheung/Courses/171/CS171-2012-Li/... · 2019. 10. 27. · CS171 Introduction to Computer Science II Hash Tables. Review •Sequential

Review

• Sequential search using linked list

• Binary search using ordered arrays

• Binary search tree (BST)

• 2-3 tree (balanced search tree, implemented • 2-3 tree (balanced search tree, implemented

as red black tree)

Page 3: CS171 Introduction to Computer Science II Hash Tablescheung/Courses/171/CS171-2012-Li/... · 2019. 10. 27. · CS171 Introduction to Computer Science II Hash Tables. Review •Sequential
Page 4: CS171 Introduction to Computer Science II Hash Tablescheung/Courses/171/CS171-2012-Li/... · 2019. 10. 27. · CS171 Introduction to Computer Science II Hash Tables. Review •Sequential
Page 5: CS171 Introduction to Computer Science II Hash Tablescheung/Courses/171/CS171-2012-Li/... · 2019. 10. 27. · CS171 Introduction to Computer Science II Hash Tables. Review •Sequential
Page 6: CS171 Introduction to Computer Science II Hash Tablescheung/Courses/171/CS171-2012-Li/... · 2019. 10. 27. · CS171 Introduction to Computer Science II Hash Tables. Review •Sequential

Hash Tables

• A hash table for a given key type consists of

– Hash function h

– Array (called table) of size N

6

– Array (called table) of size N

Page 7: CS171 Introduction to Computer Science II Hash Tablescheung/Courses/171/CS171-2012-Li/... · 2019. 10. 27. · CS171 Introduction to Computer Science II Hash Tables. Review •Sequential
Page 8: CS171 Introduction to Computer Science II Hash Tablescheung/Courses/171/CS171-2012-Li/... · 2019. 10. 27. · CS171 Introduction to Computer Science II Hash Tables. Review •Sequential

Example

• We design a hash table for

storing entries as (SSN, Name),

where SSN (social security

number) is a nine-digit

positive integer

0

1

2

3

4 451-229-0004

981-101-0002

025-612-0001

8

positive integer

• Our hash table uses an array

of size N = 10,000 and the

hash function

h(x) = last four digits of x

9997

9998

9999

200-751-9998

Page 9: CS171 Introduction to Computer Science II Hash Tablescheung/Courses/171/CS171-2012-Li/... · 2019. 10. 27. · CS171 Introduction to Computer Science II Hash Tables. Review •Sequential

Hash Functions

• A hash function is usually

specified as the

composition of two

functions:

• The hash code is

applied first, and the

compression function is

applied next on the

9

functions:

Hash code:

h1: keys → integers

Compression function:

h2: integers → [0, N − 1]

applied next on the

result, i.e.,

h(x) = h2(h1(x))

Page 10: CS171 Introduction to Computer Science II Hash Tablescheung/Courses/171/CS171-2012-Li/... · 2019. 10. 27. · CS171 Introduction to Computer Science II Hash Tables. Review •Sequential
Page 11: CS171 Introduction to Computer Science II Hash Tablescheung/Courses/171/CS171-2012-Li/... · 2019. 10. 27. · CS171 Introduction to Computer Science II Hash Tables. Review •Sequential
Page 12: CS171 Introduction to Computer Science II Hash Tablescheung/Courses/171/CS171-2012-Li/... · 2019. 10. 27. · CS171 Introduction to Computer Science II Hash Tables. Review •Sequential
Page 13: CS171 Introduction to Computer Science II Hash Tablescheung/Courses/171/CS171-2012-Li/... · 2019. 10. 27. · CS171 Introduction to Computer Science II Hash Tables. Review •Sequential
Page 14: CS171 Introduction to Computer Science II Hash Tablescheung/Courses/171/CS171-2012-Li/... · 2019. 10. 27. · CS171 Introduction to Computer Science II Hash Tables. Review •Sequential
Page 15: CS171 Introduction to Computer Science II Hash Tablescheung/Courses/171/CS171-2012-Li/... · 2019. 10. 27. · CS171 Introduction to Computer Science II Hash Tables. Review •Sequential
Page 16: CS171 Introduction to Computer Science II Hash Tablescheung/Courses/171/CS171-2012-Li/... · 2019. 10. 27. · CS171 Introduction to Computer Science II Hash Tables. Review •Sequential

Collision Handling

• Separate Chaining

• Linear Probing

Page 17: CS171 Introduction to Computer Science II Hash Tablescheung/Courses/171/CS171-2012-Li/... · 2019. 10. 27. · CS171 Introduction to Computer Science II Hash Tables. Review •Sequential
Page 18: CS171 Introduction to Computer Science II Hash Tablescheung/Courses/171/CS171-2012-Li/... · 2019. 10. 27. · CS171 Introduction to Computer Science II Hash Tables. Review •Sequential
Page 19: CS171 Introduction to Computer Science II Hash Tablescheung/Courses/171/CS171-2012-Li/... · 2019. 10. 27. · CS171 Introduction to Computer Science II Hash Tables. Review •Sequential
Page 20: CS171 Introduction to Computer Science II Hash Tablescheung/Courses/171/CS171-2012-Li/... · 2019. 10. 27. · CS171 Introduction to Computer Science II Hash Tables. Review •Sequential
Page 21: CS171 Introduction to Computer Science II Hash Tablescheung/Courses/171/CS171-2012-Li/... · 2019. 10. 27. · CS171 Introduction to Computer Science II Hash Tables. Review •Sequential
Page 22: CS171 Introduction to Computer Science II Hash Tablescheung/Courses/171/CS171-2012-Li/... · 2019. 10. 27. · CS171 Introduction to Computer Science II Hash Tables. Review •Sequential
Page 23: CS171 Introduction to Computer Science II Hash Tablescheung/Courses/171/CS171-2012-Li/... · 2019. 10. 27. · CS171 Introduction to Computer Science II Hash Tables. Review •Sequential
Page 24: CS171 Introduction to Computer Science II Hash Tablescheung/Courses/171/CS171-2012-Li/... · 2019. 10. 27. · CS171 Introduction to Computer Science II Hash Tables. Review •Sequential
Page 25: CS171 Introduction to Computer Science II Hash Tablescheung/Courses/171/CS171-2012-Li/... · 2019. 10. 27. · CS171 Introduction to Computer Science II Hash Tables. Review •Sequential