
In
computer science
Computer science is the study of computation, information, and automation. Computer science spans Theoretical computer science, theoretical disciplines (such as algorithms, theory of computation, and information theory) to Applied science, ...
, a red–black tree is a
self-balancing binary search tree
In computer science, a self-balancing binary search tree (BST) is any node-based binary search tree that automatically keeps its height (maximal number of levels below the root) small in the face of arbitrary item insertions and deletions.Donald ...
data structure
In computer science, a data structure is a data organization and storage format that is usually chosen for Efficiency, efficient Data access, access to data. More precisely, a data structure is a collection of data values, the relationships amo ...
noted for fast storage and retrieval of ordered information. The nodes in a red-black tree hold an extra "color" bit, often drawn as red and black, which help ensure that the tree is always approximately balanced.
When the tree is modified, the new tree is rearranged and "repainted" to restore the coloring properties that constrain how unbalanced the tree can become in the worst case. The properties are designed such that this rearranging and recoloring can be performed efficiently.
The (re-)balancing is not perfect, but guarantees searching in
time, where
is the number of entries in the tree. The insert and delete operations, along with tree rearrangement and recoloring, also execute in
time.
Tracking the color of each node requires only one bit of information per node because there are only two colors (due to memory alignment present in some programming languages, the real memory consumption may differ). The tree does not contain any other data specific to it being a red–black tree, so its
memory footprint
Memory footprint refers to the amount of main memory that a program uses or references while running.
The word footprint generally refers to the extent of physical dimensions that an object occupies, giving a sense of its size. In computing, t ...
is almost identical to that of a classic (uncolored)
binary search tree
In computer science, a binary search tree (BST), also called an ordered or sorted binary tree, is a Rooted tree, rooted binary tree data structure with the key of each internal node being greater than all the keys in the respective node's left ...
. In some cases, the added bit of information can be stored at no added memory cost.
History
In 1972,
Rudolf Bayer invented a data structure that was a special order-4 case of a
B-tree
In computer science, a B-tree is a self-balancing tree data structure that maintains sorted data and allows searches, sequential access, insertions, and deletions in logarithmic time. The B-tree generalizes the binary search tree, allowing fo ...
. These trees maintained all
paths from root to leaf with the same number of nodes, creating perfectly balanced trees. However, they were not ''binary'' search trees. Bayer called them a "symmetric binary B-tree" in his paper and later they became popular as
2–3–4 trees or even 2–3 trees.
In a 1978 paper, "A Dichromatic Framework for Balanced Trees",
Leonidas J. Guibas and
Robert Sedgewick derived the red–black tree from the symmetric binary B-tree. The color "red" was chosen because it was the best-looking color produced by the color laser printer available to the authors while working at
Xerox PARC.
[
] Another response from Guibas states that it was because of the red and black pens available to them to draw the trees.
In 1993, Arne Andersson introduced the idea of a right leaning tree to simplify insert and delete operations.
In 1999,
Chris Okasaki showed how to make the insert operation purely functional. Its balance function needed to take care of only 4 unbalanced cases and one default balanced case.
The original algorithm used 8 unbalanced cases, but reduced that to 6 unbalanced cases.
Sedgewick showed that the insert operation can be implemented in just 46 lines of
Java
Java is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea (a part of Pacific Ocean) to the north. With a population of 156.9 million people (including Madura) in mid 2024, proje ...
.
In 2008, Sedgewick proposed the
left-leaning red–black tree, leveraging Andersson’s idea that simplified the insert and delete operations. Sedgewick originally allowed nodes whose two children are red, making his trees more like 2–3–4 trees, but later this restriction was added, making new trees more like 2–3 trees. Sedgewick implemented the insert algorithm in just 33 lines, significantly shortening his original 46 lines of code.
Terminology
The black depth of a node is defined as the number of black nodes from the root to that node (i.e. the number of black ancestors). The black height of a red–black tree is the number of black nodes in any path from the root to the leaves, which, by
requirement 4, is constant (alternatively, it could be defined as the black depth of any leaf node).
The black height of a node is the black height of the subtree rooted by it. In this article, the black height of a null node shall be set to 0, because its subtree is empty as suggested by the example figure, and its tree height is also 0.
Properties
In addition to the requirements imposed on a
binary search tree
In computer science, a binary search tree (BST), also called an ordered or sorted binary tree, is a Rooted tree, rooted binary tree data structure with the key of each internal node being greater than all the keys in the respective node's left ...
the following must be satisfied by a
# Every node is either red or black.
# All null nodes are considered black.
# A red node does not have a red child.
# Every
path
A path is a route for physical travel – see Trail.
Path or PATH may also refer to:
Physical paths of different types
* Bicycle path
* Bridle path, used by people on horseback
* Course (navigation), the intended path of a vehicle
* Desir ...
from a given node to any of its leaf nodes goes through the same number of black nodes.
#(Conclusion) If a node N has exactly one child, the child must be red. If the child were black, its leaves would sit at a different black depth than N's null node (which is considered black by rule 2), violating
requirement 4.
Some authors, e.g. Cormen & al.,
claim "the root is black" as fifth requirement; but not Mehlhorn & Sanders
or Sedgewick & Wayne.
Since the root can always be changed from red to black, this rule has little effect on analysis.
This article also omits it, because it slightly disturbs the recursive algorithms and proofs.
As an example, every
perfect binary tree that consists only of black nodes is a red–black tree.
The read-only operations, such as search or tree traversal, do not affect any of the requirements. In contrast, the modifying operations
insert and
delete easily maintain requirements 1 and 2, but with respect to the other requirements some extra effort must be made, to avoid introducing a violation of requirement 3, called a red-violation, or of requirement 4, called a black-violation.
The requirements enforce a critical property of red–black trees: ''the path from the root to the farthest leaf is no more than twice as long as the path from the root to the nearest leaf''. The result is that the tree is
height-balanced. Since operations such as inserting, deleting, and finding values require worst-case time proportional to the height
of the tree, this upper bound on the height allows red–black trees to be efficient in the worst case, namely
logarithmic in the number
of entries, i.e. (a property which is shared by all self-balancing trees, e.g.,
AVL tree or
B-tree
In computer science, a B-tree is a self-balancing tree data structure that maintains sorted data and allows searches, sequential access, insertions, and deletions in logarithmic time. The B-tree generalizes the binary search tree, allowing fo ...
, but not the ordinary
binary search tree
In computer science, a binary search tree (BST), also called an ordered or sorted binary tree, is a Rooted tree, rooted binary tree data structure with the key of each internal node being greater than all the keys in the respective node's left ...
s). For a mathematical proof see section
Proof of bounds.
Red–black trees, like all
binary search tree
In computer science, a binary search tree (BST), also called an ordered or sorted binary tree, is a Rooted tree, rooted binary tree data structure with the key of each internal node being greater than all the keys in the respective node's left ...
s, allow quite efficient sequential access (e.g.
in-order traversal
In computer science, tree traversal (also known as tree search and walking the tree) is a form of graph traversal and refers to the process of visiting (e.g. retrieving, updating, or deleting) each node in a tree data structure, exactly once. S ...
, that is: in the order Left–Root–Right) of their elements. But they support also asymptotically optimal
direct access via a traversal from root to leaf, resulting in
search time.
Analogy to 2–3–4 trees
Red–black trees are similar in structure to
2–3–4 trees, which are
B-trees of order 4. In 2–3–4 trees, each node can contain between 1 and 3 values and have between 2 and 4 children. These 2–3–4 nodes correspond to black node – red children groups in red-black trees, as shown in figure 1. It is not a
1-to-1 correspondence, because 3-nodes have two equivalent representations: the red child may lie either to the left or right. The
left-leaning red-black tree variant makes this relationship exactly 1-to-1, by only allowing the left child representation. Since every 2–3–4 node has a corresponding black node, invariant 4 of red-black trees is equivalent to saying that the leaves of a 2–3–4 tree all lie at the same level.
Despite structural similarities, operations on red–black trees are more economical than B-trees. B-trees require management of vectors of variable length, whereas red-black trees are simply binary trees.
Applications and related data structures
Red–black trees offer worst-case guarantees for insertion time, deletion time, and search time. Not only does this make them valuable in time-sensitive applications such as
real-time applications, but it makes them valuable building blocks in other data structures that provide worst-case guarantees. For example, many data structures used in
computational geometry are based on red–black trees, and the
Completely Fair Scheduler and
epoll
epoll is a Linux kernel system call for a scalable I/O event notification mechanism, first introduced in version 2.5.45 of the Linux kernel. Its function is to monitor multiple file descriptors to see whether I/O is possible on any of them. It is ...
system call of the
Linux kernel
The Linux kernel is a Free and open-source software, free and open source Unix-like kernel (operating system), kernel that is used in many computer systems worldwide. The kernel was created by Linus Torvalds in 1991 and was soon adopted as the k ...
use red–black trees.
The
AVL tree is another structure supporting
search, insertion, and removal. AVL trees can be colored red–black, and thus are a subset of red-black trees. The worst-case height of AVL is 0.720 times the worst-case height of red-black trees, so AVL trees are more rigidly balanced. The performance measurements of Ben Pfaff with realistic test cases in 79 runs find AVL to RB ratios between 0.677 and 1.077, median at 0.947, and geometric mean 0.910. The performance of
WAVL trees lie in between AVL trees and red-black trees.
Red–black trees are also particularly valuable in
functional programming
In computer science, functional programming is a programming paradigm where programs are constructed by Function application, applying and Function composition (computer science), composing Function (computer science), functions. It is a declarat ...
, where they are one of the most common
persistent data structures, used to construct
associative array
In computer science, an associative array, key-value store, map, symbol table, or dictionary is an abstract data type that stores a collection of (key, value) pairs, such that each possible key appears at most once in the collection. In math ...
s and
set
Set, The Set, SET or SETS may refer to:
Science, technology, and mathematics Mathematics
*Set (mathematics), a collection of elements
*Category of sets, the category whose objects and morphisms are sets and total functions, respectively
Electro ...
s that can retain previous versions after mutations. The persistent version of red–black trees requires
space for each insertion or deletion, in addition to time.
For every
2–3–4 tree, there are corresponding red–black trees with data elements in the same order. The insertion and deletion operations on 2–3–4 trees are also equivalent to color-flipping and rotations in red–black trees. This makes 2–3–4 trees an important tool for understanding the logic behind red–black trees, and this is why many introductory algorithm texts introduce 2–3–4 trees just before red–black trees, even though 2–3–4 trees are not often used in practice.
In 2008,
Sedgewick introduced a simpler version of the red–black tree called the
left-leaning red–black tree by eliminating a previously unspecified degree of freedom in the implementation. The LLRB maintains an additional invariant that all red links must lean left except during inserts and deletes. Red–black trees can be made isometric to either
2–3 trees, or 2–3–4 trees,
for any sequence of operations. The 2–3–4 tree isometry was described in 1978 by Sedgewick.
With 2–3–4 trees, the isometry is resolved by a "color flip," corresponding to a split, in which the red color of two children nodes leaves the children and moves to the parent node.
The original description of the
tango tree, a type of tree optimised for fast searches, specifically uses red–black trees as part of its data structure.
As of
Java
Java is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea (a part of Pacific Ocean) to the north. With a population of 156.9 million people (including Madura) in mid 2024, proje ...
8, the
HashMap has been modified such that instead of using a
LinkedList to store different elements with
colliding hashcodes, a red–black tree is used. This results in the improvement of time complexity of searching such an element from
to
where
is the number of elements with colliding hashes.
Implementation
The read-only operations, such as search or tree traversal, on a red–black tree require no modification from those used for
binary search tree
In computer science, a binary search tree (BST), also called an ordered or sorted binary tree, is a Rooted tree, rooted binary tree data structure with the key of each internal node being greater than all the keys in the respective node's left ...
s, because every red–black tree is a special case of a simple binary search tree. However, the immediate result of an insertion or removal may violate the properties of a red–black tree, the restoration of which is called ''rebalancing'' so that red–black trees become self-balancing.
Rebalancing (i.e. color changes) has a worst-case time complexity of
and average of
,
though these are very quick in practice. Additionally, rebalancing takes no more than three
tree rotation
In discrete mathematics, tree rotation is an operation on a binary tree that changes the structure without interfering with the order of the elements. A tree rotation moves one node up in the tree and one node down. It is used to change the shape ...
s (two for insertion).
This is an example implementation of insert and remove in
C. Below are the data structures and the
rotate_subtree
helper function used in the insert and remove examples.
enum Color ;
enum Dir ;
// red-black tree node
struct Node ;
struct Tree ;
#define DIRECTION(N) (N N->parent->right ? RIGHT : LEFT)
struct Node *rotate_subtree(struct Tree *tree, struct Node *sub, enum Dir dir)
Notes to the sample code and diagrams of insertion and removal
The proposal breaks down both insertion and removal (not mentioning some very simple cases) into six constellations of nodes, edges, and colors, which are called cases. The proposal contains, for both insertion and removal, exactly one case that advances one black level closer to the root and loops, the other five cases rebalance the tree of their own. The more complicated cases are pictured in a diagram.
*

symbolises a red node and

a (non-NULL) black node (of black height ≥ 1),

symbolises the color red or black of a non-NULL node, but the same color throughout the same diagram. NULL nodes are not represented in the diagrams.
* The variable N denotes the current node, which is labeled
N or
N in the diagrams.
* A diagram contains three columns and two to four actions. The left column shows the first iteration, the right column the higher iterations, the middle column shows the segmentation of a case into its different actions.
[The left columns contain far less nodes than the right ones, especially for removal. This indicates that some efficiency can be gained by pulling the first iteration out of the rebalancing loops of insertion and deletion, because many of the named nodes are NIL nodes in the first iteration and definitively non-NIL later. (See also this remark.)]
# The action "entry" shows the constellation of nodes with their colors which defines a case and mostly violates some of the
requirements
In engineering, a requirement is a condition that must be satisfied for the output of a work effort to be acceptable. It is an explicit, objective, clear and often quantitative description of a condition to be satisfied by a material, design, pro ...
.
A blue border rings the current node N and the other nodes are labeled according to their relation to N.
# If a
rotation
Rotation or rotational/rotary motion is the circular movement of an object around a central line, known as an ''axis of rotation''. A plane figure can rotate in either a clockwise or counterclockwise sense around a perpendicular axis intersect ...
is considered useful, this is pictured in the next action, which is labeled "rotation".
# If some recoloring is considered useful, this is pictured in the next action, which is labeled "color".
# If there is still some need to repair, the cases make use of code of other cases and this after a reassignment of the current node N, which then again carries a blue ring and relative to which other nodes may have to be reassigned also. This action is labeled "reassign".
For both, insert and delete, there is (exactly) one case which iterates one black level closer to the root; then the reassigned constellation satisfies the respective loop invariant.
* A possibly numbered triangle with a black circle atop

represents a red–black subtree (connected to its parent according to
requirement 3) with a black height equal to the iteration level minus one, i.e. zero in the first iteration. Its root may be red or black.
A possibly numbered triangle

represents a red–black subtree with a black height one less, i.e. its parent has black height zero in the second iteration.
; Remark
: For simplicity, the sample code uses the
disjunction
In logic, disjunction (also known as logical disjunction, logical or, logical addition, or inclusive disjunction) is a logical connective typically notated as \lor and read aloud as "or". For instance, the English language sentence "it is ...
:
::
U NULL , , U->color BLACK ''// considered black''
: and the
conjunction:
::
U != NULL && U->color RED ''// not considered black''
: Thereby, it must be kept in mind that both statements are ''not'' evaluated in total, if
U NULL
. Then in both cases
U->color
is not touched (see
Short-circuit evaluation
Short-circuit evaluation, minimal evaluation, or McCarthy evaluation (after John McCarthy) is the semantics of some Boolean operators in some programming languages in which the second argument is executed or evaluated only if the first argumen ...
).
(The comment
considered black
is in accordance with
requirement 2.)
: The related
if
-statements have to occur far less frequently if the proposal
is realised.
Insertion
Insertion begins by placing the new (non-NULL) node, say N, at the position in the
binary search tree
In computer science, a binary search tree (BST), also called an ordered or sorted binary tree, is a Rooted tree, rooted binary tree data structure with the key of each internal node being greater than all the keys in the respective node's left ...
of a NULL node whose
in-order predecessor’s key compares less than the new node’s key, which in turn compares less than the key of its in-order successor.
(Frequently, this positioning is the result of a search within the tree immediately preceding the insert operation and consists of a node
P
together with a direction
dir
with
The newly inserted node is temporarily colored red so that all paths contain the same number of black nodes as before.
But if its parent, say P, is also red then this action introduces a
red-violation.
// parent is optional
void insert(struct Tree *tree, struct Node *node, struct Node *parent, enum Dir dir)
The rebalancing loop of the insert operation has the following
invariants:
* Node is the current node, initially the insertion node.
* Node is red at the beginning of each iteration.
*
Requirement 3 is satisfied for all pairs node←parent with the possible exception node←parent when parent is also red (a
red-violation at node).
* All other properties (including
requirement 4) are satisfied throughout the tree.
Notes to the insert diagrams
* In the diagrams, P is used for N’s parent, G for its grandparent, and U for its uncle. In the table, "—" indicates the root.
* The diagrams show the parent node P as the left child of its parent G even though it is possible for P to be on either side. The sample code covers both possibilities by means of the side variable
dir
.
* The diagrams show the cases where P is red also, the red-violation.
* The column ''x'' indicates the change in child direction, i.e. o (for "outer") means that P and N are both left or both right children, whereas i (for "inner") means that the child direction changes from P’s to N’s.
* The
column group ''before'' defines the case, whose name is given in the column ''case''. Thereby possible values in cells left empty are ignored. So in case
I2 the sample code covers both possibilities of child directions of N, although the corresponding diagram shows only one.
* The rows in the synopsis are ordered such that the coverage of all possible RB cases is easily comprehensible.
* The column ''rotation'' indicates whether a rotation contributes to the rebalancing.
* The column ''assignment'' shows an assignment of N before entering a subsequent step. This possibly induces a reassignment of the other nodes P, G, U also.
* If something has been changed by the case, this is shown in the column group ''after''.
* A

sign in column ''next'' signifies that the rebalancing is complete with this step. If the column ''after'' determines exactly one case, this case is given as the subsequent one, otherwise there are question marks.
* In case
2 the problem of rebalancing is escalated
tree levels or 1 black level higher in the tree, in that the grandfather G becomes the new current node N. So it takes maximally
steps of iteration to repair the tree (where is the height of the tree). Because the probability of escalation decreases exponentially with each step the total rebalancing cost is constant on average, indeed
amortized constant.
* Rotations occur in cases I6 and I5 + I6 – outside the loop. Therefore, at most two rotations occur in total.
Insert case 1
The current node’s parent P is black, so
requirement 3 holds. Requirement 4 holds also according to the
loop invariant
In computer science, a loop invariant is a property of a program loop that is true before (and after) each iteration. It is a logical assertion, sometimes checked with a code assertion. Knowing its invariant(s) is essential in understanding th ...
.
Insert case 2
If both the parent P and the uncle U are red, then both of them can be repainted black and the grandparent G becomes red for maintaining
requirement 4. Since any path through the parent or uncle must pass through the grandparent, the number of black nodes on these paths has not changed. However, the grandparent G may now violate requirement 3, if it has a red parent. After relabeling G to N the
loop invariant
In computer science, a loop invariant is a property of a program loop that is true before (and after) each iteration. It is a logical assertion, sometimes checked with a code assertion. Knowing its invariant(s) is essential in understanding th ...
is fulfilled so that the rebalancing can be iterated on one black level (= 2 tree levels) higher.
Insert case 3
Insert case 2 has been executed for
times and the total height of the tree has increased by 1, now being .
The current node N is the (red) root of the tree, and all RB-properties are satisfied.
Insert case 4
The parent P is red and the root.
Because N is also red, requirement 3 is violated. But after switching P’s color the tree is in RB-shape.
The black height of the tree increases by 1.
Insert case 5
The parent P is red but the uncle U is black. The ultimate goal is to rotate the parent node P to the grandparent position, but this will not work if N is an "inner" grandchild of G (i.e., if N is the left child of the right child of G or the right child of the left child of G). A at P switches the roles of the current node N and its parent P. The rotation adds paths through N (those in the subtree labeled 2, see diagram) and removes paths through P (those in the subtree labeled 4). But both P and N are red, so
requirement 4 is preserved. Requirement 3 is restored in case 6.
Insert case 6
The current node N is now certain to be an "outer" grandchild of G (left of left child or right of right child). Now at G, putting P in place of G and making P the parent of N and G. G is black and its former child P is red, since
requirement 3 was violated. After switching the colors of P and G the resulting tree satisfies requirement 3.
Requirement 4 also remains satisfied, since all paths that went through the black G now go through the black P.
Because the algorithm transforms the input without using an auxiliary data structure and using only a small amount of extra storage space for auxiliary variables it is
in-place.
Removal
Simple cases
* When the deleted node has 2 children (non-NULL), then we can swap its value with its in-order successor (the leftmost child of the right subtree), and then delete the successor instead. Since the successor is leftmost, it can only have a right child (non-NULL) or no child at all.
* When the deleted node has only 1 child (non-NULL). In this case, just replace the node with its child, and color it black.
** The single child (non-NULL) must be red according to
conclusion 5, and the deleted node must be black according to
requirement 3.
* When the deleted node has no children (both NULL) and is the root, replace it with NULL. The tree is empty.
* When the deleted node has no children (both NULL), and is red, simply remove the leaf node.
* When the deleted node has no children (both NULL), and is black, deleting it will create an imbalance, and requires a rebalance, as covered in the next section.
Removal of a black non-root leaf
The complex case is when N is not the root, colored black and has no proper child (⇔ only NULL children).
In the first iteration, N is replaced by NULL.
void remove(struct Tree *tree, struct Node *node)
The rebalancing loop of the delete operation has the following
invariant:
* At the beginning of each iteration the black height of N equals the iteration number minus one, which means that in the first iteration it is zero and that N is a true black node

in higher iterations.
* The number of black nodes on the paths through N is one less than before the deletion, whereas it is unchanged on all other paths, so that there is a
black-violation at P if other paths exist.
* All other properties (including
requirement 3) are satisfied throughout the tree.
Notes to the delete diagrams
* In the diagrams below, P is used for N’s parent, S for the sibling of N, C (meaning ''close'' nephew) for S’s child in the same direction as N, and D (meaning ''distant'' nephew) for S’s other child (S cannot be a NULL node in the first iteration, because it must have black height one, which was the black height of N before its deletion, but C and D may be NULL nodes).
* The diagrams show the current node N as the left child of its parent P even though it is possible for N to be on either side. The code samples cover both possibilities by means of the side variable
dir
.
* At the beginning (in the first iteration) of removal, N is the NULL node replacing the node to be deleted. Because its location in parent’s node is the only thing of importance, it is symbolised by

(meaning: the current node N is a NULL node and left child) in the left column of the delete diagrams. As the operation proceeds also proper nodes (of black height ≥ 1) may become current (see e.g. case
2).
* By counting the black bullets (

and ) in a delete diagram it can be observed that the paths through N have one bullet less than the other paths. This means a black-violation at P—if it exists.
* The color constellation in column group ''before'' defines the case, whose name is given in the column ''case''. Thereby possible values in cells left empty are ignored.
* The rows in the synopsis are ordered such that the coverage of all possible RB cases is easily comprehensible.
* The column ''rotation'' indicates whether a rotation contributes to the rebalancing.
* The column ''assignment'' shows an assignment of N before entering a subsequent iteration step. This possibly induces a reassignment of the other nodes P, C, S, D also.
* If something has been changed by the case, this is shown in the column group ''after''.
* A sign in column ''next'' signifies that the rebalancing is complete with this step. If the column ''after'' determines exactly one case, this case is given as the subsequent one, otherwise there are question marks.
* The loop is where the problem of rebalancing is escalated
level higher in the tree in that the parent P becomes the new current node N. So it takes maximally iterations to repair the tree (where is the height of the tree). Because the probability of escalation decreases exponentially with each iteration the total rebalancing cost is constant on average, indeed
amortized constant. (Just as an aside: Mehlhorn & Sanders point out: "AVL trees do ''not'' support constant amortized update costs."
This is true for the rebalancing after a deletion, but not AVL insertion.)
* Out of the body of the loop there are exiting branches to the cases
3,
6,
5,
4, and
1; section
"Delete case 3" of its own has three different exiting branches to the cases 6, 5 and 4.
* Rotations occur in cases 6 and 5 + 6 and 3 + 5 + 6 – all outside the loop. Therefore, at most three rotations occur in total.
Delete case 1
The current node N is the new root. One black node has been removed from every path, so the RB-properties are preserved.
The black height of the tree decreases by 1.
Delete case 2
P, S, and S’s children are black. After painting S red all paths passing through S, which are precisely those paths ''not'' passing through N, have one less black node. Now all paths in the subtree rooted by P have the same number of black nodes, but one fewer than the paths that do not pass through P, so
requirement 4 may still be violated. After relabeling P to N the
loop invariant
In computer science, a loop invariant is a property of a program loop that is true before (and after) each iteration. It is a logical assertion, sometimes checked with a code assertion. Knowing its invariant(s) is essential in understanding th ...
is fulfilled so that the rebalancing can be iterated on one black level (= 1 tree level) higher.
Delete case 3
The sibling S is red, so P and the nephews C and D have to be black. A at P turns S into N’s grandparent.
Then after reversing the colors of P and S, the path through N is still short one black node. But N now has a red parent P and after the reassignment a black sibling S, so the transformations in cases 4, 5, or 6 are able to restore the RB-shape.
Delete case 4
The sibling S and S’s children are black, but P is red. Exchanging the colors of S and P does not affect the number of black nodes on paths going through S, but it does add one to the number of black nodes on paths going through N, making up for the deleted black node on those paths.
Delete case 5
The sibling S is black, S’s close child C is red, and S’s distant child D is black. After a at S the nephew C becomes S’s parent and N’s new sibling. The colors of S and C are exchanged.
All paths still have the same number of black nodes, but now N has a black sibling whose distant child is red, so the constellation is fit for case D6. Neither N nor its parent P are affected by this transformation, and P may be red or black ( in the diagram).
Delete case 6
The sibling S is black, S’s distant child D is red. After a at P the sibling S becomes the parent of P and S’s distant child D. The colors of P and S are exchanged, and D is made black. The whole subtree still has the same color at its root S, namely either red or black ( in the diagram), which refers to the same color both before and after the transformation. This way
requirement 3 is preserved. The paths in the subtree not passing through N (i.o.w. passing through D and node 3 in the diagram) pass through the same number of black nodes as before, but N now has one additional black ancestor: either P has become black, or it was black and S was added as a black grandparent. Thus, the paths passing through N pass through one additional black node, so that
requirement 4 is restored and the total tree is in RB-shape.
Because the algorithm transforms the input without using an auxiliary data structure and using only a small amount of extra storage space for auxiliary variables it is
in-place.
Proof of bounds
For
there is a red–black tree of height
with
:{,
, -
,
, , colspan=2,
, -
, rowspan=2, , , rowspan=2;style="vertical-align:bot",
, , style="vertical-align:top",
, , , , style="vertical-align:bot", if
even
, -
, style="vertical-align:top",
, , , , style="vertical-align:bot", if
odd
nodes (
is the
floor function
In mathematics, the floor function is the function that takes as input a real number , and gives as output the greatest integer less than or equal to , denoted or . Similarly, the ceiling function maps to the least integer greater than or eq ...
) and there is no red–black tree of this tree height with fewer nodes—therefore it is minimal.
Its black height is
(with black root) or for odd
(then with a red root) also
;Proof
For a red–black tree of a certain height to have minimal number of nodes, it must have exactly one longest path with maximal number of red nodes, to achieve a maximal tree height with a minimal black height. Besides this path all other nodes have to be black.
If a node is taken off this tree it either loses height or some RB property.
The RB tree of height
with red root is minimal. This is in agreement with
:
A minimal RB tree (RB
''h'' in figure 2) of height
has a root whose two child subtrees are of different height. The higher child subtree is also a minimal RB tree, containing also a longest path that defines its height it has
nodes and the black height
The other subtree is a
perfect binary tree of (black) height
having
black nodes—and no red node. Then the number of nodes is by
induction
{,
, -
, style="width:1.0em;", , ,
, ,
, ,
, , style="width:4em;text-align:center;",
, ,
, , style="width:4em;text-align:center;",
, ,
, -
, , , , , , , (higher subtree) , , , , (root) , , , , (second subtree)
, -
, colspan="8", resulting in
, -
, , ,
, ,
, ,
, , style="text-align:center;",
, , colspan="2" style="text-align:right;",
, -
, , , , ,
, , colspan="8",
■
The
graph of the function
is
convex
Convex or convexity may refer to:
Science and technology
* Convex lens, in optics
Mathematics
* Convex set, containing the whole line segment that joins points
** Convex polygon, a polygon which encloses a convex set of points
** Convex polytop ...
and
piecewise linear with breakpoints at
where
The function has been tabulated as
A027383(''h''–1) for
;Solving the function for
The inequality
leads to
, which for odd
leads to
:
.
So in both, the even and the odd case,
is in the interval
{,
, -
, style="width:1.0em;", , , style="text-align:right;",
, , style="width:8em;text-align:center",
, ,
, , style="width:12em;text-align:right",