Rope (data structure)
In computer programming, a rope, or cord, is a data structure composed of smaller strings that is used to efficiently store and manipulate longer strings or entire texts. For example, a text editing program may use a rope to represent the text being edited, so that operations such as insertion, deletion, and random access can be done efficiently.
Description
A rope is a type of binary tree where each leaf holds a string of manageable size and length, and each node further up the tree holds the sum of the lengths of all the leaves in its left subtree. A node with two children thus divides the whole string into two parts: the left subtree stores the first part of the string, the right subtree stores the second part of the string, and a node's weight is the length of the first part.For rope operations, the strings stored in nodes are assumed to be constant immutable objects in the typical nondestructive case, allowing for some copy-on-write behavior. Leaf nodes are usually implemented as basic fixed-length strings with a reference count attached for deallocation when no longer needed, although other garbage collection methods can be used as well.
Operations
In the following definitions, N is the length of the rope, that is, the weight of the root node. These examples are defined in the Java programming language.Collect leaves
package org.wikipedia.example;
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Iterator;
import jakarta.annotation.NonNull;
class RopeLike
public final class InOrderRopeIterator implements Iterator
Rebalance
import java.util.List;
static boolean isBalanced
static RopeLike rebalance
static RopeLike merge
static RopeLike merge
Insert
This operation can be done by aSplit and two Concat operations. The cost is the sum of the three.import javafx.util.Pair;
public Rope insert
Index
To retrieve the i-th character, we begin a recursive search from the root node:@Override
public int indexOf
For example, to find the character at in Figure 2.1 shown on the right, start at the root node, find that 22 is greater than 10 and there is a left child, so go to the left child. 9 is less than 10, so subtract 9 from 10 and go to the right child. Then because 6 is greater than 1 and there's a left child, go to the left child. 2 is greater than 1 and there's a left child, so go to the left child again. Finally 2 is greater than 1 but there is no left child, so the character at index 1 of the short string "na" is the answer.
Concat
A concatenation can be performed simply by creating a new root node with and, which is constant time. The weight of the parent node is set to the length of the left child S1, which would take time, if the tree is balanced.As most rope operations require balanced trees, the tree may need to be re-balanced after concatenation.
Split
There are two cases that must be dealt with:- The split point is at the end of a string
- The split point is in the middle of a string.
For example, to split the 22-character rope pictured in Figure 2.3 into two equal component ropes of length 11, query the 12th character to locate the node K at the bottom level. Remove the link between K and G. Go to the parent of G and subtract the weight of K from the weight of D. Travel up the tree and remove any right links to subtrees covering characters past position 11, subtracting the weight of K from their parent nodes. Finally, build up the newly orphaned nodes K and H by concatenating them together and creating a new parent P with weight equal to the length of the left node K.
As most rope operations require balanced trees, the tree may need to be re-balanced after splitting.
import javafx.util.Pair;
public Pair
Remove
This operation can be done by twoSplit and one Concat operation. First, split the rope in three, divided by i-th and i+j-th character respectively, which extracts the string to remove in a separate node. Then concatenate the other two nodes.import javafx.util.Pair;
@Override
public RopeLike remove
Report
To report the string, find the node u that contains Ci and, and then traverse T starting at node u. Output by doing an in-order traversal of T starting at node u.Comparison with monolithic arrays
Advantages:- Ropes enable much faster insertion and deletion of text than monolithic string arrays, on which operations have time complexity O.
- Ropes do not require O extra memory when operated upon.
- Ropes do not require large contiguous memory spaces.
- If only nondestructive versions of operations are used, rope is a persistent data structure. For the text editing program example, this leads to an easy support for multiple undo levels.
- Greater overall space use when not being operated on, mainly to store parent nodes. There is a trade-off between how much of the total memory is such overhead and how long pieces of data are being processed as strings. The strings in example figures above are unrealistically short for modern architectures. The overhead is always O, but the constant can be made arbitrarily small.
- Increase in time to manage the extra storage
- Increased complexity of source code; greater risk of bugs
| Operation | Rope | String |
| Index | ||
| Split | ||
| Concatenate | ||
| Iterate over each character | ||
| Insert | ||
| Append | ||
| Remove | ||
| Report | ||
| Build |