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 :
Definition: Create a stack
S and a list
L. Traverse down the left-most spine of the tree until reaching a leaf l', adding each node
n to
S. Add l' to
L. The parent of l' (
p) is at the top of the stack. Repeat the procedure for p's right subtree. package org.wikipedia.example; import java.util.ArrayDeque; import java.util.Deque; import java.util.Iterator; import jakarta.annotation.NonNull; class RopeLike { private RopeLike left; private RopeLike right; public RopeLike(RopeLike left, RopeLike right) { this.left = left; this.right = right; } public RopeLike getLeft() { return left; } public RopeLike getRight() { return right; } } public final class InOrderRopeIterator implements Iterator { private final Deque stack; public InOrderRopeIterator(@NonNull RopeLike root) { stack = new ArrayDeque<>(); RopeLike c = root; while (c != null) { stack.push(c); c = c.getLeft(); } } @Override public boolean hasNext() { return stack.size() > 0; } @Override public RopeLike next() { RopeLike result = stack.pop(); if (!stack.isEmpty()) { RopeLike parent = stack.pop(); RopeLike right = parent.getRight(); if (right != null) { stack.push(right); RopeLike cleft = right.getLeft(); while (cleft != null) { stack.push(cleft); cleft = cleft.getLeft(); } } } return result; } }
Rebalance :
Definition: Collect the set of leaves
L and rebuild the tree from the bottom-up. import java.util.List; static boolean isBalanced(RopeLike r) { int depth = r.depth(); if (depth >= FIBONACCI_SEQUENCE.length - 2) { return false; } return FIBONACCI_SEQUENCE[depth + 2] leaves = Ropes.collectLeaves(r); return merge(leaves, 0, leaves.size()); } return r; } static RopeLike merge(List leaves) { return merge(leaves, 0, leaves.size()); } static RopeLike merge(List leaves, int start, int end) { int range = end - start; switch (range) { case 1: return leaves.get(start); case 2: return new RopeLikeTree(leaves.get(start), leaves.get(start + 1)); default: int mid = start + (range / 2); return new RopeLikeTree(merge(leaves, start, mid), merge(leaves, mid, end)); } }
Insert :
Definition: Insert(i, S’): insert the string
S’ beginning at position
i in the string
s, to form a new string . :
Time complexity: . This operation can be done by a Split() and two Concat() operations. The cost is the sum of the three. import javafx.util.Pair; public Rope insert(int idx, CharSequence sequence) { if (idx == 0) { return prepend(sequence); } else if (idx == length()) { return append(sequence); } else { Pair lhs = base.split(idx); return new Rope(Ropes.concat(lhs.getKey().append(sequence), lhs.getValue())); } }
Index :
Definition: Index(i): return the character at position
i :
Time complexity: To retrieve the
i-th character, we begin a
recursive search from the root node: @Override public int indexOf(char ch, int startIndex) { if (startIndex > weight) { return right.indexOf(ch, startIndex - weight); } else { return left.indexOf(ch, startIndex); } } For example, to find the character at in Figure 2.1 shown on the right, start at the root node (A), find that 22 is greater than 10 and there is a left child, so go to the left child (B). 9 is less than 10, so subtract 9 from 10 (leaving ) and go to the right child (D). Then because 6 is greater than 1 and there's a left child, go to the left child (G). 2 is greater than 1 and there's a left child, so go to the left child again (J). Finally 2 is greater than 1 but there is no left child, so the character at index 1 of the short string "na" (ie "n") is the answer. (1-based index)
Concat :
Definition: Concat(S1, S2): concatenate two ropes,
S1 and
S2, into a single rope. :
Time complexity: (or time to compute the root weight) 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 :
Definition: Split (i, S): split the string
S into two new strings
S1 and
S2, and . :
Time complexity: There are two cases that must be dealt with: • The split point is at the end of a string (i.e. after the last character of a leaf node) • The split point is in the middle of a string. The second case reduces to the first by splitting the string at the split point to create two new leaf nodes, then creating a new node that is the parent of the two component strings. 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 (only node
D and
A, in this case). Finally, build up the newly orphaned nodes
K and
H by concatenating them 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 split(int index) { if (index split = left.split(index); return Pair.of(rebalance(split.getKey()), rebalance(new RopeLikeTree(split.getValue(), right))); } else if (index > weight) { Pair split = right.split(index - weight); return Pair.of(rebalance(new RopeLikeTree(left, split.getKey())), rebalance(split.getValue())); } else { return Pair.of(left, right); } }
Remove :
Definition: Remove(i, j): remove the substring , from
s to form a new string . :
Time complexity: . This operation can be done by two Split() 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(int start, int length) { Pair lhs = split(start); Pair rhs = split(start + length); return rebalance(new RopeLikeTree(lhs.getKey(), rhs.getValue())); }
Report :
Definition: Report(i, j): output the string . :
Time complexity: 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==