A red-black tree is a binary search tree with one extra bit per node, its colour. A few simple rules about colours keep the tree roughly balanced: its height is at most 2log2(n+1). Red-black trees are used in many standard libraries, for example C++ std::map, Java TreeMap and the Linux kernel’s scheduler.
Properties
The black-height bh(x) is the number of black nodes on any path from x (not counting x) down to a NIL leaf. Properties 3 and 4 together give the height bound: a path can at most alternate red and black, so it is at most twice as long as the all-black path.
Rotations
Red-black trees use the same left and right rotations as AVL trees (see the AVL section above): a left rotation at x makes x‘s right child y the root of the subtree, x becomes y‘s left child, and y‘s old left subtree becomes x‘s right subtree. A right rotation is the mirror image. Both keep the inorder order and take O(1) time.
Insertion
Insert the new node z as in a BST and colour it red (this cannot break property 4).
If z‘s parent is black, nothing else is needed. If z is the root, colour it black.
If z‘s parent is red (property 3 is broken), look at z‘s uncley (the parent’s sibling). With z‘s parent a left child:
Case 1 — uncle red: colour the parent and uncle black and the grandparent red, then repeat the check with z = grandparent.
Case 2 — uncle black, z a right child: set z = parent and left-rotate at z; this turns the bend into a line (case 3).
Case 3 — uncle black, z a left child: colour the parent black and the grandparent red, then right-rotate at the grandparent.
The mirror cases apply when z‘s parent is a right child. Finally, colour the root black.
Case 1 may repeat up the tree, but cases 2 and 3 end the fix-up, so an insertion needs at most two rotations.
Deletion (outline)
Deletion first removes the node as in a BST. If the node actually removed (the deleted node or its successor) was black, some paths have lost a black node. The fix-up gives the node x that took its place an “extra black” and pushes it up the tree while x is not the root and x is black, using x‘s sibling w:
Case 1:w red → recolour and rotate to get a black sibling (then case 2, 3 or 4).
Case 2:w black with two black children → colour w red and move the extra black up to the parent.
Case 3:w black, w‘s far child black and near child red → rotate at w to reach case 4.
Case 4:w black with a red far child → recolour and rotate at the parent; the extra black disappears and the loop ends.
Finally x is coloured black. At most three rotations are needed.
Key takeaways
Properties: red/black, root black, NIL leaves black, red nodes have black children, equal black counts.
Height ≤ 2log2(n+1), so operations are O(logn) worst case.
Insert red; fix up: uncle red → recolour and move up; uncle black → rotate (case 2 then 3).
Deletion fix-up handles an “extra black” with four sibling cases; at most three rotations.
Practice questions
Answer on paper first, then check. The explanation matters more than the letter you chose.
Question 6 · short answer
Compare AVL trees and red-black trees (at least four points).
Show worked answer
(1) Balance rule: AVL keeps subtree heights within 1 at every node; red-black trees use colour rules (no red-red, equal black counts). (2) Height: AVL ≤ about 1.44log2n; red-black ≤ 2log2(n+1), so AVL trees are more tightly balanced and slightly faster to search. (3) Updates: AVL insertion needs at most one (single or double) rotation, but deletion may need O(logn) rotations; red-black insertion needs at most 2 rotations and deletion at most 3, plus recolouring. (4) Extra storage: AVL stores a balance factor or height; red-black stores one colour bit. (5) Use: AVL for search-heavy workloads; red-black for update-heavy ones and in standard libraries (C++ std::map, Java TreeMap).
MCA Online notes are made for reading on screen. Open https://mcastudy.online/mcs-208/advanced-trees/red-black-trees/ to read this page.