Data Structure Visualizer
Pick a data structure, perform operations, and watch the SVG diagram update in real time. Explore five fundamental structures that every computer science student needs to understand.
Controls
Operation Log
No operations yet. Try pushing a value onto the stack.
Reference Guide
Stacks and Queues
A stack follows Last-In-First-Out (LIFO) order. Think of a stack of plates: you add and remove from the top. Push adds to the top, pop removes from the top. Both operations run in O(1) time.
A queue follows First-In-First-Out (FIFO) order. Think of a line at a store: you join at the back and leave from the front. Enqueue adds to the rear, dequeue removes from the front. Both operations run in O(1) time.
Stacks power function call tracking, undo systems, and expression evaluation. Queues drive breadth-first search, print job scheduling, and message buffers.
Linked Lists
A linked list stores elements in nodes, where each node points to the next. Unlike arrays, elements are not stored in contiguous memory, so insertion and deletion at any position take O(1) time if you already have a reference to the node.
Inserting or deleting at the head is O(1). Searching for a value requires traversing the list, taking O(n) time in the worst case.
Linked lists are the foundation for implementing stacks, queues, hash table chaining, and adjacency lists in graph algorithms.
Binary Search Trees
A BST stores values so that for every node, all values in its left subtree are smaller and all values in its right subtree are larger. This ordering property makes search, insert, and delete run in O(h) time where h is the tree height.
An inorder traversal of a BST visits nodes in sorted order. Preorder visits root-left-right (useful for copying), and postorder visits left-right-root (useful for deletion).
In the best case (balanced), h = O(log n). In the worst case (completely skewed), h = O(n). Self-balancing variants like AVL and red-black trees guarantee O(log n).
Heaps and Priority Queues
A min heap is a complete binary tree where every parent is smaller than its children. The minimum element is always at the root. Insert adds a value at the end and bubbles it up. Extract-min removes the root, moves the last element to the root, and bubbles it down.
Both insert and extract-min run in O(log n) time. Peek at the minimum is O(1). Heaps are commonly stored as arrays where the children of index i are at 2i+1 and 2i+2.
Heaps power priority queues, Dijkstra's shortest path, heap sort, and event-driven simulations.