Red-Black Tree Visualizer
A self-balancing binary search tree that uses red and black node colors to maintain balance. Insert, delete and search, and see every rotation, recoloring, split and merge explained step by step.
How to use the Red-Black Tree Visualizer on KickSolve
Three quick steps, no account needed.
About the Red-Black Tree Visualizer on KickSolve
What it is and how it works.
What is a red-black tree?
A red-black tree is a binary search tree where every node is red or black. A few color rules guarantee that the longest path is at most twice as long as the shortest, so the tree stays balanced.
The five rules
- Every node is red or black
- The root is black
- Nil leaves are black
- A red node has no red child
- Every path from a node to its nil leaves has the same number of black nodes
Real-world uses
- Standard library maps and sets (C++ std::map, Java TreeMap)
- Linux kernel schedulers and memory management
- Any ordered structure with frequent inserts and deletes
Comparing trees? Try the AVL Tree, Red-Black Tree, B-Tree or B+ Tree visualizer.
Red-Black Tree cases at a glance
The situations the step-by-step log will name for you.
| Insert case | Uncle and shape | Fix |
|---|---|---|
| Case 1 | Uncle is red | Recolor parent and uncle black, grandparent red, then continue upward |
| Case 2 (triangle) | Uncle black, new node is an inner child | Rotate the parent to make a straight line, then apply Case 3 |
| Case 3 (line) | Uncle black, new node is an outer child | Rotate the grandparent and swap the colors of parent and grandparent |
| Root | New node is the root | Color it black |
Worked example
The same steps the visualizer animates for any input.
Insert 10: root, recolored black. Insert 20: red right child of 10. Parent is black, done. Insert 30: red right child of 20. Parent 20 is red, uncle is nil (black). Straight line (right-right) -> left rotation at 10. Recolor: 20 black, 10 red, 30 red. Result: 20 black root, with red children 10 and 30.
Red-Black Tree FAQ
Quick answers.
What is a red-black tree?
A self-balancing binary search tree where each node is red or black and color rules keep the tree balanced.
What are the red-black tree rules?
The root is black, nil leaves are black, a red node never has a red child, and all paths to leaves contain the same number of black nodes.
Why is a new node red?
Adding a red node never changes the black-height, so at most one rule (no red-red) can break and it is quick to fix.
When do I recolor and when do I rotate?
If the uncle is red, recolor. If the uncle is black, rotate (once for a straight line, twice for a triangle).
What is double black?
After deleting a black node, one path is missing a black. This extra black is pushed up or absorbed by rotations and recoloring, using the sibling cases.
What is the time complexity?
Search, insert and delete are all O(log n). Insert needs at most 2 rotations.
Red-black tree vs AVL tree?
Red-black trees allow slightly taller trees but rebalance with fewer rotations, so they suit write-heavy workloads.
Is the visualizer free and private?
Yes. Free, no signup, and everything runs in your browser.
More free tools from KickSolve
Other tree visualizers.