SlideShare uma empresa Scribd logo
1 de 6
Baixar para ler offline
SOM-ITSOLUTIONS
ALGORITHM
Arranging the words of an Input Text Lexicographically - Trie
SOMENATH MUKHOPADHYAY
som-itsolutions 
#A2 1/13 South Purbachal Hospital Road Kolkata 700078 Mob: +91 9748185282
Email: ​som.mukhopadhyay@som-itsolutions.com​ / ​som.mukhopadhyay@gmail.com
Website: ​http://www.som-itsolutions.com/
Blog: ​www.som-itsolutions.blogspot.com
As i am trying to recapitulate my knowledge base vis-a-vis data structure and algorithm, i am
trying to solve various practical real life problems. The real aim is to prepare my students for
programming olympiad. Thus while browsing through the Indian Association for Research in
Computing Science, i got a this problem :
“
Problem : Word List
In this problem the input will consist of a number of lines of English text consisting of the letters of
the English alphabet, the punctuation marks ' (apostrophe), . (full stop), , (comma), ; (semicolon),
:(colon) and white space characters (blank, newline).
Your task is print the words in the text in lexicographic order (that is, dictionary order). Each word
should appear exactly once in your list. You can ignore the case (for instance, "The" and "the"
are to be treated as the same word.) There should be no uppercase letters in the output.
For example, consider the following candidate for the input text:
This is a sample piece of text to illustrate this
problem.
The corresponding output would read as:
a
illustrate
is
of
piece
problem
sample
text
this
to
”
So i thought to use the concept of Trie or Prefix Tree to come out with a solution. Trie is
a special kind of Tree Data Structure which is very useful for Dictionary kind of
application. It has the in built properties of Prefix matching, neighbor search etc. The
Trie data structure is as shown below:
With the given problem, we first remove the punctuation and make all lowercase of the
input text. Then we can tokenize the string and get different words. We then put these
words in a Trie. After that if we traverse the Trie Preorderly, we will get the desired
output.
The whole solution looks like the following.
Class Trie
package​ ​com.somitsolutions.java.training.wordsindictionaryform​;
public​ ​class​ ​Trie​ ​{
​// Alphabet size (# of symbols)
​static​ ​final​ ​int​ ALPHABET_SIZE ​=​ ​26​;
​// trie node
​static​ ​class​ ​TrieNode
​{
TrieNode​[]​ children ​=​ ​new​ TrieNode​[​ALPHABET_SIZE​];
​// isEndOfWord is true if the node represents
​// end of a word
​boolean​ isEndOfWord​;
String key​;​//Store the
TrieNode​(){
key ​=​ ​null​;
isEndOfWord ​=​ ​false​;
​for​ ​(​int​ i ​=​ ​0​;​ i ​<​ ALPHABET_SIZE​;​ i​++)
children​[​i​]​ ​=​ ​null​;
​}
​}
​static​ TrieNode root​;
​// If not present, inserts key into trie
​// If the key is prefix of trie node,
​// just marks leaf node
​static​ ​void​ ​insert​(​String key​)
​{
​int​ level​;
​int​ length ​=​ key​.​length​();
​int​ index​;
TrieNode pCrawl ​=​ root​;
​for​ ​(​level ​=​ ​0​;​ level ​<​ length​;​ level​++)
​{
index ​=​ key​.​charAt​(​level​)​ ​-​ ​'a'​;
​if​ ​(​pCrawl​.​children​[​index​]​ ​==​ ​null​)
pCrawl​.​children​[​index​]​ ​=​ ​new​ TrieNode​();
pCrawl ​=​ pCrawl​.​children​[​index​];
​}
​// mark last node as leaf
pCrawl​.​isEndOfWord​ ​=​ ​true​;
​//Store the key at the leaf node
pCrawl​.​key​ ​=​ key​;
​}
​//This will print out Lexicographically sorted output.
​//To begin with, pass pCrawl as root
​static​ ​void​ ​preorder​(​TrieNode pCrawl ​){
​//exit condition
​if​(​pCrawl ​==​ ​null​){
​return​;
​}
​for​(​int​ index ​=​ ​0​;​ index​<​ ​26​ ​;​index​++){
if​(​pCrawl​.​children​[​index​]​ ​!=​ ​null​){
if​(​pCrawl​.​children​[​index​].​key​ ​!=​ ​null​){
System​.​out​.​println​(​pCrawl​.​children​[​index​].​key​);
}
preorder​(​pCrawl​.​children​[​index​]);
}
}
​}
}
//Class Main
package​ ​com.somitsolutions.java.training.wordsindictionaryform​;
import​ ​java.util.StringTokenizer​;
public​ ​class​ ​Main​ ​{
static​ String keys​[]​ ​=​ ​new​ String​[​1000​];
public​ ​static​ ​void​ ​main​(​String​[]​ args​)​ ​{
// TODO Auto-generated method stub
String inputStr ​=​ ​"This is a lamb. This is white in color."​;
String refinedStr ​= inputStr​.​replaceAll​(​"[^a-zA-Z -]"​,
""​).​toLowerCase​();
dictionary​(​refinedStr​);
}
​private​ ​static​ ​void​ ​dictionary​(​String in​){
StringTokenizer st ​=​ ​new​ StringTokenizer​(​in​,​ ​" "​);
int​ i ​=​ ​0​;
Trie​.​root​ ​=​ ​new​ Trie​.​TrieNode​();
while​ ​(​st​.​hasMoreTokens​()){
String strTemp ​=​ st​.​nextToken​();
Trie​.​insert​(​strTemp​);
i​++;
}
Trie​.​preorder​(​Trie​.​root​);
}
}

Mais conteúdo relacionado

Mais procurados

Lesson 03 python statement, indentation and comments
Lesson 03   python statement, indentation and commentsLesson 03   python statement, indentation and comments
Lesson 03 python statement, indentation and commentsNilimesh Halder
 
Compiler Designs
Compiler DesignsCompiler Designs
Compiler Designswasim liam
 
Welcome to lecture 4 in c programming
Welcome to lecture 4 in c programmingWelcome to lecture 4 in c programming
Welcome to lecture 4 in c programmingumair ansari
 
Mc0082 theory of computer science
Mc0082  theory of computer scienceMc0082  theory of computer science
Mc0082 theory of computer sciencesmumbahelp
 
Fundamentals of Computing and C Programming - Part 2
Fundamentals of Computing and C Programming - Part 2Fundamentals of Computing and C Programming - Part 2
Fundamentals of Computing and C Programming - Part 2Karthik Srini B R
 
Input processing and output in Python
Input processing and output in PythonInput processing and output in Python
Input processing and output in PythonRaajendra M
 
Doppl development iteration #4
Doppl development   iteration #4Doppl development   iteration #4
Doppl development iteration #4Diego Perini
 
Std 10 computer chapter 10 introduction to c language (part2)
Std 10 computer chapter 10 introduction to c language (part2)Std 10 computer chapter 10 introduction to c language (part2)
Std 10 computer chapter 10 introduction to c language (part2)Nuzhat Memon
 
Gentle Introduction To Funcitonal Programming - Detroit.Code
Gentle Introduction To Funcitonal Programming - Detroit.CodeGentle Introduction To Funcitonal Programming - Detroit.Code
Gentle Introduction To Funcitonal Programming - Detroit.CodeOnorioCatenacci
 
Basic data types in python
Basic data types in pythonBasic data types in python
Basic data types in pythonsunilchute1
 
Cs6503 theory of computation book notes
Cs6503 theory of computation book notesCs6503 theory of computation book notes
Cs6503 theory of computation book notesappasami
 
Usage of regular expressions in nlp
Usage of regular expressions in nlpUsage of regular expressions in nlp
Usage of regular expressions in nlpeSAT Journals
 
Variables in C and C++ Language
Variables in C and C++ LanguageVariables in C and C++ Language
Variables in C and C++ LanguageWay2itech
 
1.getting started with c
1.getting started with c1.getting started with c
1.getting started with cHardik gupta
 
Finite state system or finite automata
Finite state system or finite automataFinite state system or finite automata
Finite state system or finite automataDr. ABHISHEK K PANDEY
 

Mais procurados (20)

Simple uml
Simple umlSimple uml
Simple uml
 
Lesson 03 python statement, indentation and comments
Lesson 03   python statement, indentation and commentsLesson 03   python statement, indentation and comments
Lesson 03 python statement, indentation and comments
 
Compiler Designs
Compiler DesignsCompiler Designs
Compiler Designs
 
Welcome to lecture 4 in c programming
Welcome to lecture 4 in c programmingWelcome to lecture 4 in c programming
Welcome to lecture 4 in c programming
 
Mc0082 theory of computer science
Mc0082  theory of computer scienceMc0082  theory of computer science
Mc0082 theory of computer science
 
Fundamentals of Computing and C Programming - Part 2
Fundamentals of Computing and C Programming - Part 2Fundamentals of Computing and C Programming - Part 2
Fundamentals of Computing and C Programming - Part 2
 
Input processing and output in Python
Input processing and output in PythonInput processing and output in Python
Input processing and output in Python
 
Doppl development iteration #4
Doppl development   iteration #4Doppl development   iteration #4
Doppl development iteration #4
 
Std 10 computer chapter 10 introduction to c language (part2)
Std 10 computer chapter 10 introduction to c language (part2)Std 10 computer chapter 10 introduction to c language (part2)
Std 10 computer chapter 10 introduction to c language (part2)
 
3.5
3.53.5
3.5
 
Gentle Introduction To Funcitonal Programming - Detroit.Code
Gentle Introduction To Funcitonal Programming - Detroit.CodeGentle Introduction To Funcitonal Programming - Detroit.Code
Gentle Introduction To Funcitonal Programming - Detroit.Code
 
Basic data types in python
Basic data types in pythonBasic data types in python
Basic data types in python
 
Cs6503 theory of computation book notes
Cs6503 theory of computation book notesCs6503 theory of computation book notes
Cs6503 theory of computation book notes
 
Tech tut
Tech tutTech tut
Tech tut
 
Usage of regular expressions in nlp
Usage of regular expressions in nlpUsage of regular expressions in nlp
Usage of regular expressions in nlp
 
Variables in C and C++ Language
Variables in C and C++ LanguageVariables in C and C++ Language
Variables in C and C++ Language
 
Usage of regular expressions in nlp
Usage of regular expressions in nlpUsage of regular expressions in nlp
Usage of regular expressions in nlp
 
1.getting started with c
1.getting started with c1.getting started with c
1.getting started with c
 
Lecture02(constants, variable & data types)
Lecture02(constants, variable & data types)Lecture02(constants, variable & data types)
Lecture02(constants, variable & data types)
 
Finite state system or finite automata
Finite state system or finite automataFinite state system or finite automata
Finite state system or finite automata
 

Semelhante a Arranging the words of a text lexicographically trie

Introduction to Erlang
Introduction to ErlangIntroduction to Erlang
Introduction to ErlangRaymond Tay
 
Adding gift questions in swayam 2.0
Adding gift questions  in swayam 2.0Adding gift questions  in swayam 2.0
Adding gift questions in swayam 2.0aschrdc
 
Architecting Scalable Platforms in Erlang/OTP | Hamidreza Soleimani | Diginex...
Architecting Scalable Platforms in Erlang/OTP | Hamidreza Soleimani | Diginex...Architecting Scalable Platforms in Erlang/OTP | Hamidreza Soleimani | Diginex...
Architecting Scalable Platforms in Erlang/OTP | Hamidreza Soleimani | Diginex...Hamidreza Soleimani
 
N E T Coding Best Practices
N E T  Coding  Best  PracticesN E T  Coding  Best  Practices
N E T Coding Best PracticesAbhishek Desai
 
Questions4
Questions4Questions4
Questions4hccit
 
Washington Practitioners Significant Changes To Rpc 1.5
Washington Practitioners Significant Changes To Rpc 1.5Washington Practitioners Significant Changes To Rpc 1.5
Washington Practitioners Significant Changes To Rpc 1.5Oregon Law Practice Management
 
Paulo Freire Pedagpogia 1
Paulo Freire Pedagpogia 1Paulo Freire Pedagpogia 1
Paulo Freire Pedagpogia 1Alejandra Perez
 
Jerry Shea Resume And Addendum 5 2 09
Jerry  Shea Resume And Addendum 5 2 09Jerry  Shea Resume And Addendum 5 2 09
Jerry Shea Resume And Addendum 5 2 09gshea11
 
Majlis Persaraan Pn.Hjh.Normah bersama guru-guru Sesi Petang
Majlis Persaraan Pn.Hjh.Normah bersama guru-guru Sesi PetangMajlis Persaraan Pn.Hjh.Normah bersama guru-guru Sesi Petang
Majlis Persaraan Pn.Hjh.Normah bersama guru-guru Sesi PetangImsamad
 
Agapornis Mansos - www.criadourosudica.blogspot.com
Agapornis Mansos - www.criadourosudica.blogspot.comAgapornis Mansos - www.criadourosudica.blogspot.com
Agapornis Mansos - www.criadourosudica.blogspot.comAntonio Silva
 
Coding Best Practices
Coding Best PracticesCoding Best Practices
Coding Best Practicesmh_azad
 
Notes1
Notes1Notes1
Notes1hccit
 
Data Structures- Part1 overview and review
Data Structures- Part1 overview and reviewData Structures- Part1 overview and review
Data Structures- Part1 overview and reviewAbdullah Al-hazmy
 
Python breakdown-workbook
Python breakdown-workbookPython breakdown-workbook
Python breakdown-workbookHARUN PEHLIVAN
 
Regular expressions
Regular expressionsRegular expressions
Regular expressionsRaghu nath
 

Semelhante a Arranging the words of a text lexicographically trie (20)

Introduction to Erlang
Introduction to ErlangIntroduction to Erlang
Introduction to Erlang
 
Adding gift questions in swayam 2.0
Adding gift questions  in swayam 2.0Adding gift questions  in swayam 2.0
Adding gift questions in swayam 2.0
 
Architecting Scalable Platforms in Erlang/OTP | Hamidreza Soleimani | Diginex...
Architecting Scalable Platforms in Erlang/OTP | Hamidreza Soleimani | Diginex...Architecting Scalable Platforms in Erlang/OTP | Hamidreza Soleimani | Diginex...
Architecting Scalable Platforms in Erlang/OTP | Hamidreza Soleimani | Diginex...
 
N E T Coding Best Practices
N E T  Coding  Best  PracticesN E T  Coding  Best  Practices
N E T Coding Best Practices
 
Questions4
Questions4Questions4
Questions4
 
MMBJ Shanzhai Culture
MMBJ Shanzhai CultureMMBJ Shanzhai Culture
MMBJ Shanzhai Culture
 
Washington Practitioners Significant Changes To Rpc 1.5
Washington Practitioners Significant Changes To Rpc 1.5Washington Practitioners Significant Changes To Rpc 1.5
Washington Practitioners Significant Changes To Rpc 1.5
 
Paulo Freire Pedagpogia 1
Paulo Freire Pedagpogia 1Paulo Freire Pedagpogia 1
Paulo Freire Pedagpogia 1
 
Jerry Shea Resume And Addendum 5 2 09
Jerry  Shea Resume And Addendum 5 2 09Jerry  Shea Resume And Addendum 5 2 09
Jerry Shea Resume And Addendum 5 2 09
 
Majlis Persaraan Pn.Hjh.Normah bersama guru-guru Sesi Petang
Majlis Persaraan Pn.Hjh.Normah bersama guru-guru Sesi PetangMajlis Persaraan Pn.Hjh.Normah bersama guru-guru Sesi Petang
Majlis Persaraan Pn.Hjh.Normah bersama guru-guru Sesi Petang
 
Agapornis Mansos - www.criadourosudica.blogspot.com
Agapornis Mansos - www.criadourosudica.blogspot.comAgapornis Mansos - www.criadourosudica.blogspot.com
Agapornis Mansos - www.criadourosudica.blogspot.com
 
LoteríA Correcta
LoteríA CorrectaLoteríA Correcta
LoteríA Correcta
 
Rubykin
Rubykin Rubykin
Rubykin
 
Programming Fundamentals
Programming FundamentalsProgramming Fundamentals
Programming Fundamentals
 
012. SQL.pdf
012. SQL.pdf012. SQL.pdf
012. SQL.pdf
 
Coding Best Practices
Coding Best PracticesCoding Best Practices
Coding Best Practices
 
Notes1
Notes1Notes1
Notes1
 
Data Structures- Part1 overview and review
Data Structures- Part1 overview and reviewData Structures- Part1 overview and review
Data Structures- Part1 overview and review
 
Python breakdown-workbook
Python breakdown-workbookPython breakdown-workbook
Python breakdown-workbook
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
 

Mais de Somenath Mukhopadhyay

Significance of private inheritance in C++...
Significance of private inheritance in C++...Significance of private inheritance in C++...
Significance of private inheritance in C++...Somenath Mukhopadhyay
 
Generic asynchronous HTTP utility for android
Generic asynchronous HTTP utility for androidGeneric asynchronous HTTP utility for android
Generic asynchronous HTTP utility for androidSomenath Mukhopadhyay
 
Java concurrency model - The Future Task
Java concurrency model - The Future TaskJava concurrency model - The Future Task
Java concurrency model - The Future TaskSomenath Mukhopadhyay
 
Memory layout in C++ vis a-vis polymorphism and padding bits
Memory layout in C++ vis a-vis polymorphism and padding bitsMemory layout in C++ vis a-vis polymorphism and padding bits
Memory layout in C++ vis a-vis polymorphism and padding bitsSomenath Mukhopadhyay
 
Developing an Android REST client to determine POI using asynctask and integr...
Developing an Android REST client to determine POI using asynctask and integr...Developing an Android REST client to determine POI using asynctask and integr...
Developing an Android REST client to determine POI using asynctask and integr...Somenath Mukhopadhyay
 
How to create your own background for google docs
How to create your own background for google docsHow to create your own background for google docs
How to create your own background for google docsSomenath Mukhopadhyay
 
The Designing of a Software System from scratch with the help of OOAD & UML -...
The Designing of a Software System from scratch with the help of OOAD & UML -...The Designing of a Software System from scratch with the help of OOAD & UML -...
The Designing of a Software System from scratch with the help of OOAD & UML -...Somenath Mukhopadhyay
 
Structural Relationship between Content Resolver and Content Provider of Andr...
Structural Relationship between Content Resolver and Content Provider of Andr...Structural Relationship between Content Resolver and Content Provider of Andr...
Structural Relationship between Content Resolver and Content Provider of Andr...Somenath Mukhopadhyay
 
Flow of events during Media Player creation in Android
Flow of events during Media Player creation in AndroidFlow of events during Media Player creation in Android
Flow of events during Media Player creation in AndroidSomenath Mukhopadhyay
 
Implementation of a state machine for a longrunning background task in androi...
Implementation of a state machine for a longrunning background task in androi...Implementation of a state machine for a longrunning background task in androi...
Implementation of a state machine for a longrunning background task in androi...Somenath Mukhopadhyay
 
Tackling circular dependency in Java
Tackling circular dependency in JavaTackling circular dependency in Java
Tackling circular dependency in JavaSomenath Mukhopadhyay
 
Implementation of composite design pattern in android view and widgets
Implementation of composite design pattern in android view and widgetsImplementation of composite design pattern in android view and widgets
Implementation of composite design pattern in android view and widgetsSomenath Mukhopadhyay
 
Exception Handling in the C++ Constructor
Exception Handling in the C++ ConstructorException Handling in the C++ Constructor
Exception Handling in the C++ ConstructorSomenath Mukhopadhyay
 
Active object of Symbian in the lights of client server architecture
Active object of Symbian in the lights of client server architectureActive object of Symbian in the lights of client server architecture
Active object of Symbian in the lights of client server architectureSomenath Mukhopadhyay
 
Android Asynctask Internals vis-a-vis half-sync half-async design pattern
Android Asynctask Internals vis-a-vis half-sync half-async design patternAndroid Asynctask Internals vis-a-vis half-sync half-async design pattern
Android Asynctask Internals vis-a-vis half-sync half-async design patternSomenath Mukhopadhyay
 

Mais de Somenath Mukhopadhyay (20)

Significance of private inheritance in C++...
Significance of private inheritance in C++...Significance of private inheritance in C++...
Significance of private inheritance in C++...
 
Generic asynchronous HTTP utility for android
Generic asynchronous HTTP utility for androidGeneric asynchronous HTTP utility for android
Generic asynchronous HTTP utility for android
 
Copy on write
Copy on writeCopy on write
Copy on write
 
Java concurrency model - The Future Task
Java concurrency model - The Future TaskJava concurrency model - The Future Task
Java concurrency model - The Future Task
 
Memory layout in C++ vis a-vis polymorphism and padding bits
Memory layout in C++ vis a-vis polymorphism and padding bitsMemory layout in C++ vis a-vis polymorphism and padding bits
Memory layout in C++ vis a-vis polymorphism and padding bits
 
Developing an Android REST client to determine POI using asynctask and integr...
Developing an Android REST client to determine POI using asynctask and integr...Developing an Android REST client to determine POI using asynctask and integr...
Developing an Android REST client to determine POI using asynctask and integr...
 
Observer pattern
Observer patternObserver pattern
Observer pattern
 
Uml training
Uml trainingUml training
Uml training
 
How to create your own background for google docs
How to create your own background for google docsHow to create your own background for google docs
How to create your own background for google docs
 
The Designing of a Software System from scratch with the help of OOAD & UML -...
The Designing of a Software System from scratch with the help of OOAD & UML -...The Designing of a Software System from scratch with the help of OOAD & UML -...
The Designing of a Software System from scratch with the help of OOAD & UML -...
 
Structural Relationship between Content Resolver and Content Provider of Andr...
Structural Relationship between Content Resolver and Content Provider of Andr...Structural Relationship between Content Resolver and Content Provider of Andr...
Structural Relationship between Content Resolver and Content Provider of Andr...
 
Flow of events during Media Player creation in Android
Flow of events during Media Player creation in AndroidFlow of events during Media Player creation in Android
Flow of events during Media Player creation in Android
 
Implementation of a state machine for a longrunning background task in androi...
Implementation of a state machine for a longrunning background task in androi...Implementation of a state machine for a longrunning background task in androi...
Implementation of a state machine for a longrunning background task in androi...
 
Tackling circular dependency in Java
Tackling circular dependency in JavaTackling circular dependency in Java
Tackling circular dependency in Java
 
Implementation of composite design pattern in android view and widgets
Implementation of composite design pattern in android view and widgetsImplementation of composite design pattern in android view and widgets
Implementation of composite design pattern in android view and widgets
 
Exception Handling in the C++ Constructor
Exception Handling in the C++ ConstructorException Handling in the C++ Constructor
Exception Handling in the C++ Constructor
 
Active object of Symbian in the lights of client server architecture
Active object of Symbian in the lights of client server architectureActive object of Symbian in the lights of client server architecture
Active object of Symbian in the lights of client server architecture
 
Android services internals
Android services internalsAndroid services internals
Android services internals
 
Android Asynctask Internals vis-a-vis half-sync half-async design pattern
Android Asynctask Internals vis-a-vis half-sync half-async design patternAndroid Asynctask Internals vis-a-vis half-sync half-async design pattern
Android Asynctask Internals vis-a-vis half-sync half-async design pattern
 
Composite Pattern
Composite PatternComposite Pattern
Composite Pattern
 

Último

%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisamasabamasaba
 
WSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaSWSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaSWSO2
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfonteinmasabamasaba
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrandmasabamasaba
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...masabamasaba
 
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benonimasabamasaba
 
What Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationWhat Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationJuha-Pekka Tolvanen
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisamasabamasaba
 
tonesoftg
tonesoftgtonesoftg
tonesoftglanshi9
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...SelfMade bd
 
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...chiefasafspells
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...Jittipong Loespradit
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastPapp Krisztián
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in sowetomasabamasaba
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2
 
Artyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxArtyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxAnnaArtyushina1
 
WSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security ProgramWSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security ProgramWSO2
 

Último (20)

%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
WSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaSWSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaS
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go Platformless
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
 
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
 
What Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationWhat Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the Situation
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
tonesoftg
tonesoftgtonesoftg
tonesoftg
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the past
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
 
Artyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxArtyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptx
 
WSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security ProgramWSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security Program
 

Arranging the words of a text lexicographically trie

  • 1. SOM-ITSOLUTIONS ALGORITHM Arranging the words of an Input Text Lexicographically - Trie SOMENATH MUKHOPADHYAY som-itsolutions  #A2 1/13 South Purbachal Hospital Road Kolkata 700078 Mob: +91 9748185282 Email: ​som.mukhopadhyay@som-itsolutions.com​ / ​som.mukhopadhyay@gmail.com Website: ​http://www.som-itsolutions.com/ Blog: ​www.som-itsolutions.blogspot.com
  • 2. As i am trying to recapitulate my knowledge base vis-a-vis data structure and algorithm, i am trying to solve various practical real life problems. The real aim is to prepare my students for programming olympiad. Thus while browsing through the Indian Association for Research in Computing Science, i got a this problem : “ Problem : Word List In this problem the input will consist of a number of lines of English text consisting of the letters of the English alphabet, the punctuation marks ' (apostrophe), . (full stop), , (comma), ; (semicolon), :(colon) and white space characters (blank, newline). Your task is print the words in the text in lexicographic order (that is, dictionary order). Each word should appear exactly once in your list. You can ignore the case (for instance, "The" and "the" are to be treated as the same word.) There should be no uppercase letters in the output. For example, consider the following candidate for the input text: This is a sample piece of text to illustrate this problem. The corresponding output would read as: a illustrate is of piece problem sample text this to ” So i thought to use the concept of Trie or Prefix Tree to come out with a solution. Trie is a special kind of Tree Data Structure which is very useful for Dictionary kind of application. It has the in built properties of Prefix matching, neighbor search etc. The Trie data structure is as shown below:
  • 3. With the given problem, we first remove the punctuation and make all lowercase of the input text. Then we can tokenize the string and get different words. We then put these words in a Trie. After that if we traverse the Trie Preorderly, we will get the desired output. The whole solution looks like the following. Class Trie package​ ​com.somitsolutions.java.training.wordsindictionaryform​; public​ ​class​ ​Trie​ ​{ ​// Alphabet size (# of symbols) ​static​ ​final​ ​int​ ALPHABET_SIZE ​=​ ​26​; ​// trie node ​static​ ​class​ ​TrieNode ​{ TrieNode​[]​ children ​=​ ​new​ TrieNode​[​ALPHABET_SIZE​];
  • 4. ​// isEndOfWord is true if the node represents ​// end of a word ​boolean​ isEndOfWord​; String key​;​//Store the TrieNode​(){ key ​=​ ​null​; isEndOfWord ​=​ ​false​; ​for​ ​(​int​ i ​=​ ​0​;​ i ​<​ ALPHABET_SIZE​;​ i​++) children​[​i​]​ ​=​ ​null​; ​} ​} ​static​ TrieNode root​; ​// If not present, inserts key into trie ​// If the key is prefix of trie node, ​// just marks leaf node ​static​ ​void​ ​insert​(​String key​) ​{ ​int​ level​; ​int​ length ​=​ key​.​length​(); ​int​ index​; TrieNode pCrawl ​=​ root​; ​for​ ​(​level ​=​ ​0​;​ level ​<​ length​;​ level​++) ​{ index ​=​ key​.​charAt​(​level​)​ ​-​ ​'a'​; ​if​ ​(​pCrawl​.​children​[​index​]​ ​==​ ​null​) pCrawl​.​children​[​index​]​ ​=​ ​new​ TrieNode​(); pCrawl ​=​ pCrawl​.​children​[​index​]; ​} ​// mark last node as leaf pCrawl​.​isEndOfWord​ ​=​ ​true​; ​//Store the key at the leaf node pCrawl​.​key​ ​=​ key​; ​} ​//This will print out Lexicographically sorted output. ​//To begin with, pass pCrawl as root ​static​ ​void​ ​preorder​(​TrieNode pCrawl ​){ ​//exit condition ​if​(​pCrawl ​==​ ​null​){ ​return​;
  • 5. ​} ​for​(​int​ index ​=​ ​0​;​ index​<​ ​26​ ​;​index​++){ if​(​pCrawl​.​children​[​index​]​ ​!=​ ​null​){ if​(​pCrawl​.​children​[​index​].​key​ ​!=​ ​null​){ System​.​out​.​println​(​pCrawl​.​children​[​index​].​key​); } preorder​(​pCrawl​.​children​[​index​]); } } ​} } //Class Main package​ ​com.somitsolutions.java.training.wordsindictionaryform​; import​ ​java.util.StringTokenizer​; public​ ​class​ ​Main​ ​{ static​ String keys​[]​ ​=​ ​new​ String​[​1000​]; public​ ​static​ ​void​ ​main​(​String​[]​ args​)​ ​{ // TODO Auto-generated method stub String inputStr ​=​ ​"This is a lamb. This is white in color."​; String refinedStr ​= inputStr​.​replaceAll​(​"[^a-zA-Z -]"​, ""​).​toLowerCase​(); dictionary​(​refinedStr​); } ​private​ ​static​ ​void​ ​dictionary​(​String in​){ StringTokenizer st ​=​ ​new​ StringTokenizer​(​in​,​ ​" "​); int​ i ​=​ ​0​; Trie​.​root​ ​=​ ​new​ Trie​.​TrieNode​(); while​ ​(​st​.​hasMoreTokens​()){ String strTemp ​=​ st​.​nextToken​(); Trie​.​insert​(​strTemp​); i​++;