Data Structures

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/20

flashcard set

Earn XP

Description and Tags

Memorizing data structures, remembering the time complexities, ups and downsides, and when to use one over the other

21 Terms

1
New cards

std::vector

Dynamic array; O(1) amortized push_back and O(1) random access by index, O(n) insert or erase in the middle.

2
New cards

std::list

Doubly linked list; O(1) insert or erase at both ends and O(1) insert or erase at a known iterator, but O(n) random access.

3
New cards

std::deque

Double-ended queue; O(1) push or pop at both front and back, O(1) random access, O(n) insert in the middle.

4
New cards

std::set

Ordered set implemented as a red-black tree; O(log n) insert, erase, and find, with elements kept in sorted order.

5
New cards

std::unordered_set

Hash-based set with unique keys; O(1) average insert, erase, and find, but O(n) worst case and no sorted order.

6
New cards

std::map

Ordered key-value map implemented as a red-black tree; O(log n) insert, erase, and lookup by key.

7
New cards

std::unordered_map

Hash-based key-value map; O(1) average insert, erase, and lookup by key, but O(n) worst case and no sorted order.

8
New cards

std::stack

Container adapter with LIFO access; O(1) push, pop, and top.

9
New cards

std::queue

Container adapter with FIFO access; O(1) push, pop, front, and back.

10
New cards

std::priority_queue

Max-heap priority queue; O(log n) push and pop, O(1) top. Use when you repeatedly need the largest element.

11
New cards

std::array

Fixed-size array; O(1) random access and O(1) size, with no resizing. Use when the size is known at compile time.

12
New cards

Disjoint Set Union (Union-Find)

Disjoint set union (DSU); near O(α(n)) per union and find operation with path compression and union by size. Use for dynamic connectivity and grouping problems.

13
New cards

Binary search on a sorted vector

Sorted array searched by halving the range; O(log n) lookup and O(n) insertion. Use when the data is static and sorted.

14
New cards

Trie

Prefix tree for strings; O(L) insert and search, where L is the string length. Use for prefix queries and autocomplete.

15
New cards

Segment tree

Tree of range aggregates; O(log n) point update and range query, and O(n) build. Use for range sum or min queries with updates.

16
New cards

Prefix sum array

Array of prefix sums; O(1) range sum query after O(n) build, but no efficient updates. Use for static range sum queries.

17
New cards

std::multiset

Ordered multiset implemented as a red-black tree; allows duplicate keys, stores equal keys together in sorted order, O(log n) insert and find, and erase by key removes all copies.

18
New cards

std::unordered_multiset

Hash-based multiset that allows duplicate keys; O(1) average insert, erase, and find, but O(n) worst case and no sorted order.

19
New cards

Key difference between std::set and std::multiset

std::set stores each key at most once, while std::multiset allows duplicate keys; both keep elements sorted with O(log n) insert, erase, and find.

20
New cards

Situation that calls for std::multiset instead of std::set

A sorted collection that must keep repeated values, such as tracking every occurrence of a value while still answering smallest or largest queries efficiently.

21
New cards

Removing only one copy of a key from std::multiset

Call find(x) to get an iterator to one copy of x, then call erase on that iterator; O(log n) total. Calling erase(x) directly removes every copy of x.