encyclopegia.org

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.[src] 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.[src]

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.[src] 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.[src] ==Description== A rope is a type of binary tree where each leaf (end node) holds a string of manageable size and length (also known as a weight), and each node further up the tree holds the sum of the lengths of all the leaves in its left subtree.[src] 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.[src] 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.[src] 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.[src] ==Operations== In the following definitions, N is the length of the rope, that is, the weight of the root node.[src] These examples are defined in the Java programming language.[src] 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; } } 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)); } } 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())); } } @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).[src] 9 is less than 10, so subtract 9 from 10 (leaving ) and go to the right child (D).[src] Then because 6 is greater than 1 and there's a left child, go to the left child (G).[src] 2 is greater than 1 and there's a left child, so go to the left child again (J).[src] 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.[src] (1-based index) 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); } } 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())); } ==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(n).[src] Ropes do not require O(n) extra memory when operated upon (which arrays need for copying operations).[src] Ropes do not require large contiguous memory spaces.[src] If only nondestructive versions of operations are used, rope is a persistent data structure.[src] For the text editing program example, this leads to an easy support for multiple undo levels.[src] Disadvantages: Greater overall space use when not being operated on, mainly to store parent nodes.[src] 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.[src] The strings in example figures above are unrealistically short for modern architectures.[src] The overhead is always O(n), but the constant can be made arbitrarily small.[src] Increase in time to manage the extra storage Increased complexity of source code; greater risk of bugs This table compares the algorithmic traits of string and rope implementations, not their raw speed.[src] Array-based strings have smaller overhead, so (for example) concatenation and split operations are faster on small datasets.[src] However, when array-based strings are used for longer strings, time complexity and memory use for inserting and deleting characters becomes unacceptably large.[src] In contrast, a rope data structure has stable performance regardless of data size.[src] Further, the space complexity for ropes and arrays are both O(n).[src] In summary, ropes are preferable when the data is large and modified often.[src] ==See also== The Cedar programming environment, which used ropes "almost since its inception" The Model T enfilade, a similar data structure from the early 1970s Gap buffer, a data structure commonly used in text editors that allows efficient insertion and deletion operations clustered near the same location Piece table, another data structure commonly used in text editors ==References== ==External links== "absl::Cord" implementation of ropes within The Abseil library "C cords" implementation of ropes within the Boehm Garbage Collector library SGI C++ specification for ropes (supported by STLPort and libstdc++) Ropes for C# ropes for Common Lisp Ropes for Java String-Like Ropes for Java Ropes for JavaScript Ropes for Limbo ropes for Nim Ropes for OCaml pyropes for Python Ropes for Smalltalk SwiftRope for Swift "Ropey" for Rust Rope for Dart Rope & SumTree in Zed Editor[src]

31 sentences · 31 cited inline · 0 references stored

References

No reference rows published for this article.

Cite this article

MLA

"Rope (data structure)." encyclopegia.org, 2026, https://encyclopegia.org/wiki/rope-data-structure. Accessed 28 November 2026.

Chicago

"Rope (data structure)." encyclopegia.org. Accessed November 28, 2026. https://encyclopegia.org/wiki/rope-data-structure.

APA

encyclopegia.org. (2026). Rope (data structure). Retrieved November 28, 2026, from https://encyclopegia.org/wiki/rope-data-structure

Accessed November 28, 2026 · CC BY-SA 4.0 · citation page · JSON

Related articles

tooth supporting structure

stub · references available

rope bridge

stub · references available

digital data

stub · references available

Enhanced Data Rates for GSM Evolution

stub · references available

Data Becker

stub · references available

Polity data series

stub · references available

Ribboned rope squirrel

stub · references available

Kintampo rope squirrel

stub · references available
CC BY-SA 4.0 · share-alike · https://en.wikipedia.org/w/index.php?oldid=1372318141 · 0.5 · snapshot enwiki-001372318141

Cite · JSON · Search

Related services and records

Services

  • Estate — measured countries, lanes and facts
  • Intel — live corroborated events
  • Legal — statutes, cases and citations
  • Encyclopedia — articles with inline sources
  • Space — launches, satellites and orbits
  • Search — federated search and reports
  • Pass — passport account and SSO
  • Corpora — read-only corpus origins
  • e01 — self-hosted corpora and tools
  • Careers — open roles: frontier engineers and sales partners

Text and metadata only; each target carries its own sources. Missing data is published as no-data, never estimated.