SlideShare a Scribd company logo
1 of 11
Trie Tree

Md. Shakil Ahmed
Software Engineer
Astha it research & consultancy ltd.
Dhaka, Bangladesh
Trie Tree
• a trie, or prefix tree, is an
ordered tree data structure that is
used to store an associative array
 where the keys are usually strings.
The next figure shows a trie with the
words "tree", "trie", "algo", "assoc",
"all", and "also."
Use:
• countPreffixes. This function will count the
  number of words in the dictionary that have a
  string prefix as prefix.
• countWords. This function will count the
  number of words in the dictionary that match
  exactly with a given string word.
Problem
LightOJ => 1129 - Consistency Checker
no number is the prefix another number in that data set. Let's consider
    a data set:
1. 123
2. 5123456
3. 123456
In this data set, numbers not consistent, because the first number is
    the prefix of the third one.
n integer n (1 ≤ n ≤ 10000) denoting the total numbers in their list.
    Each of the next n lines contains one unique phone number. A
    number is a sequence of digits and has length from 1 to 10.
Print 'YES' if the list is consistent or 'NO' if it's not.
Source Code
long B[100009],A[100009][10],yes,len,N; char temp[15];
void MakeTree(int root,int h1,int h2) {
if(h1<len)
  { if(A[root][temp[h1]-'0']==-1)
    {   if(B[root]==1)
         yes = 0;
         else{
                  long i1;
                  for(i1=0;i1<10;i1++)
                  A[N][i1]=-1;
                  B[N]=0;
                  A[root][temp[h1]-'0']=N; N++;
                  MakeTree(N-1,h1+1,0);
       } }
else
   {
         if(B[root]==1)
         yes = 0;
         else
         MakeTree(A[root][temp[h1]-'0'],h1+1,1);
     }
    }
    else
    {
      if(B[root]==1 || h2 == 1)
        yes = 0;
      B[root]=1;
    }
}
Use:
scanf("%ld",&n);
yes = 1;
for(i=0;i<10;i++)
A[0][i]=-1;
B[0]=0;
N=1;
for(i=0;i<n;i++)
{scanf("%s",temp);
len = strlen(temp);
if(yes==1)
MakeTree(0,0,1);
}
if(yes==1)
printf("YESn");
else
printf("NOn");
Sample
• LightOJ =>
• http://www.lightoj.com/volume_showproble
  m.php?problem=1114
• http://www.lightoj.com/volume_showproble
  m.php?problem=1129
• http://www.lightoj.com/volume_showproble
  m.php?problem=1224
Thanks!

More Related Content

What's hot

Data Structure Midterm Lesson Arrays
Data Structure Midterm Lesson ArraysData Structure Midterm Lesson Arrays
Data Structure Midterm Lesson Arrays
Maulen Bale
 
Machine learning interviews day4
Machine learning interviews   day4Machine learning interviews   day4
Machine learning interviews day4
rajmohanc
 

What's hot (20)

Data Structure Midterm Lesson Arrays
Data Structure Midterm Lesson ArraysData Structure Midterm Lesson Arrays
Data Structure Midterm Lesson Arrays
 
Arrays
ArraysArrays
Arrays
 
Array
ArrayArray
Array
 
Hashing
HashingHashing
Hashing
 
Array in c++
Array in c++Array in c++
Array in c++
 
Array Data Structures
Array Data StructuresArray Data Structures
Array Data Structures
 
Data structures
Data structures Data structures
Data structures
 
Machine learning interviews day4
Machine learning interviews   day4Machine learning interviews   day4
Machine learning interviews day4
 
Arrays basics
Arrays basicsArrays basics
Arrays basics
 
Introduction to Array ppt
Introduction to Array pptIntroduction to Array ppt
Introduction to Array ppt
 
Array C programming
Array C programmingArray C programming
Array C programming
 
C Programming : Arrays
C Programming : ArraysC Programming : Arrays
C Programming : Arrays
 
SPL 10 | One Dimensional Array in C
SPL 10 | One Dimensional Array in CSPL 10 | One Dimensional Array in C
SPL 10 | One Dimensional Array in C
 
Arrays in c
Arrays in cArrays in c
Arrays in c
 
7.basic array
7.basic array7.basic array
7.basic array
 
Arrays in c
Arrays in cArrays in c
Arrays in c
 
Array,MULTI ARRAY, IN C
Array,MULTI ARRAY, IN CArray,MULTI ARRAY, IN C
Array,MULTI ARRAY, IN C
 
Array in-c
Array in-cArray in-c
Array in-c
 
Sparse Matrix and Polynomial
Sparse Matrix and PolynomialSparse Matrix and Polynomial
Sparse Matrix and Polynomial
 
Lecture 16 - Multi dimensional Array
Lecture 16 - Multi dimensional ArrayLecture 16 - Multi dimensional Array
Lecture 16 - Multi dimensional Array
 

Viewers also liked

TRIES_data_structure
TRIES_data_structureTRIES_data_structure
TRIES_data_structure
ddewithaman10
 
Application of tries
Application of triesApplication of tries
Application of tries
Tech_MX
 
Introduction of suffix tree
Introduction of suffix treeIntroduction of suffix tree
Introduction of suffix tree
Liou Shu Hung
 
Packet forwarding in wan.46
Packet  forwarding in wan.46Packet  forwarding in wan.46
Packet forwarding in wan.46
myrajendra
 
Binomial heap presentation
Binomial heap presentationBinomial heap presentation
Binomial heap presentation
Hafsa.Naseem
 

Viewers also liked (19)

Trie Data Structure
Trie Data StructureTrie Data Structure
Trie Data Structure
 
Tries - Tree Based Structures for Strings
Tries - Tree Based Structures for StringsTries - Tree Based Structures for Strings
Tries - Tree Based Structures for Strings
 
TRIES_data_structure
TRIES_data_structureTRIES_data_structure
TRIES_data_structure
 
Application of tries
Application of triesApplication of tries
Application of tries
 
Data structure tries
Data structure triesData structure tries
Data structure tries
 
Lec18
Lec18Lec18
Lec18
 
Trie (1)
Trie (1)Trie (1)
Trie (1)
 
Introduction of suffix tree
Introduction of suffix treeIntroduction of suffix tree
Introduction of suffix tree
 
Packet forwarding in wan.46
Packet  forwarding in wan.46Packet  forwarding in wan.46
Packet forwarding in wan.46
 
Suffix Tree and Suffix Array
Suffix Tree and Suffix ArraySuffix Tree and Suffix Array
Suffix Tree and Suffix Array
 
Introduction to statistics ii
Introduction to statistics iiIntroduction to statistics ii
Introduction to statistics ii
 
Yzm 2116 Bölüm 7 - Tree ve Binary tree - İkili Ağaç
Yzm 2116  Bölüm 7 - Tree  ve Binary tree - İkili AğaçYzm 2116  Bölüm 7 - Tree  ve Binary tree - İkili Ağaç
Yzm 2116 Bölüm 7 - Tree ve Binary tree - İkili Ağaç
 
Fundamentals
FundamentalsFundamentals
Fundamentals
 
Basic Packet Forwarding in NS2
Basic Packet Forwarding in NS2Basic Packet Forwarding in NS2
Basic Packet Forwarding in NS2
 
Digital Search Tree
Digital Search TreeDigital Search Tree
Digital Search Tree
 
Multi ways trees
Multi ways treesMulti ways trees
Multi ways trees
 
Binomial heap presentation
Binomial heap presentationBinomial heap presentation
Binomial heap presentation
 
Priority queues
Priority queuesPriority queues
Priority queues
 
Cis82 e2-1-packet forwarding
Cis82 e2-1-packet forwardingCis82 e2-1-packet forwarding
Cis82 e2-1-packet forwarding
 

Similar to Trie tree

2DArrays.ppt
2DArrays.ppt2DArrays.ppt
2DArrays.ppt
Nooryaseen9
 
Each team works on assigned sorting algorithms and will give demonstr.pdf
Each team works on assigned sorting algorithms and will give demonstr.pdfEach team works on assigned sorting algorithms and will give demonstr.pdf
Each team works on assigned sorting algorithms and will give demonstr.pdf
omarionmatzmcwill497
 

Similar to Trie tree (20)

array.ppt
array.pptarray.ppt
array.ppt
 
Arrays
ArraysArrays
Arrays
 
CP Handout#7
CP Handout#7CP Handout#7
CP Handout#7
 
Array
ArrayArray
Array
 
Arrays in C language
Arrays in C languageArrays in C language
Arrays in C language
 
COM1407: Arrays
COM1407: ArraysCOM1407: Arrays
COM1407: Arrays
 
2DArrays.ppt
2DArrays.ppt2DArrays.ppt
2DArrays.ppt
 
07. Java Array, Set and Maps
07.  Java Array, Set and Maps07.  Java Array, Set and Maps
07. Java Array, Set and Maps
 
Each team works on assigned sorting algorithms and will give demonstr.pdf
Each team works on assigned sorting algorithms and will give demonstr.pdfEach team works on assigned sorting algorithms and will give demonstr.pdf
Each team works on assigned sorting algorithms and will give demonstr.pdf
 
R Basics
R BasicsR Basics
R Basics
 
Data structure.pptx
Data structure.pptxData structure.pptx
Data structure.pptx
 
Python 04-ifelse-return-input-strings.pptx
Python 04-ifelse-return-input-strings.pptxPython 04-ifelse-return-input-strings.pptx
Python 04-ifelse-return-input-strings.pptx
 
Array-part1
Array-part1Array-part1
Array-part1
 
LeetCode April Coding Challenge
LeetCode April Coding ChallengeLeetCode April Coding Challenge
LeetCode April Coding Challenge
 
Array
ArrayArray
Array
 
L5 array
L5 arrayL5 array
L5 array
 
Big Data Mining in Indian Economic Survey 2017
Big Data Mining in Indian Economic Survey 2017Big Data Mining in Indian Economic Survey 2017
Big Data Mining in Indian Economic Survey 2017
 
Python quickstart for programmers: Python Kung Fu
Python quickstart for programmers: Python Kung FuPython quickstart for programmers: Python Kung Fu
Python quickstart for programmers: Python Kung Fu
 
Array
ArrayArray
Array
 
11.1 Sequences and Series
11.1 Sequences and Series11.1 Sequences and Series
11.1 Sequences and Series
 

More from Shakil Ahmed (20)

Algorithm
AlgorithmAlgorithm
Algorithm
 
B-tree & R-tree
B-tree & R-treeB-tree & R-tree
B-tree & R-tree
 
Advanced data structure
Advanced data structureAdvanced data structure
Advanced data structure
 
Prototype pattern
Prototype patternPrototype pattern
Prototype pattern
 
Observer pattern
Observer patternObserver pattern
Observer pattern
 
Mediator pattern
Mediator patternMediator pattern
Mediator pattern
 
Flyweight pattern
Flyweight patternFlyweight pattern
Flyweight pattern
 
Facade pattern
Facade patternFacade pattern
Facade pattern
 
Composite pattern
Composite patternComposite pattern
Composite pattern
 
Command pattern
Command patternCommand pattern
Command pattern
 
Chain of responsibility
Chain of responsibilityChain of responsibility
Chain of responsibility
 
Builder pattern
Builder patternBuilder pattern
Builder pattern
 
Bridge pattern
Bridge patternBridge pattern
Bridge pattern
 
Adapter pattern
Adapter patternAdapter pattern
Adapter pattern
 
Proxy pattern
Proxy patternProxy pattern
Proxy pattern
 
iOS 5
iOS 5iOS 5
iOS 5
 
Ios development
Ios developmentIos development
Ios development
 
Graph
GraphGraph
Graph
 
Lowest common ancestor
Lowest common ancestorLowest common ancestor
Lowest common ancestor
 
Segment tree
Segment treeSegment tree
Segment tree
 

Recently uploaded

Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
fonyou31
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 

Recently uploaded (20)

Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 

Trie tree

  • 1. Trie Tree Md. Shakil Ahmed Software Engineer Astha it research & consultancy ltd. Dhaka, Bangladesh
  • 2. Trie Tree • a trie, or prefix tree, is an ordered tree data structure that is used to store an associative array where the keys are usually strings.
  • 3. The next figure shows a trie with the words "tree", "trie", "algo", "assoc", "all", and "also."
  • 4. Use: • countPreffixes. This function will count the number of words in the dictionary that have a string prefix as prefix. • countWords. This function will count the number of words in the dictionary that match exactly with a given string word.
  • 5.
  • 6. Problem LightOJ => 1129 - Consistency Checker no number is the prefix another number in that data set. Let's consider a data set: 1. 123 2. 5123456 3. 123456 In this data set, numbers not consistent, because the first number is the prefix of the third one. n integer n (1 ≤ n ≤ 10000) denoting the total numbers in their list. Each of the next n lines contains one unique phone number. A number is a sequence of digits and has length from 1 to 10. Print 'YES' if the list is consistent or 'NO' if it's not.
  • 7. Source Code long B[100009],A[100009][10],yes,len,N; char temp[15]; void MakeTree(int root,int h1,int h2) { if(h1<len) { if(A[root][temp[h1]-'0']==-1) { if(B[root]==1) yes = 0; else{ long i1; for(i1=0;i1<10;i1++) A[N][i1]=-1; B[N]=0; A[root][temp[h1]-'0']=N; N++; MakeTree(N-1,h1+1,0); } }
  • 8. else { if(B[root]==1) yes = 0; else MakeTree(A[root][temp[h1]-'0'],h1+1,1); } } else { if(B[root]==1 || h2 == 1) yes = 0; B[root]=1; } }
  • 9. Use: scanf("%ld",&n); yes = 1; for(i=0;i<10;i++) A[0][i]=-1; B[0]=0; N=1; for(i=0;i<n;i++) {scanf("%s",temp); len = strlen(temp); if(yes==1) MakeTree(0,0,1); } if(yes==1) printf("YESn"); else printf("NOn");
  • 10. Sample • LightOJ => • http://www.lightoj.com/volume_showproble m.php?problem=1114 • http://www.lightoj.com/volume_showproble m.php?problem=1129 • http://www.lightoj.com/volume_showproble m.php?problem=1224