Reference

Data structures & costs

Compare operation costs, ordering, invariants and common algorithm notation.

Scope and conventions

Abstract data structures and conventional implementations. Costs use a unit-cost RAM model unless stated; n is item count, V vertices, E edges and k key/output length. Native containers can have different contracts. Expected, amortized and worst-case bounds are distinguished.

Lookup & ordering

Hash map / hash set

Operations & costs

A hash table uses hashing to locate candidate buckets. Expected bounds assume suitable hashing and controlled load; equality still resolves collisions.

Fragment

Operations & costs · Hash map / hash set
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
Lookup                    expected O(1)
Insert/delete (resizing)   expected amortized O(1)
Individual worst case     O(n) in a conventional table
Resize/rehash             O(n)
Sorted/range iteration    not provided by hashing alone

Hashing a k-length key can cost O(k). Ordering and adversarial protections depend on the implementation.

Sorted array

Operations & costs

Ordering enables logarithmic comparison search but does not eliminate shifts for insertion/deletion.

Fragment

Operations & costs · Sorted array
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
Binary search             O(log n) comparisons
Insert/delete             O(n) shifts
Return k adjacent matches O(log n + k)

Binary search needs efficient indexed access and a matching sort/comparison order.

Binary search tree

Operations & costs

The ordering invariant places smaller keys on one side and larger keys on the other, under a defined duplicate policy. Cost follows tree height h.

Fragment

Operations & costs · Binary search tree
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
Lookup/insert/delete      O(h)
Unbalanced worst height   O(n)
Balanced height           O(log n)

Insertion order can make an unbalanced tree degenerate into a chain.

Balanced ordered map / set

Operations & costs

A balancing scheme keeps height logarithmic. Ordered traversal and predecessor/successor/range operations are available through the chosen API.

Fragment

Operations & costs · Balanced ordered map / set
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
Lookup/insert/delete      O(log n)
Min/max                   O(log n) generally; may be cached
Range of k results        O(log n + k)

Constants and guarantees depend on the balancing scheme and implementation.

Binary heap / priority queue

Operations & costs

A min-heap guarantees each parent is no greater than its children. It exposes the minimum, without sorting every element.

Fragment

Operations & costs · Binary heap / priority queue
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
Peek minimum              O(1)
Insert                    O(log n)
Extract minimum           O(log n)
Build from n items        O(n)
Search arbitrary value    O(n)

Efficient change-key needs a handle/index map or another supported API. Max-heaps reverse the priority relation.

Trie / prefix tree

Operations & costs

Trie paths follow key symbols, making prefix queries natural. Bounds depend on child lookup representation.

Fragment

Operations & costs · Trie / prefix tree
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
Key of length k:
Lookup/insert             O(k) with O(1) child access
Prefix lookup             O(prefix length) + output traversal

Memory can be high; alphabet size and compressed paths change the tradeoff.

Disjoint set / union–find

Operations & costs

Maintains partition membership while merging sets. Path compression plus union by rank/size gives almost-constant amortized operations.

Fragment

Operations & costs · Disjoint set / union–find
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
find(x): representative of x’s set
union(a,b): merge their sets
Amortized operation cost: O(α(n))

α is the inverse Ackermann function. This structure does not support arbitrary edge deletions.