-
-
Save intothedeep/a76001bb30c63100d6705e3ca759d719 to your computer and use it in GitHub Desktop.
Example of implementing a trie. Includes inserting and searching the trie (deletion not included.)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.*; | |
public class Trie | |
{ | |
private TrieNode root; | |
public Trie() | |
{ | |
root = new TrieNode( ' ' ); | |
} | |
public void insert( String s ) | |
{ | |
/* Begin at root */ | |
TrieNode current = root; | |
if( s.length() == 0 ) | |
current.marker = true; | |
/* Add the word */ | |
for( int i = 0; i < s.length(); i++ ) { | |
/* See if the letter is already in the trie */ | |
TrieNode child = current.subNode( s.charAt(i) ); | |
if( child != null ) | |
/* If the letter was in the trie, switch to it and continue */ | |
current = child; | |
else { | |
/* If the letter wasn't in the trie, add it */ | |
current.child.add( new TrieNode( s.charAt(i) ) ); | |
/* Switch to the new node */ | |
current = current.subNode( s.charAt(i) ); | |
} | |
if( i == s.length() - 1 ) | |
current.marker = true; | |
} | |
} | |
public boolean search( String s ) | |
{ | |
/* Begin at the root */ | |
TrieNode current = root; | |
while( current != null ) { | |
for( int i = 0; i < s.length(); i++ ) { | |
if( current.subNode( s.charAt(i) ) == null ) { | |
return false; | |
} else { | |
current = current.subNode( s.charAt( i ) ); | |
} | |
} | |
if( current.marker = true ) | |
return true; | |
else | |
return false; | |
} | |
return false; | |
} | |
public void run() throws IOException | |
{ | |
String s = " "; | |
BufferedReader br = new BufferedReader( new InputStreamReader( System.in ) ); | |
while( s.length() > 0 && s.charAt(0) != 'q' ) { | |
System.out.println( "Please enter a new word to be added to the trie:" ); | |
s = br.readLine(); | |
if( s.charAt(0) != 'q') { | |
insert( s ); | |
} | |
} | |
s = " "; | |
while( s.length() > 0 && s.charAt(0) != 'q' ) { | |
System.out.println( "Please enter a new word to search for in the trie:" ); | |
s = br.readLine(); | |
if( s.charAt(0) != 'q') { | |
if( search( s ) ) { | |
System.out.println( "Found " + s ); | |
} else { | |
System.out.println( "Couldn't find " + s ); | |
} | |
} | |
} | |
} | |
public static void main( String[] args ) throws IOException | |
{ | |
Trie t = new Trie(); | |
t.run(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Collection; | |
import java.util.LinkedList; | |
public class TrieNode | |
{ | |
char content; | |
boolean marker; | |
Collection<TrieNode> child; | |
public TrieNode( char c ) | |
{ | |
this.child = new LinkedList<TrieNode>(); | |
this.marker = false; | |
this.content = c; | |
} | |
public TrieNode subNode( char c ) | |
{ | |
if( child != null ) { | |
for( TrieNode eachChild : child ) { | |
if( eachChild.content == c ) { | |
return eachChild; | |
} | |
} | |
} | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment