Interactive · Data Structures
Data Structure Visualizer
See how the fundamental data structures behave. Push and pop a stack, enqueue and dequeue a queue, or insert and search a binary search tree — each operation animates so you can watch which end it touches and which path it walks.
Three structures, three access rules
- Stack (LIFO) — push and pop happen at the same end (the top). It's how function calls, undo history and bracket matching work. Both operations are O(1).
- Queue (FIFO) — enqueue at the rear, dequeue at the front, so items leave in the order they arrived. It drives schedulers, buffers and breadth-first search. Both operations are O(1).
- Binary search tree — smaller values go left, larger go right, so insert and search follow a single path down the tree, about O(log n) when balanced. An in-order walk always comes out sorted. But insert in sorted order and it degenerates into a list: the companion write-up measures the same 31 numbers averaging 5.3-comparison lookups inserted randomly versus 16 inserted in order.
More algorithm visualizers
Data structures are one half of the picture; the algorithms that run on them are the other. Watch six sorts race on a bar chart in the Sorting Algorithm Visualizer, see BFS, Dijkstra and A* in the Pathfinding Visualizer, or step through minimax and expectimax in the Game AI Visualizer. Everything runs client-side; nothing you enter leaves your browser.
Frequently asked questions
What is the difference between a stack and a queue?
A stack is LIFO — last in, first out: you push items onto the top and pop them off the same end, like a stack of plates. A queue is FIFO — first in, first out: you enqueue at the rear and dequeue from the front, like a line of people. Both push/pop and enqueue/dequeue run in O(1) constant time. The visualizer shows the top of the stack and the front and rear of the queue so you can see which end each operation touches.
How does a binary search tree work?
A binary search tree keeps every value smaller than a node in its left subtree and every larger value in its right subtree. To insert or search, you start at the root and go left or right by comparing values, so you only ever follow one path down the tree — about log n steps for a balanced tree. Press Insert or Search in the visualizer and watch the highlighted path walk down the tree. An in-order traversal of a BST always yields the values in sorted order.
What is the time complexity of these operations?
Stack push and pop and queue enqueue and dequeue are all O(1) — constant time, independent of how many items are stored. Binary search tree insert and search are O(log n) on average when the tree is reasonably balanced, but degrade to O(n) in the worst case if values are inserted in sorted order, which makes the tree a straight line. The status line reports how many comparisons each operation took.
Where are these data structures used?
Stacks power function call stacks, undo/redo and expression parsing; queues drive task scheduling, buffering and breadth-first search; binary search trees (and their balanced variants) back ordered maps and sets in many standard libraries. They are the building blocks behind most algorithms, which is why they are taught first. This tool runs the textbook versions in the browser so you can see each operation directly.