More on Hash Tables Andy Wang Data Structures, Algorithms, and Generic Programming.

18
More on Hash Tables Andy Wang Data Structures, Algorithms, and Generic Programming

description

Steps to Build a Hash Table 1. Build a table to support basic operations Insert Lookup Remove 2. Use a hash function to determine the table entry 3. Add mechanisms to handle collisions

Transcript of More on Hash Tables Andy Wang Data Structures, Algorithms, and Generic Programming.

Page 1: More on Hash Tables Andy Wang Data Structures, Algorithms, and Generic Programming.

More on Hash Tables

Andy WangData Structures, Algorithms, and

Generic Programming

Page 2: More on Hash Tables Andy Wang Data Structures, Algorithms, and Generic Programming.

Why Hash Tables?

Arguments Linear search is simple Hash table does not save that much time

Counter arguments What if the data volume is high? (internet

routers) What if the data set if large? (yellow page)

Page 3: More on Hash Tables Andy Wang Data Structures, Algorithms, and Generic Programming.

Steps to Build a Hash Table

1. Build a table to support basic operations Insert Lookup Remove

2. Use a hash function to determine the table entry

3. Add mechanisms to handle collisions

Page 4: More on Hash Tables Andy Wang Data Structures, Algorithms, and Generic Programming.

Insert()

Name: Bobby HillAddress: Arlen

0123

Hash(name)

Hash Table

Page 5: More on Hash Tables Andy Wang Data Structures, Algorithms, and Generic Programming.

Insert()

Name: Bobby HillAddress: Arlen

01 Bobby Hill Arlen23

Hash(name)

Hash Table

Page 6: More on Hash Tables Andy Wang Data Structures, Algorithms, and Generic Programming.

Insert()

Name: Bart SimpsonAddress: Springfield

01 Bobby Hill Arlen23

Hash(name)

Hash Table

Icarumba!

Page 7: More on Hash Tables Andy Wang Data Structures, Algorithms, and Generic Programming.

Linear Probing

Name: Bart SimpsonAddress: Springfield

01 Bobby Hill Arlen23

Hash(name)

Hash Table

Page 8: More on Hash Tables Andy Wang Data Structures, Algorithms, and Generic Programming.

Linear Probing

Name: Bart SimpsonAddress: Springfield

01 Bobby Hill Arlen2 Bart Simpson Springfield3

Hash(name)

Hash Table

Page 9: More on Hash Tables Andy Wang Data Structures, Algorithms, and Generic Programming.

Remove()

Name: Bobby Hill

01 Bobby Hill Arlen2 Bart Simpson Springfield3

Hash Table

Page 10: More on Hash Tables Andy Wang Data Structures, Algorithms, and Generic Programming.

Remove()

01 Bobby Hill Arlen2 Bart Simpson Springfield3

Hash(name)

Hash Table

Name: Bobby Hill

Page 11: More on Hash Tables Andy Wang Data Structures, Algorithms, and Generic Programming.

Remove()

012 Bart Simpson Springfield3

Hash(name)

Hash Table

Name: Bobby Hill

Page 12: More on Hash Tables Andy Wang Data Structures, Algorithms, and Generic Programming.

Lookup()

Name: Bart Simpson

012 Bart Simpson Springfield3

Hash Table

Page 13: More on Hash Tables Andy Wang Data Structures, Algorithms, and Generic Programming.

Lookup()

012 Bart Simpson Springfield3

Hash(name)

Hash Table

Name: Bart Simpson

Page 14: More on Hash Tables Andy Wang Data Structures, Algorithms, and Generic Programming.

Insert()

Name: Bart SimpsonAddress: Springfield

012 Bart Simpson Springfield3

Hash(name)

Hash Table

Page 15: More on Hash Tables Andy Wang Data Structures, Algorithms, and Generic Programming.

Tricky Case…

Name: Bart SimpsonAddress: Springfield

01 Bart Simpson Springfield2 Bart Simpson Springfield3

Hash(name)

Hash Table

Oops…

Page 16: More on Hash Tables Andy Wang Data Structures, Algorithms, and Generic Programming.

To Handle Tricky Cases

Insert(…) {if (Lookup(…) == false) {// insert

}}Remove(…) {

if (entry[hash] empty || entry[hash] != key) // linear search for the entry

}}

Page 17: More on Hash Tables Andy Wang Data Structures, Algorithms, and Generic Programming.

To Handle Tricky Cases

Lookup(…) {if (entry[hash] empty || entry[hash] != key)

// linear search for the entry}

}

Page 18: More on Hash Tables Andy Wang Data Structures, Algorithms, and Generic Programming.

How can we make it more efficient?