Android Open Source - WordOfTheDay Dictionary Trie






From Project

Back to project page WordOfTheDay.

License

The source code is released under:

MIT License

If you think the Android project WordOfTheDay listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.aferla.wotd.dictionary;
/*from  w  ww.j a  va 2  s .  c o  m*/
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
 *
 */
public class DictionaryTrie {

    private TrieNode treeRoot;

    public DictionaryTrie(){
        treeRoot = new TrieNode('\0');
    }

    /**
     *
     * @return
     */
    public int size (){
        return treeRoot.getSize();
    }

    //TODO: Experiment with Recursion
    public void addWord (String word){
        TrieNode current = treeRoot;

        for (Character c  : word.toCharArray()){
            if (!current.hasChild(c)){
                current.addChild(c);
            }

            //TODO: Should addChild instead return a reference to this?
            current = current.findChildNode(c);
        }
    }

    public TrieNode getRoot(){
        return treeRoot;
    }

    /**
     * Fix
     * @param prefix
     * @param rootNode
     * @return
     */
    public TrieNode findByPrefix (String prefix, TrieNode rootNode){
        if (prefix == null || prefix.isEmpty()){
            return rootNode;
        }

        if (rootNode.hasChild(prefix.charAt(0))){
            TrieNode nextRoot = rootNode.findChildNode(prefix.charAt(0));
            if (prefix.length() >= 2){
                return findByPrefix(prefix.substring(1), nextRoot);
            }
            else{
                return findByPrefix("",nextRoot);
            }
        }

        return null;
    }

    public List<String> findByPrefix(String prefix){
        TrieNode trieNode = findByPrefix(prefix,treeRoot);
        if (trieNode != null){
            return trieNode.getPossibleStrings(prefix);
        }

        return Collections.emptyList();
    }


    /**
     * A Node in the Trie.
     *
     * Represents a Character and its children
     *
     */
    public static class TrieNode{

        private Character c;
        private boolean useHashmap;

        //TODO: Is a map the best for this?
        private Map<Character,TrieNode> children;


        /**
         * Constructor
         * @param c The character that this TrieNode contains
         */
        public TrieNode(Character c){
            this.c  = c;
            children = new TreeMap<>();
        }

        public void addChild (Character c){
            children.put(c, new TrieNode(c));
        }

        public Boolean hasChild (Character c){
            return children.containsKey(c);
        }

        public TrieNode findChildNode (Character c){
            return children.get(c);
        }

        public Integer getSize (){
            if (children.isEmpty()){
                return 0;
            }

            int size = children.size();
            for (TrieNode node : children.values()){
                size += node.getSize();
            }

            return size;
        }

        private List<String> getPossibleStrings (String acc){
            List<String> strings = new ArrayList<String>();

            if (children == null || children.isEmpty()){
                return Collections.singletonList(acc + c.toString());
            }

            if (children.size() > 0){
                strings.add(acc + c.toString());
            }

            for (Map.Entry<Character,TrieNode> node : children.entrySet()){
                strings.addAll(node.getValue().getPossibleStrings(acc + c.toString()));
            }

            return strings;
        }

        public List<String> getPossibleStrings (){
            return getPossibleStrings("");
        }

    }

}




Java Source Code List

com.adamf.wotd.WotdMainActivity.java
com.aferla.wotd.dictionary.DictionaryTrie.java
com.aferla.wotd.dictionary.TrieNodeHashmapExperimentTmp.java