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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Longest palindromic string formed by concatenation of prefix and suffix of a string
- Check if given string is a substring of string formed by repeated concatenation of z to a
- Check if a string is concatenation of another given string
- Karatsuba algorithm for fast multiplication using Divide and Conquer algorithm
- Lexicographical concatenation of all substrings of a string
- Longest palindromic String formed using concatenation of given strings in any order
- Minimum flips required to convert given string into concatenation of equal substrings of length K
- Count of pairs of strings whose concatenation forms a palindromic string
- Print Concatenation of Zig-Zag String in 'n' Rows
- Minimize cost to convert given string into concatenation of equal substrings of length K
- 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
- Word formation using concatenation of two dictionary words
- Concatenation of two strings in PHP
- Count of three non-overlapping sub-strings which on concatenation forms a palindrome
- Find the number obtained after concatenation of binary representation of M and N
- Caesar Concatenation
- Count pair of strings whose concatenation has every vowel
- Pairs whose concatenation contain all digits
- Count pair of strings whose concatenation of substrings form a palindrome