Nepedia
Back to Discover
Independent Reader
💻

Red–black tree

technology Maturity 7-9 Next article →

Computers use special tools to store and find information quickly. One tool is called a red-black tree.

Red-black tree example with sockets
Red-black tree example with sockets

A red-black tree is a type of binary search tree. It is a way to organize data in parts called nodes. Each node holds a piece of information. In this tree, every node also has a color. The color is either red or black.

BlackNode
BlackNode

The colors help keep the tree balanced. A balanced tree is not too tall or lopsided. This helps the computer find things fast. The tree follows strict rules. For example, a red node cannot have a red child. Also, every path from the top to the bottom must have the same number of black nodes.

When you add or remove information, the tree might break these rules. To fix it, the computer uses a set of steps called rebalancing. It can change the colors of the nodes. It can also move parts around using a way called rotation.

Binary Tree Rotation (animated)
Binary Tree Rotation (animated)

These trees are very useful. They help the Linux kernel work well. They are also used in the Java HashMap to make searching better.

185 words

Where will your curiosity go next?

Keep exploring at your chosen reading level.

🖼️ Images & Media (11)
File:Red-black tree example with sockets.svg
Red-black tree example with sockets.svg
File:Red-black trees and 2–3–4 trees.svg
Red-black trees and 2–3–4 trees.svg
File:Binary Tree Rotation (animated).gif
Binary Tree Rotation (animated).gif
File:RedNode.svg
RedNode.svg
File:BlackNode.svg
BlackNode.svg
File:RedOrBlackNode.svg
RedOrBlackNode.svg
File:TriangleTop.svg
TriangleTop.svg
File:TriangleSubtree.svg
TriangleSubtree.svg
File:Check-green.svg
Check-green.svg
File:NilBlue.svg
NilBlue.svg
File:5 minimal red-black trees nN.svg
5 minimal red-black trees nN.svg
More to explore

What is Nepedia?

A free, ad-free encyclopedia for children. Every article is written at five reading levels, so the same page works for a five-year-old and a fifteen-year-old — use the level switcher above to see this one change. No account needed to read.