This is about the advance data structure called rope/cord.
Size: 84.27 KB
Language: en
Added: May 28, 2015
Slides: 15 pages
Slide Content
Rope/Cord-Data structure By D.Darling Jemima, 14MZ31
Definition of Rope A rope or cord , is a data structure composed of smaller strings. It is used for efficiently storing and manipulating a very long string . A rope is a binary tree having leaf nodes that contain a short string. Each node has a weight value equal to the length of its string plus the sum of all leaf nodes weight in its left subtree .
A simple rope built on the string of " Hello_my_name_is_Jemima ". > 9 6 22 3 6 2 2 4 1 1 6 6 Hello_ my_ na m e_i s Jemima A B C D G E F I J K L H
Contd … A node with two children divides the whole string into two parts: the left subtree stores the first part of the string. The right subtree stores the second part and its weight is the sum of the left child's weight and the length of its contained string.
Operations in rope Index Split Concatenation Insert Delete
Index operation in ropes function index( RopeNode node, integer i ) if node.weight < i then return index( node.right , i - node.weight ) else if exists( node.left ) then return index( node.left , i ) else return node.string [ i ] endif endif end
Index(To find the character i =10) > 9 6 22 3 6 2 2 4 1 1 6 6 Hello_ my_ na m e_i s jemima A B C D E F I J K L G H
Concatenation 6 3 6 2 2 4 9 6 2 6 3 2 4 Hello_ my_ na m e_i Hello_ my_ na m e_i
Split 9 6 22 3 6 2 2 4 1 1 6 6 Hello_ my_ na m e_i s jemima
Split 9 6 22 3 6 2 2 4 1 1 6 6 Hello_ my_ na m e_i s jemima
Split 9 6 22 3 6 2 2 4 1 1 6 6 Hello_ my_ na m e_i s jemima 4
Advantages of ropes Ropes enable much faster insertion and deletion of text. Ropes don't require O(n) extra memory when operated upon (arrays need that for copying operations) Ropes don't require large contiguous memory spaces.
Application of ropes in text editor Hello, World! ^------Cursor here +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | H|e|l|l|o |,| |W| <free> | o|r|l|d |!| +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | ^ ^ | begin cur1 cur2 end