Practical Data Structures
-
A Stack and Last-In, First-Out Order
A stack exposes the newest retained element. Push adds to the top, and pop removes that element.
-
A Queue and First-In, First-Out Order
A FIFO queue exposes the oldest retained element. New items enter behind all pending items.
-
Using Both Ends of a Deque
A double-ended queue supports insertion and removal at the front and back. A useful interface can restrict that flexibility to express a policy.
-
Checking Matched Delimiters with a Stack
A delimiter stack records which opener needs to be closed next, so it checks order as well as counts.
-
A Priority Queue and Its Tie Rule
A priority queue selects an item by its ordering key. Its interface should specify what to expect when keys tie.
-
Reading a Binary Heap by Hand
A binary min-heap keeps each parent no greater than its children. This places a minimum at the root and permits disorder between siblings.
-
A Linked List as Linked Nodes
A singly linked list represents order through references from one node to the next. Reading an offset generally requires following that many links.
-
Two Neighbor Links in a List
A doubly linked list records a previous and next reference for each node. An edit should preserve their agreement.
-
A Ring Buffer by Indices
A ring buffer uses fixed storage and wraps logical positions back to the beginning of its physical array.
-
Choosing a Policy for a Full Queue
A bounded queue needs a documented outcome when an insertion reaches capacity, including which entries are affected.
-
The Ordering Rule of a Binary Search Tree
A binary search tree places items on sides of a node according to a comparison rule. The rule must hold across the entire corresponding subtree.
-
Inorder Traversal of a Binary Search Tree
Inorder traversal visits the left subtree, then the node, then the right subtree. Under the search-tree rule, keys appear in sorted order.
-
Ordered Maps and Insertion-Ordered Maps
An ordered map can support queries by key order. A dictionary preserving insertion order can replay the history of insertions.
-
Selecting a Range from Ordered Records
A range query uses ordered keys to find its start and then returns only the items before its stop boundary.
-
Following Prefixes in a Trie
A trie shares initial parts of keys. The presence of a path for a prefix and the presence of a complete key have separate flags.
-
Two Keys in One Hash Bucket
A hash table can map distinct keys to the same bucket. Its collision-handling rule preserves the ability to distinguish them.
-
Load Factor of a Hash Table
A load factor compares stored entries with table slots or buckets according to the table's design.
-
Tracing Linear Probing in a Hash Table
Linear probing visits successive slots after a collision, wrapping according to the table's capacity.
-
Why Growing a Hash Table Repositions Keys
A common table indexing rule depends on capacity. Growing that capacity can change a key's home slot even when its hash stays constant.
-
Parent Pointers in Disjoint Sets
A disjoint-set structure represents each group through parent pointers leading to a representative root.
-
Tracking Recency with a Map and a List
A recency cache can combine a map for locating entries with an ordering structure for moving and evicting them.
-
Representing Intervals as Pairs
An interval record holds endpoints and an agreed convention about whether each is included.
-
Merging Ordered Intervals
An interval union routine sorts by start and keeps the furthest active end while consecutive ranges overlap.
-
Adjacency Lists as Neighbor Records
An adjacency list stores each vertex with the edges or neighbors that can be reached in one step.
-
Edge Lists and Adjacency Matrices
An edge list represents actual connections as records. An adjacency matrix assigns a position to each possible ordered pair of vertices.
-
Storing a Sparse Vector
A sparse vector can store its dimension and only the positions whose values differ from the default zero.
-
Using Prefix Totals for Range Sums
An array of accumulated totals can answer an unchanged sequence's range sums using two array reads and a subtraction.
-
A Sentinel Node at a List Boundary
A sentinel node represents a structural boundary, so ordinary nodes can use a more uniform neighbor rule at the ends.
-
Maintaining a Sorted Sequence under Insertion
A sorted sequence can accept new items by locating their insertion position and then moving storage as needed.
-
Maintaining a Separate Queue for Each Key
A map of queues preserves arrival order for each key while allowing the application to choose which key to service next.