Documente Academic
Documente Profesional
Documente Cultură
Interface
[a,b,c,d, , , , ]
[a,b,c,d,e, , , ]
[a,b,c,d,e,f, , ]
[a,b,c,d,e,f,g,
[a,b,c,d,e,f,g,h]
which results in the string "abcdefgh".
Related Work
In the modern implementations of ropes, such
as C Cords or the SGI C++ implementation,
ropes are defined as balanced binary search
trees (Boehm et al., 1995, SGI, 1999). Depending on the programmers choice, they can be implemented using specific forms such as B Trees
or AVL Trees. Yet ropes may contain indistinct nodes and are thus more accurately defined as directed acyclic graphs (Boehm et al.,
1995). For an AVL Tree implementation, concatenation would take slower than constant time,
as AVL trees rebalance for each and every insertion, which is not the case in the general rope
class. However, the speed of indexing would increase as the tree would be guaranteed to have
an exact height of log n by definition. B Trees
would be helpful in situations in which portions
of the rope would have to be saved on the disk.
Since B Trees are optimized for RAM-to-disk
access, they would allow indexing and searching through the rope to be faster than normal
(Neubauer, 1999). However, B Trees also have
space concerns that can increase runtime overall (Semaphore). In general, the idea of the abstract data structure and the pros and cons of
implementation is one of the main components
of theoretical computer science.
Illustration
The following is a discussion of the construction of a rope, which showcases some of the data
structures unique qualities and benefits. Our
example string will be How grey is my dragon.
First of all, large input strings are traditionally
separated into small substrings for each node
of the tree by spaces. After that division is
complete, longer substrings can be divided again
(e.g. dragon has been split into two substrings,
dra and gon). One of the unique elements
of the general constructor for ropes is that the
rope is built from the bottom level (the leaves) of each parent node must be updated to express
upwards. These leaf nodes contain both values the sum of the values in its left subtree, see Figand data. The data are the small split substrings ure 3.
and the values are the int representations of the
number of characters in each substring (Boehm
et al., 1995). For an example, see Figure 1.
Once parent nodes have been randomly assigned, the nodes without parents are then connected to the leaf nodes immediately to their left
(Wikipedia, 2012). This then creates two different levels of nodes that contain data: leaf nodes
and nodes with a single right child. For the sake
of simplicity, any node that contains data will
be herein referred to as a leaf node. Because of
the addition of the parentless nodes, the values
Analysis
Indexing is a O(log n) method that showcases
rope tree traversal.
The pseudocode is as
follows,
algorithm is as follows:
int index(i):
current = root
while current != NULL:
if current.value > i:
current = current->left
else:
i = i - current.value
current = current->right
return current.data[i]
concatenate(r1, r2):
newroot = new Node(r1.root.value + r2.root.value)
newroot->left = r1
newroot->right = r2
example, seen in Figure 3, this producing a subrope of size 4. This subrope is then concatenated
to the rightmost node of the main rope and all
concatenation nodes affected have their values
updated (Boehm et al., 1995, SGI, 1999). The
following is the pseudocode for this operation,
are O(log n) because they function like modified indexing (O(1)) and may or many not contain concatenation (O(1)). Tree traversal methods, like iterating, are O(n) by the definition of
traversal.
The above methods and motivation for ropes
speak to its overall midlevel performance as a
data structure. Strings may have the extremes
of O(1) and O(n) operations, but ropes contain
mainly O(log n) operations. This ability to represent long strings, concatenate easily, and have
generally logarithmic operations is the strength
of the rope data structure.
contatenate(r1, n1):
lastnode = r1.pop_back()
newnode = new Node(lastnode.value + n1.value)
newnode->left = lastnode
lastnode->right = n1
r1.push_back(lastnode)
References
Hans J. Boehm, Russ Atkinson, and Michael Plass.
December 1995.
Ropes: An Alternative to
Strings Software-Practice and Experience, Volume 25. John Wiley & Sons, Ltd., Somerset, NJ.
Neubauer, P. (1999). B-trees: Balanced tree data
structures. Bluer White. Retrieved December 8,
2012, from http://www.bluerwhite.org/btree
Rope (computer science). 12 October 2012. In
Wikipedia. Retrieved December 7, 2012, from
http://en.wikipedia.org/wiki/Rope (computer science)
Semaphore Corporation.
When not to use btrees.
(n.d.).
Retrieved
December
8, 2012, from
The push_back() and pop_back() methods
http://www.semaphorecorp.com/btp/b11.html