Trie树

DBC 1.3K 0

Trie树插图

什么是Trie字典树

  • 字典
    • 如果有n个条目
    • 使用树结构
    • 查询的时间复杂度是O(logn)
    • 如果有100万个条目(2²º)
    • log大约为20
  • Trie
    • 查询每个条目的时间复杂度
    • 和字典中一共有多少条目无关
    • 时间复杂度为O(w)
    • w为查询单词的长度
    • 大多数单词的长度小于10

每个节点有26个指向下个节点的指针

温馨提示

考虑不同的语言,不同的情景:
每个节点有若干指向下个节点的指针 [aru_42]

二、Trie字典树基础

package Trie;

import java.util.TreeMap;

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;
    }

    // 获得Trie中存储的单词数量
    public int getSize() {
        return size;
    }

    // 向Trie中添加一个新的单词word
    public void add(String word) {
        Node cur = root;
        for (int i = 0; i < word.length(); i++) {
            char c = word.charAt(i);
            if (cur.next.get(c) == null) {
                cur.next.put(c, new Node());
            }
            cur = cur.next.get(c);
        }
        if (!cur.isWord){
            cur.isWord = true;
            size++;
        }
        
    }
}

三、查询是否在Trie中有单词以prefix为前缀

    // 查询是否在Trie中有单词以prefix为前缀
    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;
    }

四、211. 添加与搜索单词 - 数据结构设计

Trie树插图2

package Trie;

import java.util.TreeMap;

public class WordDictionary {
    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;

    public WordDictionary() {
        root = new Node();
    }

    public void addWord(String word) {
        Node cur = root;
        for (int i = 0; i < word.length(); i++) {
            char c = word.charAt(i);
            if (cur.next.get(c) == null) {
                cur.next.put(c, new Node());
            }
            cur = cur.next.get(c);
        }
        if (!cur.isWord) {
            cur.isWord = true;
        }
    }

    public boolean search(String word) {
        return match(root, word, 0);
    }

    public boolean match(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 match(node.next.get(c), word, index + 1);
        }else {
            for (char nextChar: node.next.keySet()){
                if (match(node.next.get(nextChar),word,index+1)){
                    return true;
                }
            }
            return false;
        }
    }
}

Trie树插图4

五、Trie的局限性

  • 最大的问题:空间!
  • 压缩字典树: Compressed Trie
  • Trie树插图6

    • Ternary Search Trie (三分搜索字典树)
    • Trie树插图8

    • Trie树插图10

    • 更多字符串问题
      • 子串查询
        • 相关算法:KMP、Boyer-Moore、Babin-Karp
      • 文件压缩
      • 模式匹配

发表评论 取消回复
表情 图片 链接 代码

分享