SlideShare a Scribd company logo
Tries
What is a TRIE?
• Tree based data structure used for
Information ReTRIEval task
• Also called as Digital Tree, Prefix tree, Radix
Tree
• Trie is used mostly for storing strings in
a compact way. E.g. words in dictionary
• A tries supports pattern matching queries in
time proportional to the pattern size
Trie – An ordered Tree
• S= { bear, bell, bid, bull, buy, sell, stock, stop}
Tries 4
Standard Tries
• The standard trie for a set of strings S is an ordered tree such that:
– Each node but the root is labeled with a character
– The children of a node are alphabetically ordered
– The paths from the external nodes to the root yield the strings of
S
• Example: standard trie for the set of strings
S = { bear, bell, bid, bull, buy, sell, stock, stop }
a
e
b
r
l
l
s
u
l
l
y
e t
l
l
o
c
k
p
i
d
TRIE Representation
struct Trie
{
struct Trie* S[26]; //a-z or A-Z
bool isEndOfWord;
};
Tries for number
• Name | Social Security Number (SS#)
Jack | 951-94-1654
Jill | 562-44-2169
Bill | 271-16-3624
Kathy | 278-49-1515
April | 951-23-7625
Bill | 271-16-3624
Kathy | 278-49-1515
8
Applications of Tries
• A standard trie supports the following operations on a
preprocessed text in time O(m), where m = |X|
-word matching:
find the first occurrence of word X in the text
-prefix matching:
find the first occurrence of the longest prefix of word X in
the text
• Each operation is performed by tracing a path in the trie starting
at the root
Tries 9
Word Matching with a Trie
• We insert the
words of the
text into a trie
• Each leaf
stores the
occurrences of
the associated
word in the
text
s e e b e a r ? s e l l s t o c k !
s e e b u l l ? b u y s t o c k !
b i d s t o c k !
a
a
h e t h e b e l l ? s t o p !
b i d s t o c k !
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
a r
87 88
a
e
b
l
s
u
l
e t
e
0, 24
o
c
i
l
r
6
l
78
d
47, 58
l
30
y
36
l
12
k
17, 40,
51, 62
p
84
h
e
r
69
a
Prefix : What is prefix:
• The prefix of a string is nothing but
any n letters n≤|S| that can be considered
beginning strictly from the starting of a string.
• For example , the word “abacaba” has the
following prefixes:
a
ab
aba
abac
abaca
abacab
Common Prefix
Tries data structures
Tries data structures
Use of Prefix in IP routing
Suffix
• The suffix of a string is nothing but
any n letters n≤|S| that can be considered ending
strictly at the end of a string.
• For example , the word “abacaba” has the
following prefixes:
a
ba
aba
caba
acaba
bacaba
Operations
• Insert – top-down traversal
• Delete – bottom-up
• Search – top-down
Tries 17
Analysis of Standard Tries
• A standard trie uses O(n) space and supports searches,
insertions and deletions in time O(dm), where:
n total size of the strings in S
m size of the string parameter of the operation
d size of the alphabet
a
e
b
r
l
l
s
u
l
l
y
e t
l
l
o
c
k
p
i
d
Tries data structures
Insert Word into TRIE
Insert “their”
Pseudo code for Insertion of a string into a
TRIE
void insert(String s)
{
for(every char in string s)
{
if(child node belonging to current char is null)
{
child node=new Node();
}
current_node=child_node;
}
}
Search
• To search a trie for an element with a given
key,
– we start at the root and follow a path down the
trie until we either fall off the trie (i.e., we follow
a null pointer in a branch node)
or
– we reach an element node; The path we follow is
determined by the alphabets/digits of the search
key.
Is string NEWS is present?
Pseudo code to check whether a single word
exists in a TRIE
boolean check(String s)
{
for(every char in String s)
{
if(child node of current char is null)
{
return false;
}
}
return true;
}
Tries data structures
Check node has any children, before
deleting that node
//Node T has children or not
boolean haveChildren(struct Trie * T)
{
for(every child node of T)
{
if(child node belonging to current char is not null)
{
return True;
}
}
return False;
}
Algorithm requirements for deleting key 'k':
1. If key 'k' is not present in trie, then just return.
2. If key 'k' is not a prefix nor a suffix of any other key and
nodes of key 'k' are not part of any other key then all the
nodes starting from root node(excluding root node) to leaf
node of key 'k' should be deleted.
Delete key
“word”
Algorithm requirements for deleting key 'k': contd…
3. If key 'k' is a prefix of some other key, then leaf node
corresponding to key 'k' should be marked as 'not a leaf node'.
No node should be deleted in this case.
delete key - "xyz", then
without deleting any
node we have to simply
mark node 'z' as 'not a
leaf node' and change its
value to "NON_VALUE"
4. If key 'k' is a suffix of some other key 'k1', then all nodes of
key 'k' which are not part of key 'k1' should be deleted.
delete key - "xyzb", then we should only delete node
"b" of key "xyzb" since other nodes of this key are also
part of key "xyz".
Algorithm requirements for deleting key 'k': (contd..)
5. If key 'k' is not a prefix nor a suffix of any other key but
some nodes of key 'k' are shared with some other key 'k1',
then nodes of key 'k' which are not common to any other key
should be deleted and shared nodes should be kept intact.
delete key "abc" which
shares node 'a', node 'b'
with key "abb", then the
algorithm should delete
only node 'c' of key "abc"
and should not delete
node 'a' and node 'b'.
Delete “their”
31
Compressed Tries
• Trie with nodes of degree at least 2
• Obtained from standard trie by compressing chains of redundant
nodes
Compressed Trie:
Standard Trie:
Tries 32
Compressed Tries
e
b
ar ll
s
u
ll y
ell to
ck p
id
a
e
b
r
l
l
s
u
l
l
y
e t
l
l
o
c
k
p
i
d
Draw a compressed Trie for
S= {romane, romanus, romulus, rubens, ruber, rubicon,
rubicundus}
Draw a compressed Trie for
S= {romane, romanus, romulus, rubens, ruber, rubicon,
rubicundus}
Tries 35
Suffix Trie
• The suffix trie of a string X is the compressed trie of all the
suffixes of X ( i.e. the one defined over a set of
substrings of a string X )
e nimize
nimize ze
zei mi
mize nimize ze
m i n i z em i
0 1 2 3 4 5 6 7

More Related Content

What's hot (20)

PPTX
THREADED BINARY TREE AND BINARY SEARCH TREE
Siddhi Shrivas
 
PDF
List , tuples, dictionaries and regular expressions in python
channa basava
 
PPTX
Circular queue
Lovely Professional University
 
PPT
Binary search tree(bst)
Hossain Md Shakhawat
 
PPT
B trees in Data Structure
Anuj Modi
 
PPT
Trees
noraidawati
 
PPSX
Data Structure (Queue)
Adam Mukharil Bachtiar
 
PPTX
B+ trees and height balance tree
Jasleen Kaur (Chandigarh University)
 
PPT
BINARY TREE REPRESENTATION.ppt
SeethaDinesh
 
PPTX
Bfs and Dfs
Masud Parvaze
 
PDF
Trees, Binary Search Tree, AVL Tree in Data Structures
Gurukul Kangri Vishwavidyalaya - Faculty of Engineering and Technology
 
PPTX
Performance analysis and randamized agoritham
lilyMalar1
 
PPTX
Binary Heap Tree, Data Structure
Anand Ingle
 
PPT
Two dimensional array
Rajendran
 
PPT
Breadth first search and depth first search
Hossain Md Shakhawat
 
PPT
Heaps
Hafiz Atif Amin
 
PDF
Heap and heapsort
Amit Kumar Rathi
 
PPTX
Python strings presentation
VedaGayathri1
 
THREADED BINARY TREE AND BINARY SEARCH TREE
Siddhi Shrivas
 
List , tuples, dictionaries and regular expressions in python
channa basava
 
Binary search tree(bst)
Hossain Md Shakhawat
 
B trees in Data Structure
Anuj Modi
 
Data Structure (Queue)
Adam Mukharil Bachtiar
 
B+ trees and height balance tree
Jasleen Kaur (Chandigarh University)
 
BINARY TREE REPRESENTATION.ppt
SeethaDinesh
 
Bfs and Dfs
Masud Parvaze
 
Trees, Binary Search Tree, AVL Tree in Data Structures
Gurukul Kangri Vishwavidyalaya - Faculty of Engineering and Technology
 
Performance analysis and randamized agoritham
lilyMalar1
 
Binary Heap Tree, Data Structure
Anand Ingle
 
Two dimensional array
Rajendran
 
Breadth first search and depth first search
Hossain Md Shakhawat
 
Heap and heapsort
Amit Kumar Rathi
 
Python strings presentation
VedaGayathri1
 

Similar to Tries data structures (20)

PPTX
TRIES_data_structure
ddewithaman10
 
PPTX
Application of tries
Tech_MX
 
PPTX
Shishirppt
Shishir Singhal
 
PPTX
Trie (1)
shrikrishnaw
 
PDF
data structure and algorithm notes - tries
muskhan3709
 
PPTX
Tries
Shubham Shukla
 
PPT
Tries in data structures using C presentation
Laxmi139487
 
PPT
Tries .ppt
SumitKumar363528
 
PPTX
Trie Logic.pptx for data structures and algorithms
NikitaBiradar13
 
PPT
Lec18
Nikhil Chilwant
 
PDF
'Trie' Data Structure for Auto Search Complete
Sonil Kumar
 
PPTX
presentation on important DAG,TRIE,Hashing.pptx
jainaaru59
 
PPT
4888009.pptnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn
RAtna29
 
PPTX
Trie Data Structure
Badiuzzaman Pranto
 
PDF
Souvenir's Booth - Algorithm Design and Analysis Project Project Report
Akshit Arora
 
PPTX
Suffix Tree and Suffix Array
Harshit Agarwal
 
PDF
Souvenir's Booth - Algorithm Design and Analysis Project Presentation
Akshit Arora
 
PPTX
TRIES_PATRICIA.pptxnnnnnnnnnnnnnnnnnnnnnnnnn
RATNANITINPATIL
 
TRIES_data_structure
ddewithaman10
 
Application of tries
Tech_MX
 
Shishirppt
Shishir Singhal
 
Trie (1)
shrikrishnaw
 
data structure and algorithm notes - tries
muskhan3709
 
Tries in data structures using C presentation
Laxmi139487
 
Tries .ppt
SumitKumar363528
 
Trie Logic.pptx for data structures and algorithms
NikitaBiradar13
 
'Trie' Data Structure for Auto Search Complete
Sonil Kumar
 
presentation on important DAG,TRIE,Hashing.pptx
jainaaru59
 
4888009.pptnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn
RAtna29
 
Trie Data Structure
Badiuzzaman Pranto
 
Souvenir's Booth - Algorithm Design and Analysis Project Project Report
Akshit Arora
 
Suffix Tree and Suffix Array
Harshit Agarwal
 
Souvenir's Booth - Algorithm Design and Analysis Project Presentation
Akshit Arora
 
TRIES_PATRICIA.pptxnnnnnnnnnnnnnnnnnnnnnnnnn
RATNANITINPATIL
 
Ad

Recently uploaded (20)

PDF
Advanced LangChain & RAG: Building a Financial AI Assistant with Real-Time Data
Soufiane Sejjari
 
PDF
STUDY OF NOVEL CHANNEL MATERIALS USING III-V COMPOUNDS WITH VARIOUS GATE DIEL...
ijoejnl
 
PPTX
Chapter_Seven_Construction_Reliability_Elective_III_Msc CM
SubashKumarBhattarai
 
PPTX
Water resources Engineering GIS KRT.pptx
Krunal Thanki
 
PPTX
sunil mishra pptmmmmmmmmmmmmmmmmmmmmmmmmm
singhamit111
 
PPTX
IoT_Smart_Agriculture_Presentations.pptx
poojakumari696707
 
PDF
Biodegradable Plastics: Innovations and Market Potential (www.kiu.ac.ug)
publication11
 
PPTX
MSME 4.0 Template idea hackathon pdf to understand
alaudeenaarish
 
PDF
2025 Laurence Sigler - Advancing Decision Support. Content Management Ecommer...
Francisco Javier Mora Serrano
 
PPTX
ENSA_Module_7.pptx_wide_area_network_concepts
RanaMukherjee24
 
DOCX
SAR - EEEfdfdsdasdsdasdasdasdasdasdasdasda.docx
Kanimozhi676285
 
PDF
SG1-ALM-MS-EL-30-0008 (00) MS - Isolators and disconnecting switches.pdf
djiceramil
 
PPTX
Introduction to Fluid and Thermal Engineering
Avesahemad Husainy
 
PPTX
Inventory management chapter in automation and robotics.
atisht0104
 
PDF
Jual GPS Geodetik CHCNAV i93 IMU-RTK Lanjutan dengan Survei Visual
Budi Minds
 
PPTX
22PCOAM21 Session 1 Data Management.pptx
Guru Nanak Technical Institutions
 
PDF
Natural_Language_processing_Unit_I_notes.pdf
sanguleumeshit
 
PDF
Zero Carbon Building Performance standard
BassemOsman1
 
PPTX
business incubation centre aaaaaaaaaaaaaa
hodeeesite4
 
PDF
勉強会資料_An Image is Worth More Than 16x16 Patches
NABLAS株式会社
 
Advanced LangChain & RAG: Building a Financial AI Assistant with Real-Time Data
Soufiane Sejjari
 
STUDY OF NOVEL CHANNEL MATERIALS USING III-V COMPOUNDS WITH VARIOUS GATE DIEL...
ijoejnl
 
Chapter_Seven_Construction_Reliability_Elective_III_Msc CM
SubashKumarBhattarai
 
Water resources Engineering GIS KRT.pptx
Krunal Thanki
 
sunil mishra pptmmmmmmmmmmmmmmmmmmmmmmmmm
singhamit111
 
IoT_Smart_Agriculture_Presentations.pptx
poojakumari696707
 
Biodegradable Plastics: Innovations and Market Potential (www.kiu.ac.ug)
publication11
 
MSME 4.0 Template idea hackathon pdf to understand
alaudeenaarish
 
2025 Laurence Sigler - Advancing Decision Support. Content Management Ecommer...
Francisco Javier Mora Serrano
 
ENSA_Module_7.pptx_wide_area_network_concepts
RanaMukherjee24
 
SAR - EEEfdfdsdasdsdasdasdasdasdasdasdasda.docx
Kanimozhi676285
 
SG1-ALM-MS-EL-30-0008 (00) MS - Isolators and disconnecting switches.pdf
djiceramil
 
Introduction to Fluid and Thermal Engineering
Avesahemad Husainy
 
Inventory management chapter in automation and robotics.
atisht0104
 
Jual GPS Geodetik CHCNAV i93 IMU-RTK Lanjutan dengan Survei Visual
Budi Minds
 
22PCOAM21 Session 1 Data Management.pptx
Guru Nanak Technical Institutions
 
Natural_Language_processing_Unit_I_notes.pdf
sanguleumeshit
 
Zero Carbon Building Performance standard
BassemOsman1
 
business incubation centre aaaaaaaaaaaaaa
hodeeesite4
 
勉強会資料_An Image is Worth More Than 16x16 Patches
NABLAS株式会社
 
Ad

Tries data structures

  • 2. What is a TRIE? • Tree based data structure used for Information ReTRIEval task • Also called as Digital Tree, Prefix tree, Radix Tree • Trie is used mostly for storing strings in a compact way. E.g. words in dictionary • A tries supports pattern matching queries in time proportional to the pattern size
  • 3. Trie – An ordered Tree • S= { bear, bell, bid, bull, buy, sell, stock, stop}
  • 4. Tries 4 Standard Tries • The standard trie for a set of strings S is an ordered tree such that: – Each node but the root is labeled with a character – The children of a node are alphabetically ordered – The paths from the external nodes to the root yield the strings of S • Example: standard trie for the set of strings S = { bear, bell, bid, bull, buy, sell, stock, stop } a e b r l l s u l l y e t l l o c k p i d
  • 5. TRIE Representation struct Trie { struct Trie* S[26]; //a-z or A-Z bool isEndOfWord; };
  • 6. Tries for number • Name | Social Security Number (SS#) Jack | 951-94-1654 Jill | 562-44-2169 Bill | 271-16-3624 Kathy | 278-49-1515 April | 951-23-7625
  • 7. Bill | 271-16-3624 Kathy | 278-49-1515
  • 8. 8 Applications of Tries • A standard trie supports the following operations on a preprocessed text in time O(m), where m = |X| -word matching: find the first occurrence of word X in the text -prefix matching: find the first occurrence of the longest prefix of word X in the text • Each operation is performed by tracing a path in the trie starting at the root
  • 9. Tries 9 Word Matching with a Trie • We insert the words of the text into a trie • Each leaf stores the occurrences of the associated word in the text s e e b e a r ? s e l l s t o c k ! s e e b u l l ? b u y s t o c k ! b i d s t o c k ! a a h e t h e b e l l ? s t o p ! b i d s t o c k ! 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 a r 87 88 a e b l s u l e t e 0, 24 o c i l r 6 l 78 d 47, 58 l 30 y 36 l 12 k 17, 40, 51, 62 p 84 h e r 69 a
  • 10. Prefix : What is prefix: • The prefix of a string is nothing but any n letters n≤|S| that can be considered beginning strictly from the starting of a string. • For example , the word “abacaba” has the following prefixes: a ab aba abac abaca abacab
  • 14. Use of Prefix in IP routing
  • 15. Suffix • The suffix of a string is nothing but any n letters n≤|S| that can be considered ending strictly at the end of a string. • For example , the word “abacaba” has the following prefixes: a ba aba caba acaba bacaba
  • 16. Operations • Insert – top-down traversal • Delete – bottom-up • Search – top-down
  • 17. Tries 17 Analysis of Standard Tries • A standard trie uses O(n) space and supports searches, insertions and deletions in time O(dm), where: n total size of the strings in S m size of the string parameter of the operation d size of the alphabet a e b r l l s u l l y e t l l o c k p i d
  • 19. Insert Word into TRIE Insert “their”
  • 20. Pseudo code for Insertion of a string into a TRIE void insert(String s) { for(every char in string s) { if(child node belonging to current char is null) { child node=new Node(); } current_node=child_node; } }
  • 21. Search • To search a trie for an element with a given key, – we start at the root and follow a path down the trie until we either fall off the trie (i.e., we follow a null pointer in a branch node) or – we reach an element node; The path we follow is determined by the alphabets/digits of the search key.
  • 22. Is string NEWS is present?
  • 23. Pseudo code to check whether a single word exists in a TRIE boolean check(String s) { for(every char in String s) { if(child node of current char is null) { return false; } } return true; }
  • 25. Check node has any children, before deleting that node //Node T has children or not boolean haveChildren(struct Trie * T) { for(every child node of T) { if(child node belonging to current char is not null) { return True; } } return False; }
  • 26. Algorithm requirements for deleting key 'k': 1. If key 'k' is not present in trie, then just return. 2. If key 'k' is not a prefix nor a suffix of any other key and nodes of key 'k' are not part of any other key then all the nodes starting from root node(excluding root node) to leaf node of key 'k' should be deleted. Delete key “word”
  • 27. Algorithm requirements for deleting key 'k': contd… 3. If key 'k' is a prefix of some other key, then leaf node corresponding to key 'k' should be marked as 'not a leaf node'. No node should be deleted in this case. delete key - "xyz", then without deleting any node we have to simply mark node 'z' as 'not a leaf node' and change its value to "NON_VALUE"
  • 28. 4. If key 'k' is a suffix of some other key 'k1', then all nodes of key 'k' which are not part of key 'k1' should be deleted. delete key - "xyzb", then we should only delete node "b" of key "xyzb" since other nodes of this key are also part of key "xyz".
  • 29. Algorithm requirements for deleting key 'k': (contd..) 5. If key 'k' is not a prefix nor a suffix of any other key but some nodes of key 'k' are shared with some other key 'k1', then nodes of key 'k' which are not common to any other key should be deleted and shared nodes should be kept intact. delete key "abc" which shares node 'a', node 'b' with key "abb", then the algorithm should delete only node 'c' of key "abc" and should not delete node 'a' and node 'b'.
  • 31. 31 Compressed Tries • Trie with nodes of degree at least 2 • Obtained from standard trie by compressing chains of redundant nodes Compressed Trie: Standard Trie:
  • 32. Tries 32 Compressed Tries e b ar ll s u ll y ell to ck p id a e b r l l s u l l y e t l l o c k p i d
  • 33. Draw a compressed Trie for S= {romane, romanus, romulus, rubens, ruber, rubicon, rubicundus}
  • 34. Draw a compressed Trie for S= {romane, romanus, romulus, rubens, ruber, rubicon, rubicundus}
  • 35. Tries 35 Suffix Trie • The suffix trie of a string X is the compressed trie of all the suffixes of X ( i.e. the one defined over a set of substrings of a string X ) e nimize nimize ze zei mi mize nimize ze m i n i z em i 0 1 2 3 4 5 6 7