Rope and Tries Fast String Manipulation t r SoftUni Team Technical Trainers Software University http://softuni.bg i e e e Table of Contents Rope Prefix Tree (Trie) Suffix Tree 2 Have a Question? sli.do #DsAlgo 3 27 18 8 2 3 Exa mple_ of 5 _a_rope_ struc ture Rope Definition and Operations 4 Rope Rope == balanced tree for indexed items with fast insert / delete Allows fast string edit operations on very long strings Rope is a binary tree having leaf nodes Each node holds a short string Each node has a weight value equal to length of its string 5 Ropes in Practice: When to Use Rope? Ropes are efficient for very large strings E.g. length > 10 000 000 For small strings ropes are slower! List<T> and StringBuilder performs better for 100 000 chars Ropes provide: Faster insert / delete operations at random position – O(log(n)) Slower access by index position – O(log(n)) Arrays provide O(1) access by index 6 Create String: "Example of a rope." Example split: Exa _ are spaces 18 mple_ of Left subtree height _a_rope. 3 Exa 8 2 3 5 mple_ 2 of String length 8 _a_rope. 7 Concat First string: "Example of a rope" Second string: " concatenation" 31 17 8 9 2 3 Exa mple_ of 4 _a_rope _con caten ation 8 Indexing Index 12 17 12 12 7 14 5 3 Exa 4 mple _of_ 1 a_r ope 9 Split Splitting at index 12 17 14 7 3 Exa 4 mple _of_ a_r ope 10 Split (2) 13 4 7 3 Exa 4 mple _of_ 1 a_ r ope 11 Insert Insert "insert in a " at index 12 17 14 7 3 Exa 4 mple _of_ a_r ope 12 Insert (2) 13 8 7 3 Exa 4 mple _of_ 4 a_ inse 1 rt_i n_a_ r ope 13 Insert (3) 29 25 13 8 7 3 Exa 4 mple _of_ 4 a_ inse 1 rt_i n_a_ r ope 14 Delete 29 Index 13 25 Length 12 13 8 7 3 Exa 4 mple _of_ 4 a_ inse 1 rt_i n_a_ r ope 15 Delete (2) 13 8 7 3 Exa 4 mple _of_ 4 a_ inse 1 rt_i n_a_ r ope 16 Delete (3) 17 13 7 3 Exa 4 mple _of_ 1 a_ r ope 17 Rope (Wintellect BigList<T>) Live Demo t r i e Trie Definition and Operations 19 What is Trie? Trie (prefix tree) is an ordered tree data structure Special tree structure used for fast multi-pattern matching Used to store a dynamic set where the keys are usually strings Applications: Dictionaries Text searching Compression Trie - Example Empty root The There These r t Trie o h Tree p e Rope e r i r s r e e i e e e Their 21 Search These r t o h p e e r i r s r e e i e e e 22 Trie - Dictionary Each terminal node can have a value Value can be of type T g i Example of a name-age pairs o v Each set of keys constructs an unique trie s a h n 18 Dictionary<string, T> o 24 23 Construction Lucas - 30 l u c a s 30 24 Construction (2) Lucia - 18 l u c a i s 30 a 18 25 Construction (3) Luigi - 23 l u i c g a i i 23 s 30 a 18 26 Construction (4) Loren - 48 l u o i c r g a i e i 23 s 30 a 18 n 48 27 Construction (5) Logan - 23 l u i c g a i 23 s 30 o g r i a e a 18 n 23 n 48 28 Construction (6) Colin - 5 c l o u l i c i g a n 5 i 23 s 30 o g r i a e a 18 n 23 n 48 29 Construction (7) Coley - 15 c l o u l i c e i g a y 15 n 5 i 23 s 30 o g r i a e a 18 n 23 n 48 30 Construction (8) Cole - 32 c l o u l i c e e 32 i g a y 15 n 5 i 23 s 30 o g r i a e a 18 n 23 n 48 31 Trie - Performance Fast search by prefix High memory footprint Can be improved by remove the one-way chaining - creates radix trie Other fast operations Add Contains 32 Trie - Performance (2) Slow implementation using array of 256 elements 33 Trie - Performance (3) Fast implementation using Dictionary 34 Live Demo Trie Lab Exercise Trie - Add Resources http://ahmadsoft.org/source/xref/ropes1.2.5/src/org/ahmadsoft/ropes/ http://www.geeksforgeeks.org/trie-insert-and-search/ http://www.sanfoundry.com/java-program-implement-rope/ https://www.cs.bu.edu/teaching/c/tree/trie/ http://algs4.cs.princeton.edu/home/ Summary Ropes are very fast for large number of elements and provide fast insertion and deletion Tries are string search trees, which reduce the time to find a given pattern Different implementations - Radix Trie, Suffix Tree, Suffix Trie, etc. 38 Rope and Tries ? https://softuni.bg/opencourses/data-structures License This course (slides, examples, labs, videos, homework, etc.) is licensed under the "Creative Commons AttributionNonCommercial-ShareAlike 4.0 International" license Attribution: this work may contain portions from "Fundamentals of Computer Programming with C#" book by Svetlin Nakov & Co. under CC-BY-SA license "Data Structures and Algorithms" course by Telerik Academy under CC-BY-NC-SA license 40 Trainings @ Software University (SoftUni) Software University – High-Quality Education, Profession and Job for Software Developers softuni.bg Software University Foundation softuni.org Software University @ Facebook facebook.com/SoftwareUniversity Software University Forums forum.softuni.bg
© Copyright 2026 Paperzz