<s>
In	O
computer	B-General_Concept
programming	I-General_Concept
,	O
a	O
rope	O
,	O
or	O
cord	O
,	O
is	O
a	O
data	B-General_Concept
structure	I-General_Concept
composed	O
of	O
smaller	O
strings	O
that	O
is	O
used	O
to	O
efficiently	O
store	O
and	O
manipulate	O
a	O
very	O
long	O
string	O
.	O
</s>
<s>
For	O
example	O
,	O
a	O
text	B-Application
editing	I-Application
program	O
may	O
use	O
a	O
rope	O
to	O
represent	O
the	O
text	O
being	O
edited	O
,	O
so	O
that	O
operations	O
such	O
as	O
insertion	O
,	O
deletion	O
,	O
and	O
random	O
access	O
can	O
be	O
done	O
efficiently	O
.	O
</s>
<s>
A	O
rope	O
is	O
a	O
type	O
of	O
binary	O
tree	O
where	O
each	O
leaf	O
(	O
end	O
node	O
)	O
holds	O
a	O
string	O
and	O
a	O
length	O
(	O
also	O
known	O
as	O
a	O
"	O
weight	O
"	O
)	O
,	O
and	O
each	O
node	O
further	O
up	O
the	O
tree	O
holds	O
the	O
sum	O
of	O
the	O
lengths	O
of	O
all	O
the	O
leaves	O
in	O
its	O
left	O
subtree	B-Application
.	O
</s>
<s>
A	O
node	O
with	O
two	O
children	O
thus	O
divides	O
the	O
whole	O
string	O
into	O
two	O
parts	O
:	O
the	O
left	O
subtree	B-Application
stores	O
the	O
first	O
part	O
of	O
the	O
string	O
,	O
the	O
right	O
subtree	B-Application
stores	O
the	O
second	O
part	O
of	O
the	O
string	O
,	O
and	O
a	O
node	O
's	O
weight	O
is	O
the	O
length	O
of	O
the	O
first	O
part	O
.	O
</s>
<s>
For	O
rope	O
operations	O
,	O
the	O
strings	O
stored	O
in	O
nodes	O
are	O
assumed	O
to	O
be	O
constant	O
immutable	B-Application
objects	I-Application
in	O
the	O
typical	O
nondestructive	O
case	O
,	O
allowing	O
for	O
some	O
copy-on-write	B-Language
behavior	O
.	O
</s>
<s>
Leaf	B-Application
nodes	I-Application
are	O
usually	O
implemented	O
as	O
basic	O
fixed-length	O
strings	O
with	O
a	O
reference	B-General_Concept
count	I-General_Concept
attached	O
for	O
deallocation	O
when	O
no	O
longer	O
needed	O
,	O
although	O
other	O
garbage	B-General_Concept
collection	I-General_Concept
methods	O
can	O
be	O
used	O
as	O
well	O
.	O
</s>
<s>
Repeat	O
the	O
procedure	O
for	O
p	O
's	O
right	O
subtree	B-Application
.	O
</s>
<s>
return	O
new	O
Rope(Ropes.concat(lhs.fst.append(sequence )	O
,	O
lhs.snd	O
)	O
)	O
;	O
</s>
<s>
To	O
retrieve	O
the	O
i-th	O
character	O
,	O
we	O
begin	O
a	O
recursive	O
search	O
from	O
the	O
root	B-Application
node	I-Application
:	O
</s>
<s>
For	O
example	O
,	O
to	O
find	O
the	O
character	O
at	O
in	O
Figure	O
2.1	O
shown	O
on	O
the	O
right	O
,	O
start	O
at	O
the	O
root	B-Application
node	I-Application
(	O
A	O
)	O
,	O
find	O
that	O
22	O
is	O
greater	O
than	O
10	O
and	O
there	O
is	O
a	O
left	O
child	O
,	O
so	O
go	O
to	O
the	O
left	O
child	O
(	O
B	O
)	O
.	O
</s>
<s>
right|x200px|thumb|Figure	O
2.2	O
:	O
Concatenating	O
two	O
child	O
ropes	B-Data_Structure
into	O
a	O
single	O
rope	O
.	O
</s>
<s>
Definition	O
:	O
Concat(S1, S2 )	O
:	O
concatenate	O
two	O
ropes	B-Data_Structure
,	O
S1	O
and	O
S2	O
,	O
into	O
a	O
single	O
rope	O
.	O
</s>
<s>
A	O
concatenation	O
can	O
be	O
performed	O
simply	O
by	O
creating	O
a	O
new	O
root	B-Application
node	I-Application
with	O
and	O
,	O
which	O
is	O
constant	O
time	O
.	O
</s>
<s>
The	O
weight	O
of	O
the	O
parent	B-Application
node	I-Application
is	O
set	O
to	O
the	O
length	O
of	O
the	O
left	O
child	O
S1	O
,	O
which	O
would	O
take	O
time	O
,	O
if	O
the	O
tree	O
is	O
balanced	O
.	O
</s>
<s>
The	O
second	O
case	O
reduces	O
to	O
the	O
first	O
by	O
splitting	O
the	O
string	O
at	O
the	O
split	O
point	O
to	O
create	O
two	O
new	O
leaf	B-Application
nodes	I-Application
,	O
then	O
creating	O
a	O
new	O
node	O
that	O
is	O
the	O
parent	O
of	O
the	O
two	O
component	O
strings	O
.	O
</s>
<s>
For	O
example	O
,	O
to	O
split	O
the	O
22-character	O
rope	O
pictured	O
in	O
Figure	O
2.3	O
into	O
two	O
equal	O
component	O
ropes	B-Data_Structure
of	O
length	O
11	O
,	O
query	O
the	O
12th	O
character	O
to	O
locate	O
the	O
node	O
K	O
at	O
the	O
bottom	O
level	O
.	O
</s>
<s>
Remove	O
the	O
link	O
between	O
K	O
and	O
G	O
.	O
Go	O
to	O
the	O
parent	O
of	O
G	O
and	O
subtract	O
the	O
weight	O
of	O
K	O
from	O
the	O
weight	O
of	O
D	O
.	O
Travel	O
up	O
the	O
tree	O
and	O
remove	O
any	O
right	O
links	O
to	O
subtrees	B-Application
covering	O
characters	O
past	O
position	O
11	O
,	O
subtracting	O
the	O
weight	O
of	O
K	O
from	O
their	O
parent	B-Application
nodes	I-Application
(	O
only	O
node	O
D	O
and	O
A	O
,	O
in	O
this	O
case	O
)	O
.	O
</s>
<s>
Ropes	B-Data_Structure
enable	O
much	O
faster	O
insertion	O
and	O
deletion	O
of	O
text	O
than	O
monolithic	O
string	O
arrays	O
,	O
on	O
which	O
operations	O
have	O
time	O
complexity	O
O(n )	O
.	O
</s>
<s>
Ropes	B-Data_Structure
do	O
not	O
require	O
O(n )	O
extra	O
memory	O
when	O
operated	O
upon	O
(	O
arrays	O
need	O
that	O
for	O
copying	O
operations	O
)	O
.	O
</s>
<s>
Ropes	B-Data_Structure
do	O
not	O
require	O
large	O
contiguous	O
memory	O
spaces	O
.	O
</s>
<s>
If	O
only	O
nondestructive	O
versions	O
of	O
operations	O
are	O
used	O
,	O
rope	O
is	O
a	O
persistent	B-Application
data	I-Application
structure	I-Application
.	O
</s>
<s>
For	O
the	O
text	B-Application
editing	I-Application
program	O
example	O
,	O
this	O
leads	O
to	O
an	O
easy	O
support	O
for	O
multiple	B-Application
undo	I-Application
levels	O
.	O
</s>
<s>
Greater	O
overall	O
space	O
use	O
when	O
not	O
being	O
operated	O
on	O
,	O
mainly	O
to	O
store	O
parent	B-Application
nodes	I-Application
.	O
</s>
<s>
In	O
contrast	O
,	O
a	O
rope	O
data	B-General_Concept
structure	I-General_Concept
has	O
stable	O
performance	O
regardless	O
of	O
data	O
size	O
.	O
</s>
<s>
Further	O
,	O
the	O
space	O
complexity	O
for	O
ropes	B-Data_Structure
and	O
arrays	O
are	O
both	O
O(n )	O
.	O
</s>
<s>
In	O
summary	O
,	O
ropes	B-Data_Structure
are	O
preferable	O
when	O
the	O
data	O
is	O
large	O
and	O
modified	O
often	O
.	O
</s>
