SlideShare a Scribd company logo
1 of 18
Tries
Why Trie Data Structure?
 Searching trees in general favor keys which are of fixed size since this leads to
efficient storage management.
 However in case of applications which are retrieval based and which call for
keys varying length, tries provide betteroptions.
 Tries are also called as Lexicographic Search trees.
 The name trie (pronounced as “try”) originated from the word “retrieval”.
Trie
Presented By: Shubham Shukla Assistant Professor,
Department of Information Technology
2
 Trie Is An Efficient Information Retrieval Data Structure Also Called Digital Tree
And Sometimes Radix Tree Or Prefix Tree (As They Can Be Searched By Prefixes), Is
An Ordered Tree Data Structure That Is Used To Store A Dynamic Set Or Associative
Array Where The Keys Are Usually Strings.
 The term trie comes from "retrieval." Due to this etymology it is pronounced [tri]
("tree"), although some encourage the use of [traɪ] ("try") in order to distinguish it
from the more general tree.
Trie
Trie
Presented By: Shubham Shukla Assistant Professor,
Department of Information Technology
3
 Unlike a binary search tree, no node in the tree stores the key associated with that node;
instead, its position in the tree shows what key it is associated with.
 All the descendants of any one node have a common prefix of the string associated with
that node, and the root is associated with the empty string.
 Values are normally not associated with every node, only with leaves and some inner
nodes that happen to correspond to keys of interest.
Trie
Trie
Presented By: Shubham Shukla Assistant Professor,
Department of Information Technology
4
Tries
b s
e
a
r
$
i
d
$
u
l
k
$ $
l
u
n
d
a
y
$
$
Set of strings: {bear, bid, bulk, bull, sun, sunday}
 Most General Form Trie - General Tree
Trie
Presented By: Shubham Shukla Assistant Professor,
Department of Information Technology
5
Tries II
 Properties of a trie:
 A multi-way tree.
 Each node has between 1 and k descendants.
 Each link of the tree has matching character.
 Each leaf node corresponds to the final word (string), which can be collected on a path
from the root to this node.
Trie
Presented By: Shubham Shukla Assistant Professor,
Department of Information Technology
6
Search and Insertion in Tries
 The search algorithm follows the path from the root towards leaf, and can
result in word being found or not found. Complexity –tree depth.
 New string insertion checks if current character is at the current level of the
tree, starting from the root, if yes – proceeds down that branch labeled with
the character, if not – inserts a new branch at that level.
Trie
Presented By: Shubham Shukla Assistant Professor,
Department of Information Technology
7
Searching Algorithm
1. Search top level for node that matches first character in key
2. If none,
return false
Else,
3. If the matched character is 0?
return true
Else,
4. Move to subtrie that matched this character
5. Advance to next character in key*
6. Go to step 1
Trie
Presented By: Shubham Shukla Assistant Professor,
Department of Information Technology
8
Trie Properties
 Trie can be implemented
 A 2d array (sequential trie)
 A linked list
 A binary search tree
Trie
Presented By: Shubham Shukla Assistant Professor,
Department of Information Technology
9
Trie Complexity
 Size:
 O(N) in the worst-case {where N is the size of strings in the set}
 Search, insertion, and deletion (string of length p, k is size of alphabets(Max used
distinct element is strings)):
 O(kp) in general case
Trie
Presented By: Shubham Shukla Assistant Professor,
Department of Information Technology
10
Patricia Tries
 Similar to prefix B tee
 Substitute a chain of one-child nodes with an edge labeled with a unique string
 Each non-leaf node (except root) has at least two children
b s
e
a
r
$
i
d
$
u
l
k
$ $
l
u
n
d
a
y
$
$
b sun
ear$
id$ ul
k$
l$
day$
$
Trie
Presented By: Shubham Shukla Assistant Professor,
Department of Information Technology
11
Trie –BINARY tree
 In the example shown, keys are listed in
the nodes and values below them as a
Binary Tree. Each complete English word
has an integer value associated with it.
 It is not necessary for keys to be explicitly
stored in nodes. (In the figure, words are
shown only to illustrate how the trie
works.)
A trie for keys "to", "tea", "ten", "i", "in", and "inn“ (Wikepedia).
Trie
Presented By: Shubham Shukla Assistant Professor,
Department of Information Technology
12
Trie – Sequential (2d array)
 End of word (nil) and 26 characters – rows, number of columns grows as more strings get inserted, each
column ONLY contains strings starting with the same character (i.e. c), more than one column can
correspond to the same starting letter (i.e. cow and colt cannot be differentiated in column 2 as both start
with co).
 VERY INEFFICIENT IN TERMS OF SPACE.
Trie
Presented By: Shubham Shukla Assistant Professor,
Department of Information Technology
13
Trie-linked trie
 A trie implemented as a doubly chained tree:
vertical arrows are child pointers, dashed
horizontal arrows are next pointers. The set of
strings stored in this trie is {baby, bad, bank, box,
dad, dance}. The lists are sorted to allow traversal
in lexicographic order.
Trie
Presented By: Shubham Shukla Assistant Professor,
Department of Information Technology
14
 Looking up keys is faster. Looking up a key of length m takes worst case O(m) time. A
BST takes O(log n) time, where n is the number of elements in the tree, because
lookups depend on the depth of the tree, which is logarithmic in the number of keys.
Also, the simple operations tries use during lookup, such as array indexing using a
character, are fast on real machines.
 Tries can require less space when they contain a large number of short strings,
because the keys are not stored explicitly and nodes are shared between keys with
common initial subsequences.
 Tries help with longest-prefix matching, where we wish to find the key sharing the
longest possible prefix of characters all unique.
Advantages, relative to binary search tree
Trie
Presented By: Shubham Shukla Assistant Professor,
Department of Information Technology
15
 Tries can be slower in some cases than hash tables for looking up data, especially if the data
is directly accessed on a hard disk drive or some other secondary storage device where the
random access time is high compared to main memory.
 It is not easy to represent all keys as strings, such as floating point numbers, which can have
multiple string representations for the same floating point number, e.g. 1, 1.0, 1.00, +1.0, etc.
 Tries are frequently less space-efficient than hash tables.
 Unlike hash tables, tries are generally not already available in programming language
Drawbacks, relative to binary search tree
Trie
Presented By: Shubham Shukla Assistant Professor,
Department of Information Technology
16
 Looking up data in a trie is faster in the worst case, O(m) time, compared to an imperfect hash
table. An imperfect hash table can have key collisions. A key collision is the hash function
mapping of different keys to the same position in a hash table. The worst-case lookup speed
in an imperfect hash table is O(N) time, but far more typically is O(1), with O(m) time spent
evaluating the hash.
 There are no collisions of different keys in a trie.
 Buckets in a trie which are analogous to hash table buckets that store key collisions are only
necessary if a single key is associated with more than one value.
 There is no need to provide a hash function or to change hash functions as more keys are
added to a trie. A trie can provide an alphabetical ordering of the entries by key.
Advantages, relative to hash table
Trie
Presented By: Shubham Shukla Assistant Professor,
Department of Information Technology
17
Dictionary representation
 A common application of a trie is storing a dictionary, such as one found on a mobile telephone.
Such applications take advantage of a trie's ability to quickly search for, insert, and delete entries;
however, if storing dictionary words is all that is required (i.e. storage of information auxiliary to
each word is not required), a minimal acyclic deterministic finite automaton would use less space
than a trie.
 Tries are also well suited for implementing approximate matching algorithms, including thoseused in spell checking software.
Full text search
 A special kind of trie, called a suffix tree, can be used to index all suffixes in a text in order to
out fast full text searches.
Sorting
 Lexicographic sorting of a set of keys can be accomplished with a simple trie-based algorithm
Trie applications
Trie
Presented By: Shubham Shukla Assistant Professor,
Department of Information Technology
18

More Related Content

What's hot

12. Indexing and Hashing in DBMS
12. Indexing and Hashing in DBMS12. Indexing and Hashing in DBMS
12. Indexing and Hashing in DBMSkoolkampus
 
Tries - Tree Based Structures for Strings
Tries - Tree Based Structures for StringsTries - Tree Based Structures for Strings
Tries - Tree Based Structures for StringsAmrinder Arora
 
String Matching Algorithms-The Naive Algorithm
String Matching Algorithms-The Naive AlgorithmString Matching Algorithms-The Naive Algorithm
String Matching Algorithms-The Naive AlgorithmAdeel Rasheed
 
Binary Heap Tree, Data Structure
Binary Heap Tree, Data Structure Binary Heap Tree, Data Structure
Binary Heap Tree, Data Structure Anand Ingle
 
Asymptotic analysis
Asymptotic analysisAsymptotic analysis
Asymptotic analysisSoujanya V
 
Boyer moore algorithm
Boyer moore algorithmBoyer moore algorithm
Boyer moore algorithmAYESHA JAVED
 
Algorithm Complexity and Main Concepts
Algorithm Complexity and Main ConceptsAlgorithm Complexity and Main Concepts
Algorithm Complexity and Main ConceptsAdelina Ahadova
 
Hashing In Data Structure
Hashing In Data Structure Hashing In Data Structure
Hashing In Data Structure Meghaj Mallick
 
Polynomial reppresentation using Linkedlist-Application of LL.pptx
Polynomial reppresentation using Linkedlist-Application of LL.pptxPolynomial reppresentation using Linkedlist-Application of LL.pptx
Polynomial reppresentation using Linkedlist-Application of LL.pptxAlbin562191
 
Data Structure and Algorithms Binary Search Tree
Data Structure and Algorithms Binary Search TreeData Structure and Algorithms Binary Search Tree
Data Structure and Algorithms Binary Search TreeManishPrajapati78
 
Tree Data Structure by Daniyal Khan
Tree Data Structure by Daniyal KhanTree Data Structure by Daniyal Khan
Tree Data Structure by Daniyal KhanDaniyal Khan
 
Biconnected components (13024116056)
Biconnected components (13024116056)Biconnected components (13024116056)
Biconnected components (13024116056)Akshay soni
 
Lec 17 heap data structure
Lec 17 heap data structureLec 17 heap data structure
Lec 17 heap data structureSajid Marwat
 
Graph representation
Graph representationGraph representation
Graph representationTech_MX
 

What's hot (20)

12. Indexing and Hashing in DBMS
12. Indexing and Hashing in DBMS12. Indexing and Hashing in DBMS
12. Indexing and Hashing in DBMS
 
Threaded Binary Tree.pptx
Threaded Binary Tree.pptxThreaded Binary Tree.pptx
Threaded Binary Tree.pptx
 
Binary tree
Binary treeBinary tree
Binary tree
 
Tries - Tree Based Structures for Strings
Tries - Tree Based Structures for StringsTries - Tree Based Structures for Strings
Tries - Tree Based Structures for Strings
 
String Matching Algorithms-The Naive Algorithm
String Matching Algorithms-The Naive AlgorithmString Matching Algorithms-The Naive Algorithm
String Matching Algorithms-The Naive Algorithm
 
Binary Heap Tree, Data Structure
Binary Heap Tree, Data Structure Binary Heap Tree, Data Structure
Binary Heap Tree, Data Structure
 
Asymptotic analysis
Asymptotic analysisAsymptotic analysis
Asymptotic analysis
 
Boyer moore algorithm
Boyer moore algorithmBoyer moore algorithm
Boyer moore algorithm
 
Topological Sort
Topological SortTopological Sort
Topological Sort
 
Boyer more algorithm
Boyer more algorithmBoyer more algorithm
Boyer more algorithm
 
Algorithm Complexity and Main Concepts
Algorithm Complexity and Main ConceptsAlgorithm Complexity and Main Concepts
Algorithm Complexity and Main Concepts
 
Hashing In Data Structure
Hashing In Data Structure Hashing In Data Structure
Hashing In Data Structure
 
Polynomial reppresentation using Linkedlist-Application of LL.pptx
Polynomial reppresentation using Linkedlist-Application of LL.pptxPolynomial reppresentation using Linkedlist-Application of LL.pptx
Polynomial reppresentation using Linkedlist-Application of LL.pptx
 
B and B+ tree
B and B+ treeB and B+ tree
B and B+ tree
 
Data Structure and Algorithms Binary Search Tree
Data Structure and Algorithms Binary Search TreeData Structure and Algorithms Binary Search Tree
Data Structure and Algorithms Binary Search Tree
 
Tree Data Structure by Daniyal Khan
Tree Data Structure by Daniyal KhanTree Data Structure by Daniyal Khan
Tree Data Structure by Daniyal Khan
 
Biconnected components (13024116056)
Biconnected components (13024116056)Biconnected components (13024116056)
Biconnected components (13024116056)
 
linear probing
linear probinglinear probing
linear probing
 
Lec 17 heap data structure
Lec 17 heap data structureLec 17 heap data structure
Lec 17 heap data structure
 
Graph representation
Graph representationGraph representation
Graph representation
 

Similar to Tries

TRIES_data_structure
TRIES_data_structureTRIES_data_structure
TRIES_data_structureddewithaman10
 
Hi,Based on the Data We can Use diffrent type of Data Structure..pdf
Hi,Based on the Data We can Use diffrent type of Data Structure..pdfHi,Based on the Data We can Use diffrent type of Data Structure..pdf
Hi,Based on the Data We can Use diffrent type of Data Structure..pdfaradhana9856
 
presentation on important DAG,TRIE,Hashing.pptx
presentation on important DAG,TRIE,Hashing.pptxpresentation on important DAG,TRIE,Hashing.pptx
presentation on important DAG,TRIE,Hashing.pptxjainaaru59
 
introduction to trees,graphs,hashing
introduction to trees,graphs,hashingintroduction to trees,graphs,hashing
introduction to trees,graphs,hashingAkhil Prem
 
Index Structures.pptx
Index Structures.pptxIndex Structures.pptx
Index Structures.pptxMBablu1
 
e computer notes - Binary search tree
e computer notes - Binary search treee computer notes - Binary search tree
e computer notes - Binary search treeecomputernotes
 
Cinterviews Binarysearch Tree
Cinterviews Binarysearch TreeCinterviews Binarysearch Tree
Cinterviews Binarysearch Treecinterviews
 
Souvenir's Booth - Algorithm Design and Analysis Project Project Report
Souvenir's Booth - Algorithm Design and Analysis Project Project ReportSouvenir's Booth - Algorithm Design and Analysis Project Project Report
Souvenir's Booth - Algorithm Design and Analysis Project Project ReportAkshit Arora
 
Bit by bit into data structures
Bit by bit into data structuresBit by bit into data structures
Bit by bit into data structuresHridyesh Bisht
 
Introduction to Data Structure
Introduction to Data StructureIntroduction to Data Structure
Introduction to Data StructureJazz Jinia Bhowmik
 
International Journal of Engineering Research and Development
International Journal of Engineering Research and DevelopmentInternational Journal of Engineering Research and Development
International Journal of Engineering Research and DevelopmentIJERD Editor
 
Data structure & algorithms introduction
Data structure & algorithms introductionData structure & algorithms introduction
Data structure & algorithms introductionSugandh Wafai
 
DATA STUCTURES-TREES.pptx
DATA STUCTURES-TREES.pptxDATA STUCTURES-TREES.pptx
DATA STUCTURES-TREES.pptxAryaMNair6
 
Tree data structure in java
Tree data structure in javaTree data structure in java
Tree data structure in javaIrfan CH
 

Similar to Tries (20)

Tries
TriesTries
Tries
 
Shishirppt
ShishirpptShishirppt
Shishirppt
 
TRIES_data_structure
TRIES_data_structureTRIES_data_structure
TRIES_data_structure
 
Hi,Based on the Data We can Use diffrent type of Data Structure..pdf
Hi,Based on the Data We can Use diffrent type of Data Structure..pdfHi,Based on the Data We can Use diffrent type of Data Structure..pdf
Hi,Based on the Data We can Use diffrent type of Data Structure..pdf
 
String Searching and Matching
String Searching and MatchingString Searching and Matching
String Searching and Matching
 
presentation on important DAG,TRIE,Hashing.pptx
presentation on important DAG,TRIE,Hashing.pptxpresentation on important DAG,TRIE,Hashing.pptx
presentation on important DAG,TRIE,Hashing.pptx
 
introduction to trees,graphs,hashing
introduction to trees,graphs,hashingintroduction to trees,graphs,hashing
introduction to trees,graphs,hashing
 
Index Structures.pptx
Index Structures.pptxIndex Structures.pptx
Index Structures.pptx
 
e computer notes - Binary search tree
e computer notes - Binary search treee computer notes - Binary search tree
e computer notes - Binary search tree
 
Cinterviews Binarysearch Tree
Cinterviews Binarysearch TreeCinterviews Binarysearch Tree
Cinterviews Binarysearch Tree
 
Souvenir's Booth - Algorithm Design and Analysis Project Project Report
Souvenir's Booth - Algorithm Design and Analysis Project Project ReportSouvenir's Booth - Algorithm Design and Analysis Project Project Report
Souvenir's Booth - Algorithm Design and Analysis Project Project Report
 
Unit 3,4.docx
Unit 3,4.docxUnit 3,4.docx
Unit 3,4.docx
 
Bit by bit into data structures
Bit by bit into data structuresBit by bit into data structures
Bit by bit into data structures
 
A survey of xml tree patterns
A survey of xml tree patternsA survey of xml tree patterns
A survey of xml tree patterns
 
Introduction to Data Structure
Introduction to Data StructureIntroduction to Data Structure
Introduction to Data Structure
 
International Journal of Engineering Research and Development
International Journal of Engineering Research and DevelopmentInternational Journal of Engineering Research and Development
International Journal of Engineering Research and Development
 
Data structure & algorithms introduction
Data structure & algorithms introductionData structure & algorithms introduction
Data structure & algorithms introduction
 
DATA STUCTURES-TREES.pptx
DATA STUCTURES-TREES.pptxDATA STUCTURES-TREES.pptx
DATA STUCTURES-TREES.pptx
 
Implementation of union
Implementation of unionImplementation of union
Implementation of union
 
Tree data structure in java
Tree data structure in javaTree data structure in java
Tree data structure in java
 

More from Shubham Shukla

Floyd warshall algo {dynamic approach}
Floyd warshall algo {dynamic approach}Floyd warshall algo {dynamic approach}
Floyd warshall algo {dynamic approach}Shubham Shukla
 
Skip lists (Advance Data structure)
Skip lists (Advance Data structure)Skip lists (Advance Data structure)
Skip lists (Advance Data structure)Shubham Shukla
 
Lecture 4 sql {basics keys and constraints}
Lecture 4 sql {basics  keys and constraints}Lecture 4 sql {basics  keys and constraints}
Lecture 4 sql {basics keys and constraints}Shubham Shukla
 
Lecture 3 sql {basics ddl commands}
Lecture 3 sql {basics  ddl commands}Lecture 3 sql {basics  ddl commands}
Lecture 3 sql {basics ddl commands}Shubham Shukla
 
Lecture 2 sql {basics date type, constrains , integrity types etc.}
Lecture 2 sql {basics  date type, constrains , integrity types etc.}Lecture 2 sql {basics  date type, constrains , integrity types etc.}
Lecture 2 sql {basics date type, constrains , integrity types etc.}Shubham Shukla
 
Lecture 1 sql {installation & uninstallation}
Lecture 1 sql {installation & uninstallation}Lecture 1 sql {installation & uninstallation}
Lecture 1 sql {installation & uninstallation}Shubham Shukla
 

More from Shubham Shukla (6)

Floyd warshall algo {dynamic approach}
Floyd warshall algo {dynamic approach}Floyd warshall algo {dynamic approach}
Floyd warshall algo {dynamic approach}
 
Skip lists (Advance Data structure)
Skip lists (Advance Data structure)Skip lists (Advance Data structure)
Skip lists (Advance Data structure)
 
Lecture 4 sql {basics keys and constraints}
Lecture 4 sql {basics  keys and constraints}Lecture 4 sql {basics  keys and constraints}
Lecture 4 sql {basics keys and constraints}
 
Lecture 3 sql {basics ddl commands}
Lecture 3 sql {basics  ddl commands}Lecture 3 sql {basics  ddl commands}
Lecture 3 sql {basics ddl commands}
 
Lecture 2 sql {basics date type, constrains , integrity types etc.}
Lecture 2 sql {basics  date type, constrains , integrity types etc.}Lecture 2 sql {basics  date type, constrains , integrity types etc.}
Lecture 2 sql {basics date type, constrains , integrity types etc.}
 
Lecture 1 sql {installation & uninstallation}
Lecture 1 sql {installation & uninstallation}Lecture 1 sql {installation & uninstallation}
Lecture 1 sql {installation & uninstallation}
 

Recently uploaded

Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...M56BOOKSTORE PRODUCT/SERVICE
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfUjwalaBharambe
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxJiesonDelaCerna
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 

Recently uploaded (20)

Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptx
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 

Tries

  • 2. Why Trie Data Structure?  Searching trees in general favor keys which are of fixed size since this leads to efficient storage management.  However in case of applications which are retrieval based and which call for keys varying length, tries provide betteroptions.  Tries are also called as Lexicographic Search trees.  The name trie (pronounced as “try”) originated from the word “retrieval”. Trie Presented By: Shubham Shukla Assistant Professor, Department of Information Technology 2
  • 3.  Trie Is An Efficient Information Retrieval Data Structure Also Called Digital Tree And Sometimes Radix Tree Or Prefix Tree (As They Can Be Searched By Prefixes), Is An Ordered Tree Data Structure That Is Used To Store A Dynamic Set Or Associative Array Where The Keys Are Usually Strings.  The term trie comes from "retrieval." Due to this etymology it is pronounced [tri] ("tree"), although some encourage the use of [traɪ] ("try") in order to distinguish it from the more general tree. Trie Trie Presented By: Shubham Shukla Assistant Professor, Department of Information Technology 3
  • 4.  Unlike a binary search tree, no node in the tree stores the key associated with that node; instead, its position in the tree shows what key it is associated with.  All the descendants of any one node have a common prefix of the string associated with that node, and the root is associated with the empty string.  Values are normally not associated with every node, only with leaves and some inner nodes that happen to correspond to keys of interest. Trie Trie Presented By: Shubham Shukla Assistant Professor, Department of Information Technology 4
  • 5. Tries b s e a r $ i d $ u l k $ $ l u n d a y $ $ Set of strings: {bear, bid, bulk, bull, sun, sunday}  Most General Form Trie - General Tree Trie Presented By: Shubham Shukla Assistant Professor, Department of Information Technology 5
  • 6. Tries II  Properties of a trie:  A multi-way tree.  Each node has between 1 and k descendants.  Each link of the tree has matching character.  Each leaf node corresponds to the final word (string), which can be collected on a path from the root to this node. Trie Presented By: Shubham Shukla Assistant Professor, Department of Information Technology 6
  • 7. Search and Insertion in Tries  The search algorithm follows the path from the root towards leaf, and can result in word being found or not found. Complexity –tree depth.  New string insertion checks if current character is at the current level of the tree, starting from the root, if yes – proceeds down that branch labeled with the character, if not – inserts a new branch at that level. Trie Presented By: Shubham Shukla Assistant Professor, Department of Information Technology 7
  • 8. Searching Algorithm 1. Search top level for node that matches first character in key 2. If none, return false Else, 3. If the matched character is 0? return true Else, 4. Move to subtrie that matched this character 5. Advance to next character in key* 6. Go to step 1 Trie Presented By: Shubham Shukla Assistant Professor, Department of Information Technology 8
  • 9. Trie Properties  Trie can be implemented  A 2d array (sequential trie)  A linked list  A binary search tree Trie Presented By: Shubham Shukla Assistant Professor, Department of Information Technology 9
  • 10. Trie Complexity  Size:  O(N) in the worst-case {where N is the size of strings in the set}  Search, insertion, and deletion (string of length p, k is size of alphabets(Max used distinct element is strings)):  O(kp) in general case Trie Presented By: Shubham Shukla Assistant Professor, Department of Information Technology 10
  • 11. Patricia Tries  Similar to prefix B tee  Substitute a chain of one-child nodes with an edge labeled with a unique string  Each non-leaf node (except root) has at least two children b s e a r $ i d $ u l k $ $ l u n d a y $ $ b sun ear$ id$ ul k$ l$ day$ $ Trie Presented By: Shubham Shukla Assistant Professor, Department of Information Technology 11
  • 12. Trie –BINARY tree  In the example shown, keys are listed in the nodes and values below them as a Binary Tree. Each complete English word has an integer value associated with it.  It is not necessary for keys to be explicitly stored in nodes. (In the figure, words are shown only to illustrate how the trie works.) A trie for keys "to", "tea", "ten", "i", "in", and "inn“ (Wikepedia). Trie Presented By: Shubham Shukla Assistant Professor, Department of Information Technology 12
  • 13. Trie – Sequential (2d array)  End of word (nil) and 26 characters – rows, number of columns grows as more strings get inserted, each column ONLY contains strings starting with the same character (i.e. c), more than one column can correspond to the same starting letter (i.e. cow and colt cannot be differentiated in column 2 as both start with co).  VERY INEFFICIENT IN TERMS OF SPACE. Trie Presented By: Shubham Shukla Assistant Professor, Department of Information Technology 13
  • 14. Trie-linked trie  A trie implemented as a doubly chained tree: vertical arrows are child pointers, dashed horizontal arrows are next pointers. The set of strings stored in this trie is {baby, bad, bank, box, dad, dance}. The lists are sorted to allow traversal in lexicographic order. Trie Presented By: Shubham Shukla Assistant Professor, Department of Information Technology 14
  • 15.  Looking up keys is faster. Looking up a key of length m takes worst case O(m) time. A BST takes O(log n) time, where n is the number of elements in the tree, because lookups depend on the depth of the tree, which is logarithmic in the number of keys. Also, the simple operations tries use during lookup, such as array indexing using a character, are fast on real machines.  Tries can require less space when they contain a large number of short strings, because the keys are not stored explicitly and nodes are shared between keys with common initial subsequences.  Tries help with longest-prefix matching, where we wish to find the key sharing the longest possible prefix of characters all unique. Advantages, relative to binary search tree Trie Presented By: Shubham Shukla Assistant Professor, Department of Information Technology 15
  • 16.  Tries can be slower in some cases than hash tables for looking up data, especially if the data is directly accessed on a hard disk drive or some other secondary storage device where the random access time is high compared to main memory.  It is not easy to represent all keys as strings, such as floating point numbers, which can have multiple string representations for the same floating point number, e.g. 1, 1.0, 1.00, +1.0, etc.  Tries are frequently less space-efficient than hash tables.  Unlike hash tables, tries are generally not already available in programming language Drawbacks, relative to binary search tree Trie Presented By: Shubham Shukla Assistant Professor, Department of Information Technology 16
  • 17.  Looking up data in a trie is faster in the worst case, O(m) time, compared to an imperfect hash table. An imperfect hash table can have key collisions. A key collision is the hash function mapping of different keys to the same position in a hash table. The worst-case lookup speed in an imperfect hash table is O(N) time, but far more typically is O(1), with O(m) time spent evaluating the hash.  There are no collisions of different keys in a trie.  Buckets in a trie which are analogous to hash table buckets that store key collisions are only necessary if a single key is associated with more than one value.  There is no need to provide a hash function or to change hash functions as more keys are added to a trie. A trie can provide an alphabetical ordering of the entries by key. Advantages, relative to hash table Trie Presented By: Shubham Shukla Assistant Professor, Department of Information Technology 17
  • 18. Dictionary representation  A common application of a trie is storing a dictionary, such as one found on a mobile telephone. Such applications take advantage of a trie's ability to quickly search for, insert, and delete entries; however, if storing dictionary words is all that is required (i.e. storage of information auxiliary to each word is not required), a minimal acyclic deterministic finite automaton would use less space than a trie.  Tries are also well suited for implementing approximate matching algorithms, including thoseused in spell checking software. Full text search  A special kind of trie, called a suffix tree, can be used to index all suffixes in a text in order to out fast full text searches. Sorting  Lexicographic sorting of a set of keys can be accomplished with a simple trie-based algorithm Trie applications Trie Presented By: Shubham Shukla Assistant Professor, Department of Information Technology 18