Red-Black Tree - Problem
Implement a Red-Black Tree data structure with the following operations:
insert(value)- Insert a value while maintaining RB-Tree propertiesdelete(value)- Delete a value while maintaining RB-Tree propertiessearch(value)- Search for a value in the tree
A Red-Black Tree must maintain these five properties:
- Every node is either red or black
- The root is always black
- All leaves (NIL nodes) are black
- Red nodes cannot have red children (no two red nodes are adjacent)
- Every path from root to leaf contains the same number of black nodes
Return the final tree structure as an array representation after all operations.
Input & Output
Example 1 — Basic Insert Operations
$
Input:
operations = [["insert",5],["insert",3],["insert",7],["search",3]]
›
Output:
[5,3,7]
💡 Note:
Insert 5 as root (black), insert 3 and 7 as red children. Tree maintains RB properties with root black and balanced structure.
Example 2 — Multiple Operations
$
Input:
operations = [["insert",10],["insert",5],["insert",15],["insert",3],["search",5]]
›
Output:
[10,5,15,3]
💡 Note:
Build a balanced RB-tree with multiple inserts. Colors are assigned to maintain all five RB-tree properties.
Example 3 — Single Node
$
Input:
operations = [["insert",42],["search",42]]
›
Output:
[42]
💡 Note:
Single node tree with root colored black, satisfying all RB-tree properties.
Constraints
- 1 ≤ operations.length ≤ 103
- operations[i] is one of ["insert", val], ["delete", val], or ["search", val]
- 1 ≤ val ≤ 104
- Tree maintains all five Red-Black properties
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code