This documentation refers to the Red-Black Tree problem. Your solution class will automatically extend RBTSolution, and must implement the following methods:
Your script should evaluate to the prototype of your defined class.
The constructor receives the RBTree instance. Use it to store a reference to the tree for later use.
Called when the user requests an insertion. You should implement the full RB-INSERT algorithm including the fixup procedure.
Use this.tree.makeNode(key) to create a new node, then use the visualization methods below to animate each step.
Called when the user requests a deletion. You should implement the full RB-DELETE algorithm including the fixup procedure.
Use this.tree.search(key) to find the node to delete.
These methods are inherited from the base class. Call them from your algorithm to animate operations step by step. Each method auto-generates a descriptive message from its arguments. You can override or customize these messages using the annotation system.
Prefer the transaction helpers for rotations, transplant, and delete rewires. They keep the tree connected, move edges before nodes when appropriate, and produce more stable layouts for the visualizer.
Inserts a node into the tree using standard BST insertion (walks the tree to find the correct position). The insertion is animated in the visualizer.
Marks a node as removed from the visualization. In the current transaction model, this should be the point where the deleted node actually disappears from the rendered tree.
Changes the color of a node. Use "RED" or "BLACK" as the color value.
Animates a full left rotation as one coordinated transaction. The visualizer first rewires the affected edges, then relayouts the connected tree as a whole.
Animates a full right rotation as one coordinated transaction. This is the right-rotation counterpart to rotateLeftTransaction.
Animates transplant as a tree-connected replacement step.
Moves one child edge as a transaction-aware visual step. This is useful in delete/transplant flows where an edge should move coherently and some nodes must remain visible until a later removeNode call.
side must be "left" or "right". Pass affected nodes in carryNodes if they should remain visible through the transaction even after they stop being reachable from the root.
Sets the tree's root to the given node.
Highlights a node in the visualizer with a labeled pointer (e.g. "z", "x", "uncle"). The label appears above the node and is shown in the watch panel. Returns the node argument, so you can combine assignment and tracking in a single expression:
let w = this.trackPointer("w", x.parent.right);Removes a previously tracked pointer label.
Records a narrative step without performing any tree mutation. The message is pulled from the line annotation and evaluated with the optional ctx object merged into the template context. Pass local variables that should be available in the annotation template, e.g. this.logStep({key}).
Records a fixup case entry. The label (e.g. "Insert Case 1") is displayed in a color-coded badge in the top-right corner of the viewport. The badge tracks all cases encountered during the current operation and can be expanded to see the full history. Cases are cleared automatically when a new insert or delete begins.
Marks the operation as complete. Call this at the end of insert and delete. Like logStep, accepts an optional context object for the annotation template.
The annotation system lets you add descriptive messages and branch explanations to algorithm lines without cluttering the code. Messages are stored separately and can be edited interactively.
moveEdgeTransaction(parent, "left", child) produces "Move parent.left edge to child").if/else if/else lines define branch Q&A that is displayed automatically when execution enters that branch..annotations.json file.A small icon column appears to the right of line numbers in the code editor:
Annotation messages use JavaScript template literal syntax (${...}). The following variables are available:
parent, child for moveEdgeTransaction, or oldNode, newNode for transplantTransaction).z, x, uncle). These are RBNode objects, so you can access .key, .color, .isNil, etc.tree — the RBTree instance.NIL — the tree's NIL sentinel.logStep and done, any variables passed in the ctx argument.If a template expression throws an error, the raw template string is displayed as a fallback.
Branches are detected automatically from if/else if/else lines. When a visualization method fires inside a branch body and there is an annotation on the enclosing branch line with question and answer fields, a branch step is automatically inserted before the visualization step. This replaces the old this.branch() method.
Annotations are stored as content-anchored entries (keyed by function name and trimmed line content, not line numbers), making them resilient to code edits. User edits persist for the current session. Defaults can be restored via the "Reset" button in the popover.
The red-black tree instance. Accessed via this.tree in your solution.
The root node of the tree. Equals tree.NIL when the tree is empty.
The sentinel NIL node. All leaf pointers and the root's parent point to this node. Its color is always BLACK.
Creates a new node with the given key. The node starts with both children and parent set to NIL.
Searches for a node with the given key. Returns the node if found, or tree.NIL if not found.
Returns the node with the smallest key in the subtree rooted at the given node.
A node in the red-black tree.
The integer key stored in this node.
The color of this node: "RED" or "BLACK".
The left child. Equals tree.NIL if there is no left child.
The right child. Equals tree.NIL if there is no right child.
The parent node. Equals tree.NIL for the root node.
Returns true if this node is the NIL sentinel.