Hashing

Published

2023-08-05

Video lecture: Intro to hashing, including Horner hash function.

Before we implement a hash table in C++, here are two videos that introduce new techniques we’ll use when coding our hash table. These are passing functions, and the use of optionals. These will make our lives much easier when implementing our hash tables.

By passing a hash function to our hash table class, we gain flexibility—we can use the same class with different hash functions (you’ll use this in project #5).

Optionals allow us to create functions that can return a value or null (nullopt). This will be handy when working with our hash tables, since some elements in our hash tables may contain values, whereas other elements might be empty. C++ optionals provide us with a convenient way of handling values that may be empty or null.

Passing functions in C++

C++ optionals

Implementing a simple hash table in C++

(Feel free to try it on your own before watching the video.)

Extending our hash table class to work with custom objects using getKey()

Reading: Textbook chapter 8 (hash tables)

Resources and supplemental materials:

Comprehension check:

  • A hash table is a data structure intended to allow for access in O(__________) time.
  • Rather than searching for an item, we access an item in a hash table by ___________________ its index.
  • A hash function should return values that are valid ____________ for the given hash table.
  • True or false? A hash function must incorporate randomness in order to uniformly distribute values within its codomain.
  • Given a hash table of size 271, if we have a load factor of 59.0% then we have __________ entries in the hash table.
  • True or false? For any implementation of a hash table, insert, find, and remove methods will all have different complexities.

Answers: ǝslɐɟ / 09Ɩ / ǝslɐɟ / sǝɔᴉpuᴉ / ƃuᴉʇɐlnɔlɐɔ / Ɩ

Original author: Clayton Cafiero < [given name] DOT [surname] AT uvm DOT edu >

No generative AI was used in producing this material. This was written the old-fashioned way.

All materials copyright © 2020–2023, The University of Vermont. All rights reserved.