Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

After this page, you should be able to:

Builds on: The operation contracts, collision traces and costs in the preceding readings.

Choose the behaviour a problem needs before choosing a representation. Maps and Sets answer different questions; a hash table can implement either. A Set implementation can store keys without separate associated records, while a Map must retain the values as well.

Required questionStarting choiceReason
Which name belongs to station S01?MapAn identifier retrieves an associated record
Have we visited S01?SetOnly membership matters
How often was S01 visited?Map of countsDistinctness alone loses frequency
Which task arrived first?QueueArrival order determines removal
Which pending task has highest priority?Priority QueuePriority determines removal
Which station is nearest to this coordinate?Spatial search structureExact-key hashing does not directly organise proximity

Choosing by operations and complexity

A complete design explanation states the required operations, their meaning, the chosen representation, the invariant it preserves and the cost assumptions. “Use hashing because it is O(1)” leaves out whether the query is exact-key lookup and what keeps the table efficient.

For a station directory, a hash-based Map is a plausible choice when exact identifier lookup and updates dominate. If ordered key traversal or range queries matter, compare it with a search-tree representation. A Python dictionary’s insertion order does not turn it into a sorted search tree.

For keys sharing initial symbols, Topic 15: strings and tries develops prefix reporting and explains why string length also matters to hash-table costs. Exact lookup and finding all keys beginning with a prefix are different operations.

Questions to revisit

Revisit hashing, chaining, open addressing and performance to connect each question to its invariant.

Related topics: ADTs and linear structures, BSTs, priority queues, and k-d trees.

Compare implementation choices without changing the ADT

RequirementImplementation consequence
Frequent exact-key updatesHashing is suitable under distribution and load assumptions
Ordered traversal, predecessor or key rangesCompare an ordered tree; insertion order is not key order
Predictable bounded storageFixed probing can reject full insertion; chaining uses growing buckets
Frequent deletionChaining removes bucket entries directly; probing needs path preservation and tombstone management
Distinctness versus associated valuesChoose Set versus Map before choosing a collision strategy

A complete explanation includes results and error policies as well as time and space. In this chapter Map remove returns its associated value; Set remove returns None. Consistency means making both contracts explicit, not forcing every ADT to have identical returns. For ordinary Python application code, use dict/set unless implementing the storage mechanism is the learning goal.

Check your understanding

Predict the result and resulting state before opening the explanation.

1. Must a Set return elements in insertion order because a Python dictionary preserves it?

Show explanation

No. That dictionary property is not a Set guarantee or a universal Map requirement.

2. Would a set of visited station IDs preserve how often each station was seen?

Show explanation

No. Use a map from station to count when frequency matters.

Next: Searching and BSTs.

Browse: Maps, Sets and Hashing · Concept index