Trie consumes less space so they are hot favourite data structures on PDAs or devices with less memory. Searching of a string in Trie takes around O(m) where m = length of the key. Compare it with Binary Tree which is O(logn) where n = number of elements in the tree. For a dictionary, this n could be real large.
Here is a simple implimentation of the Trie..
One class is called Node class which holds the Key and the value.
- public class Node {
- private char key;
- private String value;
- private List
children; - private boolean isTerminal;
- public boolean isTerminal() {
- return isTerminal;
- }
- public void setTerminal(boolean isTerminal) {
- this.isTerminal = isTerminal;
- }
- public Node(char key,String value)
- {
- this(key,value,new ArrayList());
- }
- public Node(char key, String value, List
children) { - super();
- this.key = key;
- this.value = value;
- this.children = children;
- }
- public char getKey() {
- return key;
- }
- public void setKey(char key) {
- this.key = key;
- }
- public String getValue() {
- return value;
- }
- public void setValue(String value) {
- this.value = value;
- }
- public List
getChildren() { - return children;
- }
- public void setChildren(List
children) { - this.children = children;
- }
- public void addChildren(Node child)
- {
- this.children.add(child);
- }
}
And the Main Trie class which builds the tree and finds any key...
package com.ds.trie;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.LinkedList;
- import java.util.List;
- import java.util.Queue;
- public class Trie {
- private Node root;
- private File fname;
- public Trie (String filename)
- {
- this.root = new Node('r',"root");
- this.fname = new File(filename);
- }
- public void buildTrie()
- {
- try {
- if (!fname.isFile())
- throw new FileNotFoundException("");
- BufferedReader f = new BufferedReader(new FileReader(fname));
- String key;
- try {
- while ((key = f.readLine()) != null)
- {
- // making value = full name only = key
- build(key,root,key);
- }
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- } catch (FileNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- // key = shamik
- private void build(String key,Node root,String value) {
- // TODO Auto-generated method stub
- char c = key.charAt(0); // c = s
- String tail = null;
- if (key.length() > 1)
- tail = key.substring(1); // hamik
- Node resultNode = findKeyinChildren(root,c); // find 's' in the children
- if (resultNode == null)
- {
- // this is a new key
- Node newNode = new Node(c,value);
- if (tail == null)
- newNode.setTerminal(true);
- else
- newNode.setTerminal(false);
- root.addChildren(newNode);
- if (tail == null)
- {
- return;
- }
- else
- build(tail,newNode,value);
- }
- else // got the key - s
- {
- if (tail == null)
- return;
- build (tail,resultNode,value);
- }
- }
- private Node findKeyinChildren(Node root, char c) {
- // TODO Auto-generated method stub
- List
list = root.getChildren(); - if (list == null || list.size() == 0)
- return null;
- for (int i=0; i < list.size(); i++)
- {
- Node oneNode = list.get(i);
- if (oneNode.getKey() == c)
- return oneNode;
- }
- return null;
- }
- public List
findKey(String key) - {
- String value = null;
- List
values = new ArrayList (); - Node resultNode = findinTrie(key,root);
- if (resultNode == null)
- {
- value = "Not found";
- values.add(value);
- }
- else
- {
- printTree(resultNode,values);
- }
- return values;
- }
- private void printTree(Node resultNode,List
values) { - // TODO Auto-generated method stub
- List
list = resultNode.getChildren(); - if (resultNode.isTerminal())
- values.add(resultNode.getValue());
- if (list.size() == 0)
- {
- return;
- }
- else
- {
- for (int i=0; i<list.size();i++)
- {
- Node oneNode = list.get(i);
- printTree(oneNode,values);
- }
- }
- }
- private Node findinTrie(String key, Node root) {
- // TODO Auto-generated method stub
- // Implementing the Breath First Traversal
- char first = key.charAt(0);
- String tail = null;
- if (key.length() > 1)
- tail = key.substring(1);
- Node resultNode = this.findKeyinChildren(root, first);
- if (resultNode == null)
- {
- return null;
- }
- else
- {
- if (tail == null)
- return resultNode;
- else
- resultNode = findinTrie(tail,resultNode );
- }
- return resultNode;
- }
- public static void main(String[] args)
- {
- if (args.length <= 0)
- {
- System.out.println("Usage : Trie
"); - System.exit(1);
- }
- Trie trie = new Trie("C:\\test\\dictionary");
- trie.buildTrie();
- System.out.println("Going to look for " + args[0]);
- List
vals = trie.findKey(args[0]); - for (int i=0; i<vals.size(); i++)
- {
- System.out.println("possible value = " + vals.get(i));
- }
- }
- }