Algorithms / 16 visualizations
Tree Algorithm Visualizers
BSTs, traversals, heaps, AVL rotations and tries.
Open the lab → Every one runs on your own input, step by step, with the code highlighted in Java, Python, C++, C or JavaScript. Free, no sign-up.
All trees visualizations
- BST InsertO(n)
Start at the root. Go left if the new value is smaller, right if bigger, until you reach an empty spot.
- BST SearchO(n)
Each comparison throws away one whole subtree, so a balanced tree is searched in O(log n) steps.
- BST DeleteO(n)
Find the node. With zero or one child, its child takes its place. With two children, copy in the smallest value from the right subtree, then delete that one.
- Inorder TraversalO(n)
Left subtree, then the node, then the right subtree.
- Preorder TraversalO(n)
The node first, then its left subtree, then its right subtree.
- Postorder TraversalO(n)
Both subtrees first, the node last.
- Level-Order TraversalO(n)
Visit the tree one level at a time from top to bottom, using a queue.
- Maximum Depth of a TreeO(n)
The depth of an empty tree is 0. Otherwise it is 1 plus the deeper of the two subtrees. Each node waits for both children, then reports up.
- Lowest Common Ancestor in a BSTO(n)
If p and q are both smaller than the node, the answer is on the left. Both bigger, on the right. Otherwise they split here (or one of them is this node): this node is the answer.
- Invert a Binary TreeO(n)
At each node, swap its left and right children, then invert both subtrees. An empty tree is already inverted.
- Validate a Binary Search TreeO(n)
Each node gets a range (low, high). The root may be anything. Going left, the node’s value becomes the new high; going right, it becomes the new low. A value outside its range breaks the tree.
- Min-Heap InsertO(log n)
Add the new value at the end of the array, then let it bubble up while it is smaller than its parent.
- Min-Heap ExtractO(log n)
The minimum is always the root. Remove it, move the last value to the root, and let it sink down to the smaller child until the heap is valid.
- Kth Largest Element (Min-Heap of Size K)O(n log k)
Keep a min-heap of the k largest values seen so far. Push each value; if the heap grows past k, pop its smallest. At the end the root is the k-th largest.
- AVL Tree InsertO(log n)
A BST that keeps itself balanced. After each insert, if one side becomes more than 1 level taller, rotate to fix it.
- Trie (Prefix Tree)O(L)
Each edge is one letter. Words that share a prefix share the same path. A double ring marks where a full word ends.