280 likes | 380 Views
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
E N D
Dictionaries A 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 set lookUp(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) removed isEmptySet(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: Smart FirstName: Joe StudentNumber: 8 SSN: 123341112 Grade: 95
Hash Table 1123 2876 4468 9867 4312
Hash Table hash function 1123 2876 4468 9867 4312
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 1123 2876 4468 9867 4312
Collisions 3454 1123 2876 4468 9867 4312
Linear Probing Add: 5 7 14 22 24 39
Linear Probing Add: 7 14 22 24 39 5
Linear Probing Add: 14 22 24 39 5 7
Linear Probing Add: 22 24 39 5 7 14
Linear Probing Add: 24 39 22 5 7 14
Linear Probing Add: 24 39 5 22 7 14
Linear Probing Add: 39 5 22 7 24 14
Linear Probing Add: 5 22 7 24 39 14
Primary Clustering 5 22 7 24 39 14
Double Hashing H(key, 0) = h(key) H(key, p+1) = (H(key, p) + h2(key)) mod m
Double Hashing 39 h2(key) = addDigits(key) 5 7 22 24 14
Double Hashing Add: 5 7 14 22 24 39
Double Hashing Add: 7 14 22 24 39 5
Double Hashing Add: 14 22 24 39 5 7
Double Hashing Add: 22 24 39 5 7 14
Double Hashing Add: 24 39 22 5 7 14
Double Hashing Add: 24 39 5 7 22 14
Double Hashing Add: 39 5 7 22 24 14
Double Hashing 39 Add: 5 7 22 24 14