`

字典树相关代码

阅读更多
package com.myway.study;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 字典树 城市相关查询 (现针对26个英文字母)
 * User: zhangyong
 * Date: 14-8-10
 * Time: 上午11:21
 * To change this template use File | Settings | File Templates.
 */
public class DictionaryTree {

    private static char SPACE = ' ';

    private static char[] BASE_CHARS = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};

    private TrieNode root = null;

    public DictionaryTree() {
        root = new TrieNode(SPACE);
    }

    public class TrieNode {

        private TrieNode[] childrenNodes = null;

        private char charValue;

        private boolean wordEnd = false;

        public TrieNode(char charValue) {
            this.charValue = charValue;
            childrenNodes = new TrieNode[26];
        }

        public void setWordEnd(boolean wordEnd) {
            this.wordEnd = wordEnd;
        }

        public boolean isWordEnd() {
            return wordEnd;
        }
    }

    public void insert(String word) {
        char[] charArray = word.toCharArray();
        TrieNode currentNode = root;
        for (char temp : charArray) {
            int index = (temp - 'a');
            TrieNode trieNode = currentNode.childrenNodes[index];
            if (trieNode == null) {
                trieNode = new TrieNode(temp);
                currentNode.childrenNodes[index] = trieNode;
            }
            currentNode = trieNode;
        }
        currentNode.setWordEnd(true);                                 //是一个单词
    }

    public boolean search(String key) {
        boolean flag = true;
        char[] charArray = key.toCharArray();
        TrieNode currentNode = root;
        for (char temp : charArray) {
            int index = (temp - 'a');
            TrieNode trieNode = currentNode.childrenNodes[index];
            if (trieNode == null) {
                flag = false;
                break;
            } else {
                currentNode = trieNode;
            }
        }
        return flag;
    }

    public Set<String> travel(String key) {
        char[] charArray = key.toCharArray();
        TrieNode currentNode = root;
        boolean flag = true;
        for (char temp : charArray) {
            int index = (temp - 'a');
            TrieNode trieNode = currentNode.childrenNodes[index];
            if (trieNode == null) {
                flag = false;
                break;
            } else {
                currentNode = trieNode;
            }
        }
        if (flag) {
            Set<String> set = new HashSet<String>();
            TrieNode[] childrenNodes = currentNode.childrenNodes;
            for (TrieNode child : childrenNodes) {
                if (child != null) {
                    Recursion(child, key, set);
                }
            }
            return set;
        }
        return null;
    }

    public void Recursion(TrieNode trieNode, String append, Set<String> set) {
        TrieNode currentNode = trieNode;
        if (currentNode != null) {
            String temp = append + currentNode.charValue;
            if (currentNode.isWordEnd()) {
                set.add(temp);
            }
            TrieNode[] childrenNodes = currentNode.childrenNodes;
            for (TrieNode child : childrenNodes) {
                if (child != null) {
                    Recursion(child, temp, set);
                }
            }
        }
    }

    public static void main(String[] args) {
        //比如查询 b 输出 beijing beidaihe  baoding
        DictionaryTree dictionaryTree = new DictionaryTree();
        dictionaryTree.insert("shanghai");
        dictionaryTree.insert("shenzhen");
        dictionaryTree.insert("beijing");
        dictionaryTree.insert("beidaihe");
        dictionaryTree.insert("baoding");
        dictionaryTree.insert("guangzhou");
        dictionaryTree.insert("hangzhou");
        System.out.println(dictionaryTree.search("beijing"));
        System.out.println(dictionaryTree.travel("b"));
        System.out.println(dictionaryTree.travel("guang"));
    }


}


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics