Trees & Ordered Maps Hard
Array-Backed Binary Search Tree
Follow the source's binary search tree stored in four parallel ArrayLists, where child relationships are stable integer indices.
Trace input
Bounded JSONEdit the lesson input, then build a deterministic source-linked trace.
Source scenarios
Complexity
BestO(1) for a root hit
AverageO(h) per put or get
WorstO(n) per operation in a skewed tree
SpaceO(n) across keys, values, and two child-index lists
Array state
Follow the active comparisons and writes.
Current operation
Trace stateReady to explore
Source behavior & common mistakes
Trace put and get operations, including search direction, key replacement, leaf-link reservation, and the four ordered append writes for a new node.
- Array indices are node identities and remain stable as later entries append.
- A new leaf's parent link is set to keys.size() before addNode appends the node.
- addNode appends key, value, left NONE, and right NONE in that exact order.
- put replaces the value and returns false when the key already exists.
- get compares once for equality and again to choose the next child link.
Algorithm code
JavaArrayBST.javaStep linked
The highlighted line follows the current step.
Ready to begin
Press Play to see what happens.
Detailed execution
Snapshot 1 / 1
Comparisons 0