One of the most common operations on strings is appending or concatenation. Appending to the end of a string when the string is stored in the traditional manner (i.e. an array of characters) would take a minimum of O(n) time (where n is the length of the original string).
We can reduce time taken by append using Ropes Data Structure.
A Rope is a binary tree structure where each node except the leaf nodes, contains the number of characters present to the left of that node. Leaf nodes contain the actual string broken into substrings (size of these substrings can be decided by the user).
Consider the image below.
The image shows how the string is stored in memory. Each leaf node contains substrings of the original string and all other nodes contain the number of characters present to the left of that node. The idea behind storing the number of characters to the left is to minimise the cost of finding the character present at i-th position.
1. Ropes drastically cut down the cost of appending two strings.
2. Unlike arrays, ropes do not require large contiguous memory allocations.
3. Ropes do not require O(n) additional memory to perform operations like insertion/deletion/searching.
4. In case a user wants to undo the last concatenation made, he can do so in O(1) time by just removing the root node of the tree.
1. The complexity of source code increases.
2. Greater chances of bugs.
3. Extra memory required to store parent nodes.
4. Time to access i-th character increases.
Now let’s look at a situation that explains why Ropes are a good substitute to monolithic string arrays.
Given two strings a and b. Concatenate them in a third string c.
Input : a = "This is ", b = "an apple" Output : "This is an apple" Input : a = "This is ", b = "geeksforgeeks" Output : "This is geeksforgeeks"
Method 1 (Naive method)
We create a string c to store concatenated string. We first traverse a and copy all characters of a to c. Then we copy all characters of b to c.
Hi This is geeksforgeeks. You are welcome here
Time complexity : O(n)
Now let’s try to solve the same problem using Ropes.
Method 2 (Rope structure method)
This rope structure can be utilized to concatenate two strings in constant time.
1. Create a new root node (that stores the root of the new concatenated string)
2. Mark the left child of this node, the root of the string that appears first.
3. Mark the right child of this node, the root of the string that appears second.
And that’s it. Since this method only requires to make a new node, it’s complexity is O(1).
Consider the image below (Image source : https://en.wikipedia.org/wiki/Rope_(data_structure))
Hi This is geeksforgeeks. You are welcome here.
Time Complexity: O(1)
This article is contributed by Akhil Goel. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Efficiently check if a string has all unique characters without using any additional data structure
- Efficiently find first repeated character in a string without using any additional data structure in one traversal
- Print Concatenation of Zig-Zag String in 'n' Rows
- Lexicographical concatenation of all substrings of a string
- Gap Buffer Data Structure
- Advantages of Trie Data Structure
- Tango Tree Data Structure
- Data Structure for Dictionary and Spell Checker?
- Trie Data Structure using smart pointer and OOP in C++
- Design an efficient data structure for given operations
- Dynamic Disjoint Set Data Structure for large range values
- Design a data structure that supports insert, delete, search and getRandom in constant time
- Concatenation of two strings in PHP
- Pairs whose concatenation contain all digits
- Number of pairs with Pandigital Concatenation