~/learning/programming/data-structures

Data Structures

10 notes·updated

Data structures are a way of organizing and storing data so that they can be accessed and worked with efficiently. They define the relationship between the data, and the operations that can be performed on the data.

These notes are meant to be a quick reference for the most common data structures and their use cases.

Overview

Arrays

Arrays are the simplest and most widely used data structure. They store a collection of elements with O(1) access by index.

Strings

Strings represent sequences of text; their encoding and indexing behavior depend on the language.

Linked Lists

Linked lists are a collection of nodes, where each node contains a value and a reference to the next node.

Stacks

Stacks follow LIFO (Last-In-First-Out) order. Elements are added and removed from the top.

Queues

Queues follow FIFO (First-In-First-Out) order. Elements are added to the back and removed from the front.

Trees

Trees are hierarchical structures where each node contains a value and references to its children.

Heaps

Heaps are complete binary trees that maintain a heap property, enabling efficient priority queue operations.

Graphs

Graphs are nodes with edges connecting them, used for modeling complex relationships.

Hash Tables

Hash tables store key-value pairs with O(1) average access time.

Sets and Maps

Collections of unique elements (Set) and key-value pairs (Map).

Cheatsheet

Data StructureAccessFeaturesUse Cases
ArraysO(1)Fixed size, contiguous memoryFast access, known size
Linked ListsO(n)Dynamic sizeFrequent insertions/deletions
StringsDepends on encoding and operationText representation varies by languageText manipulation
StacksO(1) push/popLIFOUndo, recursion, DFS
QueuesO(1) enqueue/dequeueFIFOBFS, buffering
TreesO(log n) balanced BST; O(n) worst case without balancingHierarchicalSorted data, hierarchies
HeapsO(1) find min/maxComplete binary treePriority queues
GraphsDepends on representation and operationNodes + edgesNetworks, pathfinding
Hash TablesO(1) averageKey-valueFast lookups
Sets/MapsO(1) averageUnique elementsMembership, associations