Data Structures: Sets and Hash Tables

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 characteristic of a perfect hash function?

  • It can work efficiently with an unknown number of keys.
  • It cannot map search keys to locations in a hash table.
  • It allows for collisions to occur without issues.
  • It maps each key to a unique slot when all keys are known beforehand. (correct)

Which of the following best describes double hashing?

  • It can only be used with separate chaining.
  • It is a method that increases the chances of clustering.
  • It is the same as linear probing.
  • It is a technique that significantly reduces clustering. (correct)

What happens when a search key that already exists in a dictionary is added again?

  • The dictionary grows in size.
  • The value associated with the key is updated. (correct)
  • The old value associated with the key is discarded.
  • The operation fails with an error.

Which statement regarding linear probing is accurate?

<p>It involves searching for the next free slot sequentially. (D)</p> Signup and view all the answers

Which statement about collisions in hashing is true?

<p>Collisions occur when two keys hash to the same index. (D)</p> Signup and view all the answers

What happens if an attempt is made to add a duplicate value to a set?

<p>The value is ignored. (A)</p> Signup and view all the answers

Which method of collision handling uses a linked list at each hash index?

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

What is the main issue associated with linear probing in hash tables?

<p>Leads to clustering. (D)</p> Signup and view all the answers

What is a primary requirement of a hash function to ensure effective distribution of keys?

<p>Uniform Distribution (B)</p> Signup and view all the answers

What is the average time complexity for searching an element in a hash table?

<p>O(1) (B)</p> Signup and view all the answers

What is the load factor of a hash table if there are 650 entries and the table size is 1500?

<p>0.433 (A)</p> Signup and view all the answers

Which programming statement correctly compresses a hash code to a valid array index?

<p>position = h % arrayLength; (D)</p> Signup and view all the answers

Which aspect of hash tables indicates that the output must only depend on the input?

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

Flashcards

Hash Table

A data structure that stores key-value pairs using a hash function to map keys to locations in an array.

Hash Function

A function that converts a key into a numerical index (hash code) for storage in a hash table.

Hash Code

A numerical value produced by a hash function that determines the position of an element in the hash table.

Collision

When two different keys produce the same hash code, resulting in two items needing to be stored at the same location in the hash table.

Signup and view all the flashcards

Chaining

A method for handling collisions in a hash table where multiple elements with the same hash code are stored in a linked list at the same index.

Signup and view all the flashcards

Load Factor

The ratio of the number of elements in the hash table to the table's size.

Signup and view all the flashcards

Set

A collection of unique elements emphasizing that no duplicates are allowed.

Signup and view all the flashcards

Time Complexity (Hash Table)

Indicates the performance of hash table operations, based on the number of elements (n).

Signup and view all the flashcards

Perfect Hash Function

A function that maps each key to a unique slot in a hash table, only possible when all keys are known in advance.

Signup and view all the flashcards

Re-hashing

Resizing a hash table and reassigning hash codes when the table is full.

Signup and view all the flashcards

Collision in Hashing

When two or more keys hash to the same index in a hash table.

Signup and view all the flashcards

Linear Probing

Searching sequentially for the next free slot in a hash table, starting from the initial hash index.

Signup and view all the flashcards

Key-Value Pair

The basic unit in a dictionary (or map), containing a unique key and its associated value.

Signup and view all the flashcards

Study Notes

Sets

  • Sets are collections of unique elements, designed for efficiency.
  • Duplicate elements are not allowed.
  • Removing an element that isn't present doesn't trigger an exception.
  • Elements cannot be added at specific positions within a set.

Hash Tables

  • Hash tables map keys to values via a hash function.
  • Elements are grouped by shared characteristics.
  • The hash code determines an element's position in the table.
  • Elements cannot be added at specific positions.

Hash Codes

  • Hash codes are numerical values, calculated for efficient indexing.
  • They must be consistent and uniform.
  • They cannot be directly used as array indexes (values might be too large).

Collisions

  • Collisions happen when multiple elements hash to the same index.
  • They are managed via chaining (linked lists) or probing.

Code Snippets

  • Compressing a hash code to an array index:
int h = x.hashCode();
if (h < 0) { h = -h; }
position = h % arrayLength;
  • Adding an element using chaining:
Node newNode = new Node();
newNode.data = x;
newNode.next = buckets[h];
buckets[h] = newNode;

Operations and Complexity

  • Add: Average: O(1), Worst: O(n)
  • Remove: Average: O(1), Worst: O(n)
  • Search: Average: O(1), Worst: O(n)

Collision Handling

  • Chaining: Uses linked lists (or other structures) at each index to handle multiple elements.
  • Probing: Searches for the next available slot in the array:
    • Linear Probing: Checks the next slot sequentially.
    • Quadratic Probing: Checks further slots to reduce clustering.
    • Double Hashing: Uses a second hash function for probe intervals.

Linear Probing Issues

  • Clustering: Leads to long contiguous groups, increasing search times.

Hash Function Requirements

  • Uniform Distribution: Evenly distributes keys across the table.
  • Consistency: Returns the same hash for the same input.
  • Deterministic Behavior: Output depends only on the input.

Key Definitions

  • Bucket: A structure (like a linked list) used in chaining.
  • Load Factor: The ratio of elements to table size.
    • Formula: Load Factor = Number of Entries / Table Size.
    • Example: 650/1500 = 0.433 (or 43.3%).

Special Techniques and Terms

  • Re-hashing: Resizing the hash table and reassigning hash codes.
  • Perfect Hash Function: Maps each key to a unique slot (possible only with all known keys).
  • Key-Value Pair: The fundamental unit of a dictionary/map.

True/False Key Facts

  • Statement 1: A perfect hash function maps each search key to a unique location. TRUE
  • Statement 2: Double hashing significantly reduces clustering. TRUE
  • Statement 3: Linear probing results in clustering. TRUE
  • Statement 4: Quadratic probing completely eliminates clustering. FALSE
  • Statement 5: Set difference is commutative. FALSE
  • Statement 6: With separate chaining, a dictionary's size can exceed the hash table's size. TRUE
  • Statement 7: The getValue(searchKey) method retrieves the search key for a given value. FALSE

Miscellaneous

  • Other names for ADT Dictionary: Map, Associative Array.
  • Map Ordering: Typically ordered by key.
  • Set Operations:
    • Union: Combines elements.
    • Intersection: Finds common elements.
    • Difference: Finds elements in one set but not the other.

Frequently Asked Questions

  1. Search Key Exists: Update the value associated, or reject the new entry.
  2. Primary Clustering: Causes groups of elements, increasing collisions.
  3. Collision: When two keys hash to the same index.
  4. Linear Probing: Searches sequentially for the next free slot.

Studying That Suits You

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

Quiz Team

More Like This

CS223: Data Structures - Hash Tables
24 questions
Hashing Techniques in Data Structures
10 questions
Data Structures: Hash Functions and Tries
8 questions
Use Quizgecko on...
Browser
Browser