Rope data structure
How a text editor can insert a word into the middle of a huge file without shuffling everything after it: keep the text as a tree of small pieces.
In 30 seconds
A rope stores a long text as many short pieces hanging from a binary tree, instead of one giant array. Each inner node remembers how many characters lie under its left branch.
Those counts let you jump to any position by walking down the tree. Inserting or deleting only cuts a piece or two and rearranges a few nodes; nothing else moves. Joining two texts needs one new node.
Reading one character is a bit slower than in an array, but editing a huge text stays fast, which is why some editors use it.
Rope data structure, step by step
The costs assume the tree is kept balanced; real ropes rebalance now and then, which can make a join cost O(log n). Real leaves hold hundreds of characters or more, not four.
- Step 1: One long array hurts. Most strings live in one unbroken array. Typing in the middle means sliding every later character along to make room. Here that is 15 moves; in a 100 MB file, a keystroke near the top would move about 100 million characters.
- Step 2: A tree of pieces. A rope cuts the text into short pieces and hangs them as the leaves of a binary tree; reading the leaves left to right gives the text back. Each inner node stores one number, its weight: how many characters sit under its left branch.
- Step 3: Find a character by weight. The weights steer a lookup. For character 12, the root says 10 characters lie to the left, so go right and look for character 2 there. The next node has weight 5, so go left; character 2 of “brown” is “o”. The work grows with the tree’s height, not the text’s length.
- Step 4: Join two ropes. To glue “_ran” on the end, make one new root: the old rope becomes its left child, the new piece its right, and its weight is the old length, 19. No character is copied or moved, so joining costs the same for ten characters or ten million.
- Step 5: Split a rope in two. Cutting at position 16 follows the same walk as a lookup: right at the root (16 − 10 = 6), right again (6 − 5 = 1), then cut the leaf “_fox” after its first character. Everything left of the cut stays as one rope; “fox” comes away as a second rope. Only the nodes on that one path are touched.
- Step 6: Insert and delete. Editing is just these two moves combined. To insert “red_” at 16, split there and join left + new + right. To delete “quick_”, split before and after it, drop the middle and join the ends. Each split or join walks one path of the tree, so the cost follows its height.
- Step 7: Why editors use ropes. A rope gives up a little speed on reading single characters and gains a great deal on editing. That is the right trade for a text editor holding a huge file under constant edits, and editors such as Helix and Zed store their buffers this way. For short strings a plain array is still simpler and faster.
Try it yourself
Learn this first
Spotted a mistake on this page?