Published by Dimitris Kougioumtzis · Updated
Use a left-leaning red-black tree: rotate red links, split temporary 4-nodes, and move red links downward before deletion. Repair colors on the way back up.
Represent a 2-3 search tree with left-leaning red links. Insertion repairs right-red links, consecutive red links and temporary 4-nodes. Deletion moves a red link down before removing a key, then restores the invariants upward.
When to use Red-black tree insertion and deletion
Balanced ordered sets with logarithmic insertion, deletion and lookup.
Choosing an alternative
This is the left-leaning variant, rather than the symmetric CLRS repair procedure. AVL trees instead enforce a height difference of at most one at each node.
Worked example: input and state changes
Example input
4,2,6,1,3,5,7 | 4,5
These checkpoints follow the actual example timeline. State details show the stored values and decisions; use the visualization to inspect every intervening step.
Step 0: Insert red leaf 4.
State at this checkpoint
{ "values": [ 4 ], "active": [ 0 ], "kind": "graph", "tree": true, "chosen": [], "visited": [], "colors": [ 1 ], "nodeLabels": [ "4 (red)" ] }Step 6: Make the root black.
State at this checkpoint
{ "values": [ 4, 2, 6 ], "active": [], "kind": "graph", "tree": true, "chosen": [], "visited": [], "colors": [ 0, 0, 0 ], "nodeLabels": [ "4 (black)", "2 (black)", "6 (black)" ] }Step 12: Insert red leaf 5.
State at this checkpoint
{ "values": [ 4, 2, 6, 1, 3, 5 ], "active": [ 5 ], "kind": "graph", "tree": true, "chosen": [], "visited": [], "colors": [ 0, 1, 0, 0, 0, 1 ], "nodeLabels": [ "4 (black)", "2 (red)", "6 (black)", "1 (black)", "3 (black)", "5 (red)" ] }Step 17: Make the root black.
State at this checkpoint
{ "values": [ 4, 2, 6, 1, 3, 5, 7 ], "active": [], "kind": "graph", "tree": true, "chosen": [], "visited": [], "colors": [ 0, 0, 0, 0, 0, 0, 0 ], "nodeLabels": [ "4 (black)", "2 (black)", "6 (black)", "1 (black)", "3 (black)", "5 (black)", "7 (black)" ] }Step 23: Delete 4 complete; 5 found.
State at this checkpoint
{ "values": [ 5, 2, 7, 1, 3, 6 ], "active": [], "kind": "graph", "tree": true, "chosen": [], "visited": [], "colors": [ 0, 1, 0, 0, 0, 1 ], "nodeLabels": [ "5 (black)", "2 (red)", "7 (black)", "1 (black)", "3 (black)", "6 (red)" ], "result": 1, "ordered": [ 1, 2, 3, 5, 6, 7 ] }
Expected final state
Delete 4 complete; 5 found.
Complexity and input bounds
Family: Trees. Time complexity: O(log n) per operation.
Implementation detail: The bounded view accepts twelve keys, then performs one deletion and search. Colors are also written as text in the 2D and 3D views. Downloads serialize inorder keys, node colors and the complete heap-position tree; black height includes the NIL leaf.
Space complexity: O(n) auxiliary storage for nodes and serialization; saved animation frames use additional space.
Working-space bounds describe algorithm state and any explicitly noted output, excluding the visualization's saved timeline. Recording frames can use more memory than running the function alone. Here n is sequence length, V and E are vertex and edge counts, and other parameters refer to the named input dimensions.
1..12 keys (0..99) | delete key,search key. Duplicate keys ignored; missing deletion is harmless.
The playground validates these limits before starting. Downloaded functions are callable implementations, with parameter and output-buffer contracts in their comments; they do not all repeat the playground's input validation.
Common mistakes and limitations
The root and NIL leaves are black. All root-to-NIL paths must have equal black height. A red node cannot have a red child, and final red links lean left. Skip a missing deletion safely.
Further reading
For background on algorithm analysis and the JavaScript function interface:
- MIT OpenCourseWare: algorithm design and analysis foundations
- MDN: JavaScript function parameters and return values
Editorial and testing approach · Browse all algorithm guides
Complete JavaScript implementation
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | // Left-leaning red-black insertion and top-down deletion, n 0..12 keys; keys/deleteKey/target 0..99. Duplicates ignored. Delete once, then return target membership. Output capacity at least 143: [0] remaining count; [1..count] inorder keys; [16] black height including NIL; [17..79] 63 heap-position keys (-1 absent); [80..142] colors (0 black, 1 red; absent colors 0). Empty black height is 1. Missing deletion is harmless. O(log n) per operation, O(n log n) build, O(n) auxiliary space. This is the left-leaning variant, not the symmetric CLRS repair procedure. Reference snippets assume validated playground inputs. Fixed scratch buffers have 256 slots. Sorting variants may use a different traversal strategy than the animation. function algorithm(a, n, deleteKey, target, output) { let key = Array(256).fill(0); let left = Array(256).fill(0); let right = Array(256).fill(0); let parent = Array(256).fill(0); let color = Array(256).fill(0); let stack = Array(256).fill(0); let position = Array(256).fill(0); let root = 0; let count = 0; let u = 0; let p = 0; let value = 0; let x = 0; let y = 0; let beta = 0; let minMode = 0; let successor = 0; let top = 0; let length = 0; let head = 0; let tail = 0; let found = 0; let blackHeight = 1; for (let i = 0; i < n; i++) { value = a[i]; u = root; p = 0; while (u != 0 && key[u] != value) { p = u; if (value < key[u]) { u = left[u]; } else { u = right[u]; } } if (u != 0) { continue; } u = ++count; key[u] = value; color[u] = 1; parent[u] = p; if (p == 0) { root = u; } else if (value < key[p]) { left[p] = u; } else { right[p] = u; } while (u != 0) { if (color[right[u]] != 0 && color[left[u]] == 0) { x = u; y = right[x]; beta = left[y]; p = parent[x]; right[x] = beta; left[y] = x; parent[y] = p; parent[x] = y; if (beta != 0) { parent[beta] = x; } if (p == 0) { root = y; } else if (left[p] == x) { left[p] = y; } else { right[p] = y; } color[y] = color[x]; color[x] = 1; u = y; } if (color[left[u]] != 0 && color[left[left[u]]] != 0) { x = u; y = left[x]; beta = right[y]; p = parent[x]; left[x] = beta; right[y] = x; parent[y] = p; parent[x] = y; if (beta != 0) { parent[beta] = x; } if (p == 0) { root = y; } else if (left[p] == x) { left[p] = y; } else { right[p] = y; } color[y] = color[x]; color[x] = 1; u = y; } if (color[left[u]] != 0 && color[right[u]] != 0) { color[u] = 1 - color[u]; if (left[u] != 0) { color[left[u]] = 1 - color[left[u]]; } if (right[u] != 0) { color[right[u]] = 1 - color[right[u]]; } } u = parent[u]; } color[root] = 0; } u = root; while (u != 0 && key[u] != deleteKey) { if (deleteKey < key[u]) { u = left[u]; } else { u = right[u]; } } if (u != 0) { if (color[left[root]] == 0 && color[right[root]] == 0) { color[root] = 1; } u = root; while (u != 0) { if (minMode != 0 || deleteKey < key[u]) { if (left[u] == 0) { p = parent[u]; if (p == 0) { root = 0; } else if (left[p] == u) { left[p] = 0; } else { right[p] = 0; } u = p; break; } if (color[left[u]] == 0 && color[left[left[u]]] == 0) { color[u] = 1 - color[u]; if (left[u] != 0) { color[left[u]] = 1 - color[left[u]]; } if (right[u] != 0) { color[right[u]] = 1 - color[right[u]]; } if (color[left[right[u]]] != 0) { x = right[u]; y = left[x]; beta = right[y]; p = parent[x]; left[x] = beta; right[y] = x; parent[y] = p; parent[x] = y; if (beta != 0) { parent[beta] = x; } if (p == 0) { root = y; } else if (left[p] == x) { left[p] = y; } else { right[p] = y; } color[y] = color[x]; color[x] = 1; x = u; y = right[x]; beta = left[y]; p = parent[x]; right[x] = beta; left[y] = x; parent[y] = p; parent[x] = y; if (beta != 0) { parent[beta] = x; } if (p == 0) { root = y; } else if (left[p] == x) { left[p] = y; } else { right[p] = y; } color[y] = color[x]; color[x] = 1; u = y; color[u] = 1 - color[u]; if (left[u] != 0) { color[left[u]] = 1 - color[left[u]]; } if (right[u] != 0) { color[right[u]] = 1 - color[right[u]]; } } } u = left[u]; } else { if (color[left[u]] != 0) { x = u; y = left[x]; beta = right[y]; p = parent[x]; left[x] = beta; right[y] = x; parent[y] = p; parent[x] = y; if (beta != 0) { parent[beta] = x; } if (p == 0) { root = y; } else if (left[p] == x) { left[p] = y; } else { right[p] = y; } color[y] = color[x]; color[x] = 1; u = y; } if (deleteKey == key[u] && right[u] == 0) { p = parent[u]; if (p == 0) { root = 0; } else if (left[p] == u) { left[p] = 0; } else { right[p] = 0; } u = p; break; } if (color[right[u]] == 0 && color[left[right[u]]] == 0) { color[u] = 1 - color[u]; if (left[u] != 0) { color[left[u]] = 1 - color[left[u]]; } if (right[u] != 0) { color[right[u]] = 1 - color[right[u]]; } if (color[left[left[u]]] != 0) { x = u; y = left[x]; beta = right[y]; p = parent[x]; left[x] = beta; right[y] = x; parent[y] = p; parent[x] = y; if (beta != 0) { parent[beta] = x; } if (p == 0) { root = y; } else if (left[p] == x) { left[p] = y; } else { right[p] = y; } color[y] = color[x]; color[x] = 1; u = y; color[u] = 1 - color[u]; if (left[u] != 0) { color[left[u]] = 1 - color[left[u]]; } if (right[u] != 0) { color[right[u]] = 1 - color[right[u]]; } } } if (deleteKey == key[u]) { successor = right[u]; while (left[successor] != 0) { successor = left[successor]; } key[u] = key[successor]; minMode = 1; } u = right[u]; } } while (u != 0) { if (color[right[u]] != 0 ) { x = u; y = right[x]; beta = left[y]; p = parent[x]; right[x] = beta; left[y] = x; parent[y] = p; parent[x] = y; if (beta != 0) { parent[beta] = x; } if (p == 0) { root = y; } else if (left[p] == x) { left[p] = y; } else { right[p] = y; } color[y] = color[x]; color[x] = 1; u = y; } if (color[left[u]] != 0 && color[left[left[u]]] != 0) { x = u; y = left[x]; beta = right[y]; p = parent[x]; left[x] = beta; right[y] = x; parent[y] = p; parent[x] = y; if (beta != 0) { parent[beta] = x; } if (p == 0) { root = y; } else if (left[p] == x) { left[p] = y; } else { right[p] = y; } color[y] = color[x]; color[x] = 1; u = y; } if (color[left[u]] != 0 && color[right[u]] != 0) { color[u] = 1 - color[u]; if (left[u] != 0) { color[left[u]] = 1 - color[left[u]]; } if (right[u] != 0) { color[right[u]] = 1 - color[right[u]]; } } u = parent[u]; } color[root] = 0; } u = root; while (u != 0 && key[u] != target) { if (target < key[u]) { u = left[u]; } else { u = right[u]; } } if (u != 0) { found = 1; } for (let i = 0; i < 143; i++) { output[i] = 0; } for (let i = 0; i < 63; i++) { output[17 + i] = -1; } u = root; while (u != 0) { if (color[u] == 0) { blackHeight++; } u = left[u]; } output[16] = blackHeight; u = root; while (u != 0 || top > 0) { while (u != 0) { stack[top++] = u; u = left[u]; } u = stack[--top]; output[++length] = key[u]; u = right[u]; } output[0] = length; if (root != 0) { stack[tail] = root; position[tail++] = 0; } while (head < tail) { u = stack[head]; p = position[head++]; output[17 + p] = key[u]; output[80 + p] = color[u]; if (left[u] != 0) { stack[tail] = left[u]; position[tail++] = 2 * p + 1; } if (right[u] != 0) { stack[tail] = right[u]; position[tail++] = 2 * p + 2; } } return found; } |
Source
Primary author source for the left-leaning red-black variant. The playground uses 2-3-tree insertion and top-down deletion; it does not present these as the symmetric CLRS repair cases.
- Left-leaning Red-Black Trees
Robert Sedgewick (2008). Author research paper/report.