+ All Categories
Home > Software > Hash map

Hash map

Date post: 15-Aug-2015
Category:
Upload: emmanuel-fuchs
View: 91 times
Download: 0 times
Share this document with a friend
Popular Tags:
101
HASH MAP
Transcript

HASH MAP

HashTable

KEY VALUE

Key Value Pair

Key VALUE

pierre 01.42.78.96.12

paul 03.67.90.67.00

françois 01.45.87.90.45

mohamed 04.88.8945.29

khaled 06.98.56.22.48

laila 01.23.45.67.89

alex34.56.15.27.78

.

claire 34.56.73.45.67

philippe02.34.26.48.26

.

Hash()françois 01.45.87.90.45

Phone Numbers list example

Key VALUE

pierre 01.42.78.96.12

paul 03.67.90.67.00

françois 01.45.87.90.45

mohamed 04.88.8945.29

khaled 06.98.56.22.48

laila 01.23.45.67.89

alex34.56.15.27.78

.

claire 34.56.73.45.67

philippe02.34.26.48.26

.

Hash()françois 01.45.87.90.45

Phone Numbers list example

Key Value

Employees FileSSN : Social Security Number

125675798988090

000000000000000

999999999999999

Social Security Number = address

Employees FileSSN : Social Security Number

125675798988090

000000000000000

999999999999999

Social Security Number = address

steve

mike

john

bob

max

edward

John SSN : 125675798988090

Employees FileSSN : Social Security Number

125675798988090

000000000000000

999999999999999

Social Security Number = address

steve

mike

john

bob

max

edward

Requires Memory size : 999999999999999

= 1E+15 = 1 petaoctet (Po)

John SSN : 125675798988090

Employees FileSSN : Social Security Number

125675798988090

000000000000000

999999999999999

N element Array

SSN

00

99

steve

mike

john

bob

max

edward

125675798988090

100 employees

Employees FileSSN : Social Security Number

125675798988090

000000000000000

999999999999999

N element Array

SSN

00

99

steve

mike

john

bob

max

edward

steve

bobjohn

mikeedward125675798988090

Employees FileSSN : Social Security Number

125675798988090

000000000000000

999999999999999

N element Array

SSN

00

99

steve

mike

john

bob

max

edward

steve

bobjohn

mikeedward125675798988090

Employees FileSSN : Social Security Number

125675798988090

000000000000000

999999999999999

steve

N element Array

00

99

KEY

hashbobjohn

mikeedward

value

addresses

steve

mike

john

bob

max

edward

Hashing

125675798988090000000000000000 999999999999999

00 99

Hashing

125675798988090000000000000000 999999999999999

00 99

Hashing

125675798988090000000000000000 999999999999999

00 99

Hashing

Employees FileSSN : Social Security Number

125675798988090

000000000000000

999999999999999

john

N element Array

00

99

KEY

hashbob

mikeedward

value

addresses

steve

mike

john

bob

max

edward

Keyspace

Addressspace

Key space to Address space mapping

125675798988090

000000000000000

999999999999999

Addressspace

Keyspace

00

99

Key space to Address space mapping

125675798988090

000000000000000

999999999999999

Addressspace

Keyspace

Index 00

99

Buckets principle

Hash Function The hash function is used to transform the key into

the index (the hash) of an array element (the slot or bucket) where the corresponding value is to be stored and sought.

KEY HASH

index

Value

Bucket (slot)

Hash table

hash table is an array-based data structure.

129007

926647

975378

269908

Fred

Steve

Richard

Robert

Key Value

0

1

3

4

5

2index

SSN

Hash table

hash function is used to convert the key into the index of an array element, where associated value is to be seek.

129007

926647

975378

269908

Fred

Steve

Richard

Robert

Key Value

h(129007)

h(129007)

h(975378)

h(269908)

0

1

3

4

5

2

index

SSN

buckets

Collision

If the hash function returns a slot that is already occupied there is a collision

129007

975378

269908

Fred

Richard

Robert

Key Value

H(129007)

H(975378)

H(269908)

H(926647)0

1

3

4

2

0

1

3

4

5

2

hash clustering

When the distribution of keys into buckets is not random, we say that the hash table exhibits clustering.

129007

975378

269908

Fred

Richard

Robert

Key Value

H(697803)

H(975378)

H(269908)

H(926647)

H(168477)

H(129007)

6

7

Hash function

Good Hash function provides uniform distribution of hash values.

Poor hash function will cause collisions and hash cluster.

Hash DistributionValue # Hash

0 2

1 2

2 2

3 2

4 0

5 0

6 0

7 0

8 0

9 010 1

1 2 3 4 5 6 7 8 9 10 110

1

2

3

# Hash

Hash Distribution

1 2 3 4 5 6 7 8 9 10 110

1

2

# Hash

# Hash

Value # Hash0 11 12 13 14 15 16 17 18 19 110 1

Hash Distribution

Value # Hash0 01 02 03 14 35 56 37 18 09 010 0

1 2 3 4 5 6 7 8 9 10 110

1

2

3

4

5

6

# Hash

# Hash

Hash function toy implementation : Modulo N R CAR(R) mod (11)

A 65 10B 66 0C 67 1D 68 2E 69 3F 70 4G 71 5H 72 6I 73 7J 74 8K 75 9L 76 10M 77 0N 78 1O 79 2P 80 3Q 81 4R 82 5S 83 6T 84 7U 85 8V 86 9W 87 10X 88 0Y 89 1Z 90 2

26 => 11

Modulo N Hash Distribution

Load Factor

  Key Value0 B 01 C 42 O 13 E 24 P 35 X 66 N 77 D 88 M 99    10 L 5

  Key Value0 B 01 C 42 O 13 E 24 P 35 X 66 N 77 D 88 M 99    

10 L S11    12    13    14    15    16    17    18    19    20    21    

# elements in the hash Table size of the hash table

48 %

91 %

Collision handling strategies

Closed addressing (open hashing). Open addressing (closed hashing).

0

1

3

4

5

2

129007

975378

269908

Fred

Richard

Robert

Key Value

H(697803)

H(975378)

H(269908)

H(926647)

H(168477)

H(129007)

6

7

Open addressing (closed hashing).

When there is a collision, "Probe" the array to find an empty slot after the occupied slot.

129007

926647

975378

269908

Fred

Steve

Richard

Robert

Key Value

H(697803)

H(975378)

H(269908)

H(926647)

168477 Phil

697803 GregH(168477)

H(129007)

Closed addressing (open hashing).

Each slot of the hash table contains a link to another data structure.

129007

926647

975378

269908

Fred

Steve

168477 Phil

697803 Greg

Key

129007

975378 Richard

269908 Robert

Linear Hashing

• Linear Hashing– Re-hash : hi (x) = (h(x) + i) mod B

• Stepsize : i• i = 1,2,3, …

• Quadratic Hashing– Re-hash : hi (x) = (h (x) + i ²) mod B

• Stepsize : i2

• i2 = 1,4,9,…

• Double Hashing– Re-hash : hi (x) = (h(x) + i g (x)) mod B

• Stepsize : g(x)

Toy implementation example : R

Attribut ARelation R

RID R

0 B

1 O

2 E

3 P

4 V

5 L

6 X

7 N

8 K

9 M

modulo

Char Ascii Code

Value Key

RID R CAR(R) mod (11)

0 B 66 0

1 O 79 2

2 E 69 3

3 P 80 3

4 C 67 1

5 L 76 10

6 X 88 0

7 N 78 1

8 D 68 2

9 M 77 0

Class HashLinearProbing

Hash(key) returns hash

Put (key, value) inserts key value pair

Get (key) gets key value

Remove (key) removes key preserving bucket structure.

Class HashMap JSE 1.4

Object get(Object key)Returns the value to which the specified key is

mapped in this identity hash map, or null if the map contains no mapping for this key.

Object put(Object key, Object value)Associates the specified value with the specified key

in this map.Object remove(Object key)

Removes the mapping for this key from this map if present

Linear Probing toy implementation

RID R

0 B

1 O

2 E

3 P

4 V

5 L

6 X

7 N

8 K

9 M

KEY VAL

0

1

2

3

4

5

6

7

8

9

10

MN

Relation Data Structure Implementation

Logical

Physical

Linear Probing toy implementation

RID R

0 B

1 O

2 E

3 P

4 V

5 L

6 X

7 N

8 K

9 M

KEY VAL

0

1

2

3

4

5

6

7

8

9

10

MN

M > N

Empty Slot is Search Stop Condition

Linear Probing toy implementation

RID R

0 B

1 O

2 E

3 P

4 V

5 L

6 X

7 N

8 K

9 M

KEY VAL

0

1

2

3

4

5

6

7

8

9

10

1110

M > N

Empty Slot is Search Stop Condition

Linear Probing

KEY VAL

B 00

1

2

3

4

5

6

7

8

9

10

X 6

O 1

E 2

P 3

N 7

V 4

L 5

ValueKey

RID R

0 B

1 O

2 E

3 P

4 V

5 L

6 X

7 N

8 K

9 M

0

2

3

3

9

10

0

1

9

0

Hash(key)

FirstEmptySlot ?

Put(N,7)

%M

Empty Slot is Search Place Stop Condition

Linear Probing

Hash(key)KEY VAL

B 00

1

2

3

4

5

6

7

8

9

10

X 6

O 1

E 2

P 3

N 7

K 8

M 9

V 4

L 5

ValueKey

RID R

0 B

1 O

2 E

3 P

4 V

5 L

6 X

7 N

8 K

9 M

0

2

3

3

9

10

0

1

9

0

Car Y = 89Y mod 11 = 1

Hash(key)

Get(Y)

Y ?

Empty

Stop

Return (-1) Empty Slot is Search Stop Condition

Linear Probing toy implementation

RID R

0 B

1 O

2 E

3 P

4 V

5 L

6 X

7 N

8 K

9 M

KEY VAL

0

1

2

3

4

5

6

7

8

9

10

1110

M > N

Empty Slot is Search Stop Condition

At least one empty slot

M – N = 1

Linear Probing

RID R

0 B

1 O

2 E

3 P

4 V

5 L

6 X

7 N

8 K

9 M

KEY VAL

0

1

2

3

4

5

6

7

8

9

10

MN

Linear Probing

KEY VAL

B 00

1

2

3

4

5

6

7

8

9

10

ValueKey

RID R

0 B

1 O

2 E

3 P

4 V

5 L

6 X

7 N

8 K

9 M

RID R

0 B

1 O

2 E

3 P

4 V

5 L

6 X

7 N

8 K

9 M

Example RelationImplementation

Linear Probing

KEY VAL

B 00

1

2

3

4

5

6

7

8

9

10

Hash(key) = CAR(R) Mod (M)

M = 11

ValueKey

RID R

0 B

1 O

2 E

3 P

4 V

5 L

6 X

7 N

8 K

9 M

0

2

3

3

9

10

0

1

9

0

Linear Probing

KEY VAL

B 00

1

2

3

4

5

6

7

8

9

10

Hash(key)

Put(B,0)

ValueKey

RID R

0 B

1 O

2 E

3 P

4 V

5 L

6 X

7 N

8 K

9 M

0

2

3

3

9

10

0

1

9

0

Linear Probing

KEY VAL

B 00

1

2

3

4

5

6

7

8

9

10

O 1

Hash(key)

Put(O,1)

ValueKey

RID R

0 B

1 O

2 E

3 P

4 V

5 L

6 X

7 N

8 K

9 M

0

2

3

3

9

10

0

1

9

0

Linear Probing

KEY VAL

B 00

1

2

3

4

5

6

7

8

9

10

O 1

E 2

Hash(key)

Put(E,3)

ValueKey

RID R

0 B

1 O

2 E

3 P

4 V

5 L

6 X

7 N

8 K

9 M

0

2

3

3

9

10

0

1

9

0

Linear Probing

KEY VAL

B 00

1

2

3

4

5

6

7

8

9

10

O 1

E 2

P 3

ValueKey

RID R

0 B

1 O

2 E

3 P

4 V

5 L

6 X

7 N

8 K

9 M

0

2

3

3

9

10

0

1

9

0

Hash(key)

FirstEmptySlot ?

Put(P,3)

Linear Probing

KEY VAL

B 00

1

2

3

4

5

6

7

8

9

10

O 1

E 2

P 3

V 4

ValueKey

RID R

0 B

1 O

2 E

3 P

4 V

5 L

6 X

7 N

8 K

9 M

0

2

3

3

9

10

0

1

9

0

Hash(key)

Put(V,9)

Linear Probing

KEY VAL

B 00

1

2

3

4

5

6

7

8

9

10

O 1

E 2

P 3

V 4

L 5

ValueKey

RID R

0 B

1 O

2 E

3 P

4 V

5 L

6 X

7 N

8 K

9 M

0

2

3

3

9

10

0

1

9

0

Hash(key)

Put(L,5)

Linear Probing

KEY VAL

B 00

1

2

3

4

5

6

7

8

9

10

X 6

O 1

E 2

P 3

V 4

L 5

ValueKey

RID R

0 B

1 O

2 E

3 P

4 V

5 L

6 X

7 N

8 K

9 M

0

2

3

3

9

10

0

1

9

0

Hash(key)

FirstEmptySlot ?

Put(X,6)

%M

Linear Probing

KEY VAL

B 00

1

2

3

4

5

6

7

8

9

10

X 6

O 1

E 2

P 3

N 7

V 4

L 5

ValueKey

RID R

0 B

1 O

2 E

3 P

4 V

5 L

6 X

7 N

8 K

9 M

0

2

3

3

9

10

0

1

9

0

Hash(key)

FirstEmptySlot ?

Put(N,7)

%M

Linear Probing

ValueKey

RID R

0 B

1 O

2 E

3 P

4 V

5 L

6 X

7 N

8 K

9 M

0

2

3

3

9

10

0

1

9

0

Hash(key)

FirstEmptySlot ?

Put(D,8)

%M

KEY VAL

B 00

1

2

3

4

5

6

7

8

9

10

X 6

O 1

E 2

P 3

N 7

K 9

V 4

L 5

Linear Probing

Hash(key)

Put(M,9)

KEY VAL

B 00

1

2

3

4

5

6

7

8

9

10

X 6

O 1

E 2

P 3

N 7

K 9

M 8

V 4

L 5

ValueKey

RID R

0 B

1 O

2 E

3 P

4 V

5 L

6 X

7 N

8 K

9 M

FirstEmptySlot ?

0

2

3

3

9

10

0

1

9

0

Linear Probing

Hash(key)

Put(M,9)

KEY VAL

B 00

1

2

3

4

5

6

7

8

9

10

X 6

O 1

E 2

P 3

N 7

K 9

M 0

V 4

L 5

ValueKey

RID R

0 B

1 O

2 E

3 P

4 V

5 L

6 X

7 N

8 K

9 M

0

2

3

3

9

10

0

1

9

0

Implementation of function Hash(Key)

In Java : Key % M

Specific case of Java char : in Java char are integer (Byte).

char: The char data type is a single 16-bit Unicode character. It has a minimum value of '\u0000' (or 0) and a maximum value of '\uffff' (or 65,535 inclusive).

Null char is 0 (zero).

Default value for char is 0, or u\0000.

Hash (Key) { Return Key Modulo M }

http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

Bucket Table Implementation

KEY VALchar keys Array [M] int values Array [M]

Put(key, value) simplified algo

M = # bucket entries

index = hash (key)

While Key [index) != empty

index = (index + 1) % M

End while Key [index] = key

Values [index] = value

Get(Key)

Get existing key Get non inserted key

Linear Probing

Hash(key)KEY VAL

B 00

1

2

3

4

5

6

7

8

9

10

X 6

O 1

E 2

P 3

N 7

K 8

M 9

V 4

L 5

ValueKey

RID R

0 B

1 O

2 E

3 P

4 V

5 L

6 X

7 N

8 K

9 M

0

2

3

3

9

10

0

1

9

0

Hash(key)Hash(key)

Get(B)

B,0

B ?

Linear Probing

Hash(key)KEY VAL

B 00

1

2

3

4

5

6

7

8

9

10

X 6

O 1

E 2

P 3

N 7

K 8

M 9

V 4

L 5

ValueKey

RID R

0 B

1 O

2 E

3 P

4 V

5 L

6 X

7 N

8 K

9 M

0

2

3

3

9

10

0

1

9

0

Hash(key)

K?

K,9

Get(K)

Linear Probing

Hash(key)KEY VAL

B 00

1

2

3

4

5

6

7

8

9

10

X 6

O 1

E 2

P 3

N 7

K 8

M 9

V 4

L 5

ValueKey

RID R

0 B

1 O

2 E

3 P

4 V

5 L

6 X

7 N

8 K

9 M

0

2

3

3

9

10

0

1

9

0

Hash(key)

Get(M)

M ?

M, 9

Linear Probing

Hash(key)KEY VAL

B 00

1

2

3

4

5

6

7

8

9

10

X 6

O 1

E 2

P 3

N 7

K 8

M 9

V 4

L 5

ValueKey

RID R

0 B

1 O

2 E

3 P

4 V

5 L

6 X

7 N

8 K

9 M

0

2

3

3

9

10

0

1

9

0

Car Y = 89Y mod 11 = 1

Hash(key)

Get(Y)

Y ?

Empty

Stop

Return (-1)

Get(key)

M = # buckets

index = hash (key)

valueToReturn = -1 // value to return if the key is not in the map

While key [index] != key and key [index] != empty

index = (index + 1) % M

End while If (key [index] = key) valueToReturn = Values [index]

Return valueToReturn

Remove (Key)

Remove (M) Remove (N) : rehash end of the cluster. Remove (L) : rehash end of the cluster.

Linear Probing

Hash(key)KEY VAL

B 00

1

2

3

4

5

6

7

8

9

10

X 6

O 1

E 2

P 3

N 7

K 8

M 9

V 4

L 5

ValueKey

RID R

0 B

1 O

2 E

3 P

4 V

5 L

6 X

7 N

8 K

9 M

0

2

3

3

9

10

0

1

9

0

Hash(key)

Remove (M)

K(0) != M

Blank

Scan for M

Linear Probing

Hash(key)KEY VAL

B 00

1

2

3

4

5

6

7

8

9

10

X 6

O 1

E 2

P 3

N 7

K 8

V 4

L 5

ValueKey

RID R

0 B

1 O

2 E

3 P

4 V

5 L

6 X

7 N

8 K

9 M

0

2

3

3

9

10

0

1

9

0

Hash(key)

Remove (M)

K(0) != M

Blank

Scan for M

Linear Probing

Hash(key)KEY VAL

B 00

1

2

3

4

5

6

7

8

9

10

X 6

O 1

E 2

P 3

N 7

K 8

M 9

V 4

L 5

ValueKey

RID R

0 B

1 O

2 E

3 P

4 V

5 L

6 X

7 N

8 K

9 M

0

2

3

3

9

10

0

1

9

0

Hash(key)

Remove (N)

K(1) != N

Blank and rehashEnd of cluster

Scan for N

Linear Probing

Hash(key)KEY VAL

B 00

1

2

3

4

5

6

7

8

9

10

X 6

O 1

E 2

P 3

N 7

K 8

M 9

V 4

L 5

ValueKey

RID R

0 B

1 O

2 E

3 P

4 V

5 L

6 X

7 N

8 K

9 M

0

2

3

3

9

10

0

1

9

0

Cluster

Remove (N)

K(1) != N

Blank and rehashEnd of cluster

Scan for N

Linear Probing

Hash(key)KEY VAL

B 00

1

2

3

4

5

6

7

8

9

10

X 6

O 1

E 2

P 3

K 8

M 9

V 4

L 5

ValueKey

RID R

0 B

1 O

2 E

3 P

4 V

5 L

6 X

7 N

8 K

9 M

0

2

3

3

9

10

0

1

9

0

Remove (N)

K(1) != N

Blank and rehashEnd of cluster

Scan for N

EOf cluster

Linear Probing

Hash(key)KEY VAL

B 00

1

2

3

4

5

6

7

8

9

10

X 6

O 1

E 2

P 3

K 8

M 9

V 4

L 5

ValueKey

RID R

0 B

1 O

2 E

3 P

4 V

5 L

6 X

7 N

8 K

9 M

0

2

3

3

9

10

0

1

9

0

Remove (N)

K(1) != N

Blank and rehashEnd of cluster

Scan for N

EOf cluster

Blank Key(6) , Val(6) put(K,8)

Linear Probing

Hash(key)KEY VAL

B 00

1

2

3

4

5

6

7

8

9

10

X 6

O 1

E 2

P 3

M 9

V 4

L 5

ValueKey

RID R

0 B

1 O

2 E

3 P

4 V

5 L

6 X

7 N

8 K

9 M

0

2

3

3

9

10

0

1

9

0

Remove (N)

K(1) != N

Blank and rehashEnd of cluster

Scan for N

EOf cluster

Blank Key(6) , Val(6) put(K,8)

Linear Probing

Hash(key)KEY VAL

B 00

1

2

3

4

5

6

7

8

9

10

X 6

O 1

E 2

P 3

M 9

V 4

L 5

ValueKey

RID R

0 B

1 O

2 E

3 P

4 V

5 L

6 X

7 N

8 K

9 M

0

2

3

3

9

10

0

1

9

0

Remove (N)

K(1) != N

Blank and rehashEnd of cluster

Scan for N

Blank Key(6) , Val(6) put(K,8)

Linear Probing

Hash(key)KEY VAL

B 00

1

2

3

4

5

6

7

8

9

10

X 6

O 1

E 2

P 3

M 9

V 4

L 5

ValueKey

RID R

0 B

1 O

2 E

3 P

4 V

5 L

6 X

7 N

8 K

9 M

0

2

3

3

9

10

0

1

9

0

Remove (N)

K(1) != N

Blank and rehashEnd of cluster

Scan for N

Blank Key(6), Val(6) put(K,8)

Linear Probing

Hash(key)KEY VAL

B 00

1

2

3

4

5

6

7

8

9

10

X 6

O 1

E 2

P 3

K 8

M 9

V 4

L 5

ValueKey

RID R

0 B

1 O

2 E

3 P

4 V

5 L

6 X

7 N

8 K

9 M

0

2

3

3

9

10

0

1

9

0

Remove (N)

K(1) != N

Blank and rehashEnd of cluster

Scan for N

Blank Key(7) ,Val(7) put(K,8)

Linear Probing

Hash(key)KEY VAL

B 00

1

2

3

4

5

6

7

8

9

10

X 6

O 1

E 2

P 3

K 8

M 9

V 4

L 5

ValueKey

RID R

0 B

1 O

2 E

3 P

4 V

5 L

6 X

7 N

8 K

9 M

0

2

3

3

9

10

0

1

9

0

Remove (N)

K(1) != N

Blank and rehashEnd of cluster

Scan for N

Blank Key(7) ,Val(7) put(K,8)

Linear Probing

Hash(key)KEY VAL

B 00

1

2

3

4

5

6

7

8

9

10

X 6

O 1

E 2

P 3

K 8

V 4

L 5

ValueKey

RID R

0 B

1 O

2 E

3 P

4 V

5 L

6 X

7 N

8 K

9 M

0

2

3

3

9

10

0

1

9

0

Remove (N)

K(1) != N

Blank and rehashEnd of cluster

Scan for N

Blank Key(7) , Val(7) put(M,9)

Linear Probing

Hash(key)KEY VAL

B 00

1

2

3

4

5

6

7

8

9

10

X 6

O 1

E 2

P 3

K 8

V 4

L 5

ValueKey

RID R

0 B

1 O

2 E

3 P

4 V

5 L

6 X

7 N

8 K

9 M

0

2

3

3

9

10

0

1

9

0

Remove (N)

K(1) != N

Blank and rehashEnd of cluster

Scan for N

Blank Key(7) , Val(7) put(M,9)

FirstEmptySlot ?

Linear Probing

Hash(key)KEY VAL

B 00

1

2

3

4

5

6

7

8

9

10

X 6

O 1

E 2

P 3

K 8

9

V 4

L 5

ValueKey

RID R

0 B

1 O

2 E

3 P

4 V

5 L

6 X

7 N

8 K

9 M

0

2

3

3

9

10

0

1

9

0

Remove (N)

K(1) != N

Blank and rehashEnd of cluster

Scan for N

Blank Key(7) , Val(7) put(M,9)

FirstEmptySlot ?

M

Linear Probing

Hash(key)KEY VAL

B 00

1

2

3

4

5

6

7

8

9

10

X 6

O 1

E 2

P 3

N 7

K 8

M 9

V 4

L 5

ValueKey

RID R

0 B

1 O

2 E

3 P

4 V

5 L

6 X

7 N

8 K

9 M

0

2

3

3

9

10

0

1

9

0

Remove (L)

Linear Probing

Hash(key)KEY VAL

B 00

1

2

3

4

5

6

7

8

9

10

X 6

O 1

E 2

P 3

N 7

K 8

M 9

V 4

L 5

ValueKey

RID R

0 B

1 O

2 E

3 P

4 V

5 L

6 X

7 N

8 K

9 M

0

2

3

3

9

10

0

1

9

0

K(10) = L blank

Remove (L)

Blank and rehashEnd of cluster

Linear Probing

Hash(key)KEY VAL

B 00

1

2

3

4

5

6

7

8

9

10

X 6

O 1

E 2

P 3

N 7

K 8

M 9

V 4

ValueKey

RID R

0 B

1 O

2 E

3 P

4 V

5 L

6 X

7 N

8 K

9 M

0

2

3

3

9

10

0

1

9

0

K(10) = L blank

Remove (L)

Blank and rehashEnd of cluster

Linear Probing

Hash(key)KEY VAL

B 00

1

2

3

4

5

6

7

8

9

10

X 6

O 1

E 2

P 3

N 7

K 8

M 9

V 4

ValueKey

RID R

0 B

1 O

2 E

3 P

4 V

5 L

6 X

7 N

8 K

9 M

0

2

3

3

9

10

0

1

9

0

K(10) = L blank

Remove (L)

Blank and rehashEnd of cluster

EOf cluster

Linear Probing

Hash(key)KEY VAL

B 00

1

2

3

4

5

6

7

8

9

10

X 6

O 1

E 2

P 3

N 7

K 8

M 9

V 4

ValueKey

RID R

0 B

1 O

2 E

3 P

4 V

5 L

6 X

7 N

8 K

9 M

0

2

3

3

9

10

0

1

9

0

Remove (L)

Blank and rehashEnd of cluster

EOf cluster

Blank Key(0) , Val(0) put(B,0)

Linear Probing

Hash(key)KEY VAL

B 00

1

2

3

4

5

6

7

8

9

10

X 6

O 1

E 2

P 3

N 7

K 8

M 9

V 4

ValueKey

RID R

0 B

1 O

2 E

3 P

4 V

5 L

6 X

7 N

8 K

9 M

0

2

3

3

9

10

0

1

9

0

Remove (L)

Blank and rehashEnd of cluster

EOf cluster

Blank Key(1) , Val(1) put(X,6)

Linear Probing

Hash(key)KEY VAL

B 00

1

2

3

4

5

6

7

8

9

10

X 6

O 1

E 2

P 3

N 7

K 8

M 9

V 4

ValueKey

RID R

0 B

1 O

2 E

3 P

4 V

5 L

6 X

7 N

8 K

9 M

0

2

3

3

9

10

0

1

9

0

Remove (L)

Blank and rehashEnd of cluster

EOf cluster

Blank Key(2) , Val(2) put(O,1)

Linear Probing

Hash(key)KEY VAL

B 00

1

2

3

4

5

6

7

8

9

10

X 6

O 1

E 2

P 3

N 7

K 8

M 9

V 4

ValueKey

RID R

0 B

1 O

2 E

3 P

4 V

5 L

6 X

7 N

8 K

9 M

0

2

3

3

9

10

0

1

9

0

Remove (L)

Blank and rehashEnd of cluster

EOf cluster

Blank Key(3) , Val(3) put(E,2)

Linear Probing

Hash(key)KEY VAL

B 00

1

2

3

4

5

6

7

8

9

10

X 6

O 1

E 2

P 3

N 7

K 8

M 9

V 4

ValueKey

RID R

0 B

1 O

2 E

3 P

4 V

5 L

6 X

7 N

8 K

9 M

0

2

3

3

9

10

0

1

9

0

Remove (L)

Blank and rehashEnd of cluster

EOf cluster

Blank Key(4), Val(4) put(P,3)

Linear Probing

Hash(key)KEY VAL

B 00

1

2

3

4

5

6

7

8

9

10

X 6

O 1

E 2

P 3

N 7

K 8

M 9

V 4

ValueKey

RID R

0 B

1 O

2 E

3 P

4 V

5 L

6 X

7 N

8 K

9 M

0

2

3

3

9

10

0

1

9

0

Remove (L)

Blank and rehashEnd of cluster

EOf cluster

Blank Key(5), Val(5) put(N,7)

Linear Probing

Hash(key)KEY VAL

B 00

1

2

3

4

5

6

7

8

9

10

X 6

O 1

E 2

P 3

N 7

K 8

M 9

V 4

ValueKey

RID R

0 B

1 O

2 E

3 P

4 V

5 L

6 X

7 N

8 K

9 M

0

2

3

3

9

10

0

1

9

0

Remove (L)

Blank and rehashEnd of cluster

EOf cluster

Blank Key(6) , Val(6) put(K,8)

Linear Probing

Hash(key)KEY VAL

B 00

1

2

3

4

5

6

7

8

9

10

X 6

O 1

E 2

P 3

N 7

M 9

V 4

ValueKey

RID R

0 B

1 O

2 E

3 P

4 V

5 L

6 X

7 N

8 K

9 M

0

2

3

3

9

10

0

1

9

0

Remove (L)

Blank and rehashEnd of cluster

EOf cluster

Blank Key(6) , Val(6) put(K,8)

Linear Probing

Hash(key)KEY VAL

B 00

1

2

3

4

5

6

7

8

9

10

X 6

O 1

E 2

P 3

N 7

M 9

V 4

K 8

ValueKey

RID R

0 B

1 O

2 E

3 P

4 V

5 L

6 X

7 N

8 K

9 M

0

2

3

3

9

10

0

1

9

0

Remove (L)

Blank and rehashEnd of cluster

EOf cluster

Blank Key(6) , Val(6) put(K,8)

Linear Probing

Hash(key)KEY VAL

B 00

1

2

3

4

5

6

7

8

9

10

X 6

O 1

E 2

P 3

N 7

M 9

V 4

K 8

ValueKey

RID R

0 B

1 O

2 E

3 P

4 V

5 L

6 X

7 N

8 K

9 M

0

2

3

3

9

10

0

1

9

0

Remove (L)

Blank and rehashEnd of cluster

EOf cluster

Blank Key(7) , Val(7) put(M,9)

Linear Probing

Hash(key)KEY VAL

B 00

1

2

3

4

5

6

7

8

9

10

X 6

O 1

E 2

P 3

N 7

M 9

V 4

K 8

ValueKey

RID R

0 B

1 O

2 E

3 P

4 V

5 L

6 X

7 N

8 K

9 M

0

2

3

3

9

10

0

1

9

0

Remove (L)

Blank and rehashEnd of cluster

EOf cluster

Blank Key(7) , Val(7) put(M,9)

Linear Probing

Hash(key)KEY VAL

B 00

1

2

3

4

5

6

7

8

9

10

X 6

O 1

E 2

P 3

N 7

M 9

V 4

K 8

ValueKey

RID R

0 B

1 O

2 E

3 P

4 V

5 L

6 X

7 N

8 K

9 M

0

2

3

3

9

10

0

1

9

0

Remove (L)

Blank and rehashEnd of cluster

EOf cluster

Blank Key(7) , Val(7) put(M,9)

Remove (key) simplified algo

M = # buckets

index = hash (key)While key key [index) != key and key [index) != empty

index = (index + 1) % MEnd while Key [index] = 0, Value [index] = 0

// rehash

index = (index + 1) % MWhile key [index) != empty

savedKey = Key [index], savedValue = Value [index]

Key [index] = 0 Value [index] = 0

Put ( savedKey , savedValue )

index = (index + 1) % M

End while

The End


Recommended