Trie 前缀树 字典树 简介+实现

简介

在这里插入图片描述
最上面的是根结点, 这棵树中存的单词是apple, app, all, bat, 如果IsWord为True, 就说明从根节点连到这个结点的字母组成的是一个单词

使用前缀树查询的时候时间复杂度只和单词的长度相关

实现

import java.util.TreeMap;

/**
 * 存储只包含26个字母的单词
 */
public class Trie {

    private class Node{
        public boolean isWord;  // 是否是一个单词
        public TreeMap<Character, Node> next;  // 一个字母到其他字母结点的映射

        public Node(boolean isWord){
            this.isWord = isWord;
            next = new TreeMap<>();
        }

        public Node(){
            this(false);
        }
    }

    private Node root;
    private int size;

    public Trie(){
        root = new Node();
        size = 0;
    }

    public int getSize(){
        return size;
    }

    /**
     * 向Trie中添加一个新的单词word
     * @param word
     */
    public void add(String word){
        Node cur = root;
        for(int i=0; i<word.length(); i++){
            char c = word.charAt(i);
            // 当前结点不包含到word[i]的结点
            if(cur.next.get(c) == null){
                cur.next.put(c, new Node());  // 新建一个映射
            }
            cur = cur.next.get(c);
        }
        // 如果之前不是个词
        if(cur.isWord == false){
            cur.isWord = true;
            size++;
        }
    }

    /**
     * 查询到单词是否在Trie中
     * @param word
     * @return
     */
    public boolean contains(String word){
        Node cur = root;
        for(int i=0; i<word.length(); i++){
            char c = word.charAt(i);
            if(cur.next.get(c) == null){
                return false;
            }
            cur = cur.next.get(c);
        }
        return cur.isWord;
    }

    /**
     * 可以用 "." 去匹配任意一个字母
     * @param word
     * @return
     */
    public boolean search(String word){
        return search(root, word, 0);
//        return search(root, word);
    }

    /**
     *
     * @param node
     * @param word
     * @param index 表示从word的索引index开始匹配
     * @return
     */
    private boolean search(Node node, String word, int index) {
        // 已经匹配完
        if(index == word.length())
            return node.isWord;

        char c = word.charAt(index);
        if(c != '.'){
            // 如果已经没有下个结点了
            if(node.next.get(c) == null)
                return false;
            return search(node.next.get(c), word, index+1);
        }
        else{
            for(Node nextNode: node.next.values())
                if(search(nextNode, word, index+1))
                    return true;
            return false;  // 遍历完都没有匹配中的
        }
    }

    /**
     *
     * @param node
     * @param word
     * @return
     */
    private boolean search(Node node, String word) {
        if(word.equals("")){
            if(node.isWord){
                return true;
            }
            else{
                return false;
            }
        }

        if(word.charAt(0) == '.'){
            for(Node nextNode: node.next.values()){
                boolean res = search(nextNode, word.substring(1));
                if(res == true){
                    return true;
                }
            }
        }
        else if(node.next.get(word.charAt(0)) != null){
            return search(node.next.get(word.charAt(0)), word.substring(1));
        }
        return false;
    }

    /**
     * 查询是否存在Trie中有单词以prefix为前缀
     * @param prefix
     * @return
     */
    public boolean isPrefix(String prefix){
        Node cur = root;
        for(int i=0; i<prefix.length(); i++){
            char c = prefix.charAt(i);
            if(cur.next.get(c) == null){
                return false;
            }
            cur = cur.next.get(c);
        }
        return true;
    }



    public static void main(String[] args) {
        Trie t = new Trie();
        t.add("bad");
        t.add("pad");
        System.out.println(t.contains("bad"));
        System.out.println(t.isPrefix("ba"));
        System.out.println(t.search("ba."));
        System.out.println(t.search(".ad"));
        System.out.println(t.search(".a."));
        System.out.println(t.search("..."));
        System.out.println(t.search("p.a"));
        System.out.println(t.search("b."));
    }
}

练习

LeetCode 677
思路见注释
假如 insert(“apple”, 3), insert(“all”, 2)

class MapSum {

    private class Node{
        public int value;  // apple的最后一个Node e的value就是3, 前面Node a, p, p, l的value都是0
        public TreeMap<Character, Node> next;  // 如果Node是a, 那next就是Node p, Node l

        // 构造函数
        public Node(int value){
            this.value = value;
            next = new TreeMap<>();
        }

        // 构造函数
        public Node(){
            this(0);
        }
    }

    private Node root;  // 用上面的例子的话, root的next是Node a

    /** Initialize your data structure here. */
    public MapSum() {
        root = new Node();
    }

    public void insert(String key, int val) {
        Node cur = root;
        for(int i=0; i<key.length(); i++){
            char c = key.charAt(i);
            // 如果不存在这个字母的Node, 创建
            if(cur.next.get(c) == null)
                cur.next.put(c, new Node());
            // 移动
            cur = cur.next.get(c);
        }
        cur.value = val;
    }

    public int sum(String prefix) {
        Node cur = root;
        for(int i=0; i<prefix.length(); i++){
            char c = prefix.charAt(i);
            if(cur.next.get(c) == null)
                return 0;
            cur = cur.next.get(c);
        }
        return sum(cur);
    }

    private int sum(Node node){
        // if(node.next.size() == 0)
        //     return node.value;
        // 这个递归结束的语句已包含在下面

        int res = node.value;
        for(Node nextNode: node.next.values()){
            res += sum(nextNode);
        }
        return res;
    }

    public static void main(String[] args) {
        MapSum ms = new MapSum();
        ms.insert("apple", 3);
        ms.insert("app", 8);
        ms.insert("bao", 8);
//        ms.sum("ap");
        System.out.println(ms.sum("ap"));
    }
}

其他

局限性: 占用的空间会更多, 可以使用压缩字典树来解决, 但维护成本又会上去, 有得必有失
在这里插入图片描述
还可以用三分搜索树
在这里插入图片描述在这里插入图片描述
上面这棵树中存了单词dog

后面三张图来源 link

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章