Top Banner
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 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
28

Dictionaries

Mar 15, 2016

Download

Documents

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
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Dictionaries

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

Page 2: Dictionaries

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.

Page 3: Dictionaries

A Node

NameLast: SmartFirstName: Joe

StudentNumber: 8SSN: 123341112

Grade: 95

Page 4: Dictionaries

Hash Table

11232876446898674312

Page 5: Dictionaries

Hash Table

11232876446898674312

hash function

Page 6: Dictionaries

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.

Page 7: Dictionaries

h(key) = key mod 17

11232876446898674312

Page 8: Dictionaries

Collisions

11232876446898674312

3454

Page 9: Dictionaries

Separate Chaining

Page 10: Dictionaries

Linear Probing

Add:5714222439

Page 11: Dictionaries

Linear Probing

Add:714222439

5

Page 12: Dictionaries

Linear Probing

Add:14222439

5

7

Page 13: Dictionaries

Linear Probing

Add:222439 5

7

14

Page 14: Dictionaries

Linear Probing

Add:2439

5

7

14

22

Page 15: Dictionaries

Linear Probing

Add:2439

5

7

14

22

Page 16: Dictionaries

Linear Probing

Add:39

5

7

14

22

24

Page 17: Dictionaries

Linear Probing

Add:

5

7

14

22

2439

Page 18: Dictionaries

Primary Clustering

5

7

14

22

2439

Page 19: Dictionaries

Double Hashing

H(key, 0) = h(key)H(key, p+1) = (H(key, p) + h2(key)) mod m

Page 20: Dictionaries

Double Hashing

5

7

14

22

24

39

h2(key) = addDigits(key)

Page 21: Dictionaries

Double Hashing

Add:5714222439

Page 22: Dictionaries

Double Hashing

Add:714222439

5

Page 23: Dictionaries

Double Hashing

Add:14222439

5

7

Page 24: Dictionaries

Double Hashing

Add:222439 5

7

14

Page 25: Dictionaries

Double Hashing

Add:2439

5

7

14

22

Page 26: Dictionaries

Double Hashing

Add:2439

5

7

14

22

Page 27: Dictionaries

Double Hashing

Add:39

5

7

14

22

24

Page 28: Dictionaries

Double Hashing

Add:

5

7

14

22

24

39