Dictionary (ADT)

Context: FIT1008_MOC · a container keyed by label, unlike List (ADT) · implemented by a Hash Table · a Set (ADT) with values

Quick Revision

  • 🎯 Objective: look things up by what they are (a unique key) ➔ not by position — x[“Name”].
  • 📦 Core Components: search / add / update / delete, all keyed ➔ hash-backed vs tree-backed.
  • ⚡ Key Constraint: hash map expected but unordered; tree map but ordered (range/successor).

📝 Core

1. The Dictionary (Keyed Access)

  • Map ➔ each value uniquely identified by a key, not position ➔ Python’s dict on a Hash Table.
  • Operations ➔ search, add, update, delete — all keyed.
  • Add vs update ➔ decided purely by key presence (keys are unique).

2. Two Realisations

  • Hash-backedHash Table ➔ expected keyed ops, no key order.
  • Tree-backed ➔ balanced Binary Tree keyed ops, sorted key order (range/successor, ordered iteration).
  • Recurring choice ➔ ” unordered vs ordered”.

⚙️ Core Implementation

🔹 Keyed operations

⚖️ Core Decision Matrix

OperationHash map (expected)Tree map (worst)
search x[k]
add / update amortised
delete
ordered iteration (must sort) in order

When It Flips: the hash map forfeits ordering for speed; the tree map keeps order at a factor. Hash-map is expected (good hashing) and amortised (rehash on growth) — adversarial keys degrade it to . A dictionary is a Set (ADT) with values attached to each key — same membership machinery.

📊 Exam Execution Trace

Manual Execution Trace

Choosing a backing store:

Step / StateRequirementChoose
0 (Init)
1fastest point lookups, order irrelevanthash map ( expected)
2range queries / sorted iterationtree map (, ordered)
3worst-case guaranteestree map ( worst vs hash )

Applied Exercise

Problem: Show add vs update is decided by key presence. Derivation Proof / Hand-Calculation Walkthrough:

Final Extracted Output: the unique-key invariant means the same syntax branches on membership — no duplicate keys ever coexist.

🧠 Active Recall