Upload
hadassah-hancock
View
19
Download
0
Tags:
Embed Size (px)
DESCRIPTION
Dictionaries. A Dictionary is a set S made up of n elements: x 0 x 1 x 2 x 3 … x n-1 that has the following functions. Functions: createEmptySet() returns a newly created empty set lookUp(S, k) returns the node in S that has the key k - PowerPoint PPT Presentation
Citation preview
DictionariesA Dictionary is a set S made up of n elements: x0 x1 x2 x3 … xn-1 that has
the following functions.
Functions:createEmptySet() returns a newly created empty setlookUp(S, k) returns the node in S that has the key k insert(S, x) returns S with x added delete(S, k) returns S with x (found using x.k) removedisEmptySet(S) returns true if S is empty and false if it is
not
Homework 7
• Describe how to implement a dictionary that has an average lookUp, insert, and delete time that is constant, but uses an array of no more than 2*n elements.
• Do the five Dictionary functions.
A Node
NameLast: SmartFirstName: Joe
StudentNumber: 8SSN: 123341112
Grade: 95
Hash Table
11232876446898674312
Hash Table
11232876446898674312
hash function
Hash Function
h(key) position in the array
• Last 4 of SSN for an array of size 10,000.• The modulo function is good to use for two
reasons.– Lets you fit key to any size array.– If you use a prime number, it helps your
distribution.
h(key) = key mod 17
11232876446898674312
Collisions
11232876446898674312
3454
Separate Chaining
Linear Probing
Add:5714222439
Linear Probing
Add:714222439
5
Linear Probing
Add:14222439
5
7
Linear Probing
Add:222439 5
7
14
Linear Probing
Add:2439
5
7
14
22
Linear Probing
Add:2439
5
7
14
22
Linear Probing
Add:39
5
7
14
22
24
Linear Probing
Add:
5
7
14
22
2439
Primary Clustering
5
7
14
22
2439
Double Hashing
H(key, 0) = h(key)H(key, p+1) = (H(key, p) + h2(key)) mod m
Double Hashing
5
7
14
22
24
39
h2(key) = addDigits(key)
Double Hashing
Add:5714222439
Double Hashing
Add:714222439
5
Double Hashing
Add:14222439
5
7
Double Hashing
Add:222439 5
7
14
Double Hashing
Add:2439
5
7
14
22
Double Hashing
Add:2439
5
7
14
22
Double Hashing
Add:39
5
7
14
22
24
Double Hashing
Add:
5
7
14
22
24
39