Hash Tables: Storing and Searching

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson
Download our mobile app to listen on the go
Get App

Questions and Answers

What is a hash table primarily used for?

  • Performing complex mathematical computations.
  • Managing network connections.
  • Creating graphical user interfaces.
  • Storing and retrieving information efficiently. (correct)

In the context of hash tables, what is a 'key'?

  • A flag indicating whether a record is valid.
  • A numerical index representing the location of a record.
  • A method for reorganizing the table.
  • A special field in a record used for identifying and accessing the record. (correct)

What occurs when a hash function maps two different keys to the same index in a hash table?

  • Rehashing of the entire table.
  • Automatic table resizing.
  • Data corruption.
  • A collision. (correct)

Which of the following is a common method for resolving collisions in hash tables?

<p>Linear probing. (C)</p>
Signup and view all the answers

What is the purpose of converting a key to an array index when inserting a new record into a hash table?

<p>To determine the location where the record should be stored. (C)</p>
Signup and view all the answers

When deleting a record from a hash table, why is it important not to leave the location as an ordinary 'empty spot'?

<p>To avoid interfering with searches for other records. (B)</p>
Signup and view all the answers

Which of the following is a typical method for creating a hash value?

<p>Calculating (Number mod 701). (C)</p>
Signup and view all the answers

If a collision occurs during insertion in a hash table, and linear probing is used, what happens?

<p>The algorithm moves forward until an empty spot is found. (B)</p>
Signup and view all the answers

In the context of hash tables, what does the term 'collision' refer to?

<p>The scenario where two different keys produce the same hash value. (D)</p>
Signup and view all the answers

What is 'separate chaining' as a method of collision resolution?

<p>A technique of storing colliding elements in a linked list at each index. (D)</p>
Signup and view all the answers

What is the primary advantage of collision resolution with separate chaining?

<p>Better space utilization with large items. (C)</p>
Signup and view all the answers

When implementing open addressing, what happens when a collision occurs during insertion?

<p>Alternative cells are probed until an empty cell is found. (B)</p>
Signup and view all the answers

With open addressing, what is a key disadvantage?

<p>A bigger table is needed. (A)</p>
Signup and view all the answers

In a hash table using open addressing, what is the significance of marking a deleted location in a special way rather than leaving it empty?

<p>To ensure that searches for other keys are not prematurely terminated. (B)</p>
Signup and view all the answers

What operation is necessary during a record deletion to handle searches?

<p>Marking the location in some special way. (A)</p>
Signup and view all the answers

After calculating the hash value in an open addressing scheme, what is the next step in searching for a key?

<p>Check the location of the array for the key. (A)</p>
Signup and view all the answers

In a hash table with open addressing, what action should be taken if the initial location checked during a search does not contain the desired key?

<p>Keep moving forward until the key is found, or an empty spot is reached. (B)</p>
Signup and view all the answers

When searching for a key in a hash table, what is the primary goal?

<p>To find the data associated with the key quickly. (C)</p>
Signup and view all the answers

Which data structure is the simplest form of a hash table?

<p>An array of records. (D)</p>
Signup and view all the answers

In a hash table context, what does the 'hash value' of a key represent?

<p>The index in the array where the record should be stored. (C)</p>
Signup and view all the answers

What is the main disadvantage of separate chaining?

<p>It requires the use of linked lists. (D)</p>
Signup and view all the answers

Which characteristics are part of open addressing?

<p>There is no second data structure and the element goes inside the table. (C)</p>
Signup and view all the answers

What is contained in each record of the simplest hash table?

<p>One key. (B)</p>
Signup and view all the answers

Why are hash tables used?

<p>To store data in an array and search for the information. (D)</p>
Signup and view all the answers

What is an advantageous property of the separate chaining approach to collision resolution?

<p>Deletion is quick and easy using deletion from the linked list. (C)</p>
Signup and view all the answers

If you have keys 0, 1, 4, 9, 16, 25, 36, 49, 64, 81 and the hash function is hash(key) = key % 10, what keys will collide?

<p>1, 81 and 4, 64. (D)</p>
Signup and view all the answers

With the keys 0, 1, 4, 9, 16, 25, 36, 49, 64, 81 and the hash function hash(key) = key % 10, which slots are occupied after inserting all elements?

<p>0, 1, 4, 9, 6, 5, 6, 9, 4, 1 (B)</p>
Signup and view all the answers

How should the location be handled after an item is deleted?

<p>The location must be marked in some special way, so that the searches know that the sot used to be used (C)</p>
Signup and view all the answers

If a hash table has an array of 701 records, and a new record with the number 580625685 is inserted using the hash value (Number mod 701), what array index will the new record be placed into?

<p>3 (C)</p>
Signup and view all the answers

In an open addressing scheme, what happens if several keys hash to the same location?

<p>A collision occurs, and it is solved when you move forward until you find an empty spot. (D)</p>
Signup and view all the answers

How is data found with open addressing?

<p>The data that's attached to a key can be found fairly quickly. (D)</p>
Signup and view all the answers

In a collision resolution with separate chaining, where is a new item inserted?

<p>The front of the list. (B)</p>
Signup and view all the answers

Hash tables are a common approach to what operation?

<p>Storing/searching. (D)</p>
Signup and view all the answers

When is overflow advantageous?

<p>When the table needs to hold more elements than expected. (D)</p>
Signup and view all the answers

Flashcards

What are Hash Tables?

A data structure that stores key-value pairs, using a hash function to compute indices for keys.

What is a Key in a Hash Table?

A field in a record that is used to uniquely identify the record and locate it within the hash table.

What is a hash value?

The value calculated from a key by a hash function, used as an index in a hash table.

What is key conversion to an array index?

The process of transforming a key into an array index for storage and retrieval in a hash table.

Signup and view all the flashcards

What is a Collision in Hashing?

A situation where two different keys produce the same hash value, leading to both records trying to be stored in the same location.

Signup and view all the flashcards

What is Collision Resolution?

Techniques used to handle collisions in hash tables when two or more keys hash to the same index.

Signup and view all the flashcards

What is Separate Chaining?

A collision resolution method that involves creating linked lists for each hash table index to store multiple records that hash to the same location.

Signup and view all the flashcards

What is Open Addressing?

A collision resolution technique where, instead of using linked lists, all data is stored directly within the hash table.

Signup and view all the flashcards

What is searching in an open addressing scheme?

Moving through a hash table to find a specific key, especially after a collision.

Signup and view all the flashcards

What happens when you deleting a record?

Marking a location in a hash table in a special way after deleting a record.

Signup and view all the flashcards

Study Notes

  • This chapter discusses various methods for storing and searching information in an array.
  • Hash tables are a common approach to solving storing/searching problems.

What is a Hash Table?

  • The simplest kind of hash table is an array of records, this example has 701 records.
  • Each record has a special field, called its key.
  • In this example, the key is a long integer field called "Number".
  • The number might be someone's identification number, and the remainder of the record includes details about the individual.
  • When a hash table is in use, certain spots hold valid records, while others are "empty".

Inserting a New Record

  • The key must be converted to an array index to insert a new record.
  • The hash value of the key refers to the index.
  • A typical method to create a hash value is: (Number mod 701).
  • Hash value is used for the new record's location.

Collisions

  • Collision occurs when a new record is being inserted, but the hash value of 2 already has a record in place.
  • When a collision occurs, proceed forward until an empty spot is found.

Collision Resolution

  • If an element hashes to the same value as one already inserted, a collision occurs and needs resolving.
  • Several methods exist for dealing with collision resolution, for example:
    • Separate chaining
    • Open addressing

Collision Resolution with Separate Chaining

  • The list of all elements that hash to the same value is kept as part of the idea.
  • Array elements point to the lists' first nodes.
  • Insertion of a new item is performed at the front of the list.
  • Advantages include:
    • Superior space utilization for substantial items.
    • Simple handling of collisions by searching a linked list.
    • Overflow: It is possible to store more items than the hash table size.
    • Quick and easy deletion performed from the linked list.

Example

  • Keys: 0, 1, 4, 9, 16, 25, 36, 49, 64, 81
  • hash(key) = key % 10

Collision Resolution with Open Addressing

  • The disadvantage of using linked lists exists when separate chaining is used.
  • Implementation of a second data structure is required.
  • All data is stored inside the table with open addressing hashing.
  • A bigger table is needed.
  • Alternative cells will be used until an empty cell is available, if a collision happens.

Open Addressing - Searching for a Key

  • Open addressing makes it fairly quick to find the data attached to a key.
  • Begin by calculating the hash value.
  • Determine the location of the key in the array.
  • Keep moving forward if the key is not at the location until you find it, or you reach an empty spot.
  • The information can be moved to the required area once the item has been located.

Deleting a Record

  • Records can also be removed from a hash table.
  • Because this may disrupt searches, the location cannot merely be left as an ordinary “empty spot."
  • The location needs to be marked in a specific way so that searches can recognize that it once contained something.

Summary

  • Hash tables store a collection of records with keys.
  • A record's location depends on the hash value of its key.
  • When a collision occurs, the next available location is utilized either by chaining, or by open addressing.
  • In general, it is quick to look up a specific key.
  • When an item is removed, the location needs to be marked in a specific way so that the searches are aware that the spot was formerly in use.

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team

Related Documents

Hash Tables Explained

More Like This

Hashing in Computer Science
12 questions

Hashing in Computer Science

ImmaculateFallingAction avatar
ImmaculateFallingAction
Hash Table and Hash Function Quiz
10 questions
Sorting Algorithms and Hash Tables
25 questions
Use Quizgecko on...
Browser
Browser