CS223: Data Structures - Hash Tables

PropitiousOrchid avatar
PropitiousOrchid
·
·
Download

Start Quiz

Study Flashcards

24 Questions

What is the primary advantage of using separate chaining in hash table implementation?

It is less sensitive to the hash function or load factors.

What is a disadvantage of separate chaining?

It can lead to wastage of space.

What is the main difference between open addressing and separate chaining?

Open addressing stores elements in the hash table itself.

What is the purpose of linear probing in open addressing?

To find an empty slot in the hash table.

What is the condition for using open addressing?

Size of the table must be greater than or equal to the total number of keys.

What is a disadvantage of linear probing?

It can lead to clustering.

What is the main advantage of using a simple hash function like 'key mod 7'?

It is simple to implement.

What is the purpose of a hash function in a hash table implementation?

To compute the index of the hash table.

What is the primary reason why we need hashing in many applications?

To handle a large amount of data efficiently

What is the purpose of a hash function in a hash table?

To map a key to a specific index in the hash table

What is a characteristic of a good hash function?

It avoids collisions

What is the load factor of a hash table?

The number of keys stored in the hash table divided by the capacity

What is the purpose of a hash table?

To make it easy to find specific data items

Which of the following applications is a common use of hash tables?

Web searches

What is a benefit of using hash tables?

They can perform lookups in near constant time

What is a property of a hash value?

It is a fixed-length string

What is the purpose of using a prime number as the table size in the Division Method?

To reduce the number of collisions

In the Fold-shifting method, what is the purpose of padding the last part with zero?

To ensure that the last part is not empty

What is the result of the hash function in the example with the key '12345678' and a table size of 100?

80

What is the main difference between the Fold-shifting and Fold-boundary methods?

The reversal of the boundary parts

What is the advantage of using a hash function with a simple and fast computation?

It increases the speed of the hash function

What is the purpose of using a hash function in a hash table implementation?

To map a key to a specific index in the table

What is the result of the hash function in the example with the key '12345678' and a table size of 1000?

359

What is the benefit of having keys distributed evenly among cells in a hash table?

It reduces the number of collisions

Study Notes

Why Hashing?

  • Many applications deal with large amounts of data and require efficient lookups, such as search engines and web pages.
  • Typical data structures like arrays and lists may not be sufficient for handling efficient lookups.
  • Hashing is used when lookups need to occur in near constant time, O(1).
  • It is used in various applications, including web searches, spell checkers, databases, compilers, and password storage.

Hash Table

  • A hash table is a data structure used to store key-value pairs to make lookups easy and efficient.
  • It uses a hash function to compute an index into an array of slots from which the desired value can be found.
  • The load factor is the number of keys stored in the hash table divided by the capacity.

Hash Functions

  • A hash function maps an item to a slot in the hash table.
  • A good hash function should avoid collisions, distribute keys evenly, and be simple and fast to compute.
  • Different methods for choosing a good hashing function include the Division Method and Folding Methods.

Division Method

  • This method uses the modulo operator (%) to map an integer to a value between 0 and m-1, where m is the table size.
  • It is a popular method and the table size is often chosen as a prime number.

Folding Methods

  • Folding methods involve dividing the key into equal-digit parts and then adding the parts to compute the hash value.
  • There are two types of Folding Methods: Fold-Shifting and Fold-Boundary.
  • Fold-Shifting adds the parts, while Fold-Boundary adds the parts after reversing the boundary parts.

Handling Collisions

  • Collisions occur when two keys have the same hash value.
  • There are two methods to handle collisions: Separate Chaining and Open Addressing.

Separate Chaining

  • Each cell of the hash table points to a linked list of records that have the same hash function value.
  • Advantages: simple to implement, hash table never fills up, and less sensitive to the hash function or load factors.
  • Disadvantages: performance of chaining is not good, wastage of space, and uses extra space for links.

Open Addressing

  • All elements are stored in the hash table itself, and the table size must be greater than or equal to the total number of keys.
  • Open Addressing methods include Linear Probing, Quadratic Probing, and Double Probing.

Linear Probing

  • In linear probing, we linearly probe for the next slot.
  • If a slot is full, we try the next slot by adding 1 to the hash value.

Learn about hash tables, their importance, and applications in data structures, including search engines, spell checkers, and databases.

Make Your Own Quizzes and Flashcards

Convert your notes into interactive study material.

Get started for free

More Quizzes Like This

Hash Table with Linear Probing
3 questions

Hash Table with Linear Probing

MotivatedHippopotamus avatar
MotivatedHippopotamus
HashSet Implementation in Java
16 questions

HashSet Implementation in Java

SelfSatisfactionHafnium avatar
SelfSatisfactionHafnium
Hash Table Operations
10 questions
Use Quizgecko on...
Browser
Browser