Presentation is loading. Please wait.

Presentation is loading. Please wait.

Fast String Manipulation

Similar presentations


Presentation on theme: "Fast String Manipulation"— Presentation transcript:

1 Fast String Manipulation
Rope and Tries Fast String Manipulation t Ropes Tries r SoftUni Team i e Technical Trainers Software University e e © Software University Foundation – This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

2 Table of Contents Rope Prefix Tree (Trie) Suffix Tree
© Software University Foundation – This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

3 Have a Question? sli.do #DsAlgo

4 Definition and Operations
27 18 8 3 2 5 Exa mple_ of _a_rope_ struc ture Rope Definition and Operations

5 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

6 Ropes in Practice: When to Use Rope?
Ropes are efficient for very large strings E.g. length > For small strings ropes are slower! List<T> and StringBuilder performs better for 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

7 Create String: "Example of a rope." Example split: Exa mple_ of
_ are spaces 18 8 Left subtree height 3 2 String length 3 Exa 5 mple_ 2 of 8 _a_rope.

8 Concat First string: "Example of a rope"
Second string: " concatenation" 31 17 8 9 3 2 4 _a_rope Exa mple_ of _con caten ation

9 Indexing Index 12 17 12 14 12 7 5 3 4 1 Exa mple _of_ a_r ope

10 Split Splitting at index 12 17 14 7 3 4 Exa mple _of_ a_r ope

11 Split (2) 13 4 7 3 4 1 Exa mple _of_ a_ r ope

12 Insert Insert "insert in a " at index 12 17 14 7 3 4 Exa mple _of_ a_r
ope

13 Insert (2) 13 7 8 3 4 4 1 Exa mple _of_ a_ inse rt_i n_a_ r ope

14 Insert (3) 29 25 13 7 8 3 4 4 1 Exa mple _of_ a_ inse rt_i n_a_ r ope

15 Delete Index 13 Length 12 29 25 13 7 8 3 4 4 1 Exa mple _of_ a_ inse
rt_i n_a_ r ope

16 Delete (2) 13 7 8 3 4 4 1 Exa mple _of_ a_ inse rt_i n_a_ r ope

17 Delete (3) 17 13 7 3 4 1 Exa mple _of_ a_ r ope

18 Rope (Wintellect BigList<T>)
Live Demo

19 Definition and Operations
Trie Definition and Operations

20 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

21 Trie - Example The There These Trie Tree Rope Their Empty root r t o h

22 Search These r t o h r p e i e e i r s e e r e e

23 Trie - Dictionary Each terminal node can have a value
Value can be of type T Dictionary<string, T> Example of a name-age pairs Each set of keys constructs an unique trie g i o v s a h n 18 o 24

24 Construction Lucas - 30 l u c a s 30

25 Construction (2) Lucia - 18 l u c a i s 30 a 18

26 Construction (3) Luigi - 23 l u i c g a i i 23 s 30 a 18

27 Construction (4) Loren - 48 l u o i c r g a i e i 23 s 30 a 18 n 48

28 Construction (5) Logan - 23 l u o i c g r g a i a e i 23 s 30 a 18 n
48

29 Construction (6) Colin - 5 c l o u o l i c g r i g a i a e n 5 i 23 s
30 a 18 n 23 n 48

30 Construction (7) Coley - 15 c l o u o l i c g r e i g a i a e y 15 n 5
23 s 30 a 18 n 23 n 48

31 Construction (8) Cole - 32 c l o u o l i c g r e e 32 i g a i a e y 15
23 s 30 a 18 n 23 n 48

32 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

33 Trie - Performance (2) Slow implementation using array of 256 elements

34 Trie - Performance (3) Fast implementation using Dictionary

35 Live Demo Trie

36 Lab Exercise Trie - Add

37 Resources /src/org/ahmadsoft/ropes/

38 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.

39 Rope and Tries https://softuni.bg/opencourses/data-structures
© Software University Foundation – This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

40 License This course (slides, examples, labs, videos, homework, etc.) is licensed under the "Creative Commons Attribution- NonCommercial-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 © Software University Foundation – This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

41 Trainings @ Software University (SoftUni)
Software University – High-Quality Education, Profession and Job for Software Developers softuni.bg Software University Foundation softuni.org Software Facebook facebook.com/SoftwareUniversity Software University Forums forum.softuni.bg © Software University Foundation – This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.


Download ppt "Fast String Manipulation"

Similar presentations


Ads by Google