数据结构之Trie字典树

什么是Trie字典树

Trie 树,也叫“字典树”或“前缀树”。顾名思义,它是一个树形结构。但与二分搜索树、红黑树等不一样的是,Trie 树是一种多叉树,即每一个节点能够有 m 个子节点。它是一种专门处理字符串匹配的数据结构,用来解决在一组字符串集合中快速查找某个字符串的问题。java

例如,在一个字典中有 $n$ 个条目,若是使用普通的二分搜索树(不考虑退化),那么在该字典中查询指定条目的时间复杂度是 $O(logn)$,若是有100w个条目($2^{20}$),$logn$ 大约为20。node

而若是使用 Trie 树的话,查询每一个条目的时间复杂度,和字典中一共有多少条目无关。时间复杂度为 $O(w)$,其中 $w$ 为查询单词的长度,并且绝大多数的单词长度都小于 10。因而可知,使用 Trie 树实现字符串查询,特别是只查询其前缀的状况下,是比普通的树形结构效率要更高的。算法

那么 Trie 树是如何作到其查询时间复杂度与条目数量无关的呢?这是由于 Trie 树的本质,就是利用字符串之间的公共前缀,将重复的前缀合并在一块儿。例如,咱们将:how,hi,her,hello,so,see 这6个字符串构形成一颗 Trie 树。那么,最后构造出来的就是下面这个图中的样子:
数据结构之Trie字典树数组

其中,根节点不包含任何信息。每一个节点表示一个字符串中的字符,从根节点到红色节点的一条路径表示一个字符串(注意:红色节点并不都是叶子节点)。数据结构

为了更容易理解 Trie 树是怎么构造出来的,咱们能够看以下 Trie 树构造的分解过程。构造过程的每一步,都至关于往 Trie 树中插入一个字符串。当全部字符串都插入完成以后,Trie 树就构造好了:
数据结构之Trie字典树
数据结构之Trie字典树ide

当咱们在 Trie 树中查找一个字符串的时候,好比查找字符串“her”,那咱们将要查找的字符串分割成单个的字符 h,e,r,而后从 Trie 树的根节点开始匹配。如图所示,绿色的路径就是在 Trie 树中匹配的路径:
数据结构之Trie字典树this

以前有提到过, Trie 树是多叉树,那么这个“多叉”是怎么体现的呢?一般来说,若是你只针对小写字母构造一棵 Trie 树,就像咱们上面的例子,那么每一个节点中使用一个长度为26的数组来表示其多个子节点便可。以下所示:搜索引擎

class Node {
    char data;
    Node children[26];
}

而若是咱们的需求不单单是只包含小写字母,但愿这是一棵通用的 Trie 树,那么就须要设计一个能动态变化的子节点容器,使得每一个节点有若干指向下个节点的指针。例如,咱们能够使用一个 Map 来实现,以下所示:设计

class Node {
    boolean isWord;  // 标识是不是单词的结尾
    Map<Character, Node> next;
}

Trie字典树基础代码

经过以上的介绍,咱们已经了解到了 Trie 树的基本概念。接下来,让咱们实现一下 Trie 树的基础功能代码,从代码上对 Trie 树有个直观的认识。具体代码以下:指针

package tree;

import java.util.Map;
import java.util.TreeMap;

/**
 * Trie树
 *
 * @author 01
 * @date 2021-01-28
 **/
public class TrieTree {

    private final Node root;

    private int size;

    /**
     * Trie树中每一个节点的结构
     */
    private static class Node {
        /**
         * 标识是不是单词的结尾
         */
        private boolean isWord;

        /**
         * 使用Map来实现动态存储多个子节点
         */
        private final Map<Character, Node> next;

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

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

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

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

    /**
     * 向Trie中添加一个新的单词word
     */
    public void add(String word) {
        Node current = root;
        for (int i = 0; i < word.length(); i++) {
            char c = word.charAt(i);
            if (current.next.get(c) == null) {
                // 没有与之对应的子节点,建立一个新的子节点
                current.next.put(c, new Node());
            }
            current = current.next.get(c);
        }

        if (!current.isWord) {
            // 添加的是新的单词,标识该节点是单词的结尾
            current.isWord = true;
            size++;
        }
    }
}

Trie字典树的查询

Trie 字典树的查询主要就是查询某个单词是否存在于 Trie 中,其主要逻辑与 add 方法基本上是同样的。代码以下:

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

    // 只有当最后一个字母所对应的节点标识了是一个单词的结尾,
    // 才能认为这个单词存在于Trie中
    return current.isWord;
}

Trie字典树的前缀查询

相比于查询某个单词是否存在 Trie 树中,前缀查询的使用范围更广,也是 Trie 树中的主要查询操做。经过前缀查询,咱们能够实现像搜索引擎那样的搜索关键词提示功能。实现前缀查询的代码与查询某个单词基本上是同样的,以下所示:

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

    return true;
}

Trie字典树和简单的模式匹配

接下来,咱们尝试使用Trie字典树来解决LeetCode上的一个简单模式匹配的问题,该问题的编号是211:

关于这个问题的详细内容,能够查看以上连接,这里就不作赘述了。对于该问题,具体的实现代码以下:

package tree.trie;

import java.util.Map;
import java.util.TreeMap;

/**
 * Leetcode 211. Add and Search Word - Data structure design
 * https://leetcode.com/problems/add-and-search-word-data-structure-design/description/
 *
 * @author 01
 */
public class WordDictionary {

    private static class Node {

        private boolean isWord;
        private final Map<Character, Node> next;

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

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

    private final Node root;

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

    /**
     * Adds a word into the data structure.
     */
    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);
        }
        cur.isWord = true;
    }

    /**
     * Returns if the word is in the data structure.
     * A word could contain the dot character '.' to represent any one letter.
     */
    public boolean search(String word) {
        return match(root, word, 0);
    }

    private 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字典树和字符串映射

最后,咱们再来解决一个LeetCode上的677号问题,该问题的连接以下:

对于该问题咱们就是要将Trie字典树做为一个映射,每一个单词就是一个 key,对应着一个 value,该 value 只存在于单词最后一个字母对应的节点。以下图所示:
数据结构之Trie字典树

有了这个形象的概念后,代码编写起来就简单了,因此也建议各位实现算法和数据结构时能够尝试多画图。对于该问题的具体实现代码以下:

package tree.trie;

import java.util.Map;
import java.util.TreeMap;

/**
 * 键值映射
 * https://leetcode-cn.com/problems/map-sum-pairs/
 *
 * @author 01
 */
public class MapSum {

    private static class Node {

        private int value;
        private final Map<Character, Node> next;

        public Node(int value) {
            this.value = value;
            next = new TreeMap<>();
        }

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

    private final Node root;

    /**
     * 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);
            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);
        }

        // 对该节点全部路径下的子节点的value进行求和
        return sum(cur);
    }

    private int sum(Node node) {
        int res = node.value;
        // 遍历全部子节点
        for (char c : node.next.keySet()) {
            // 对每一个子节点路径上的value进行递归求和
            res += sum(node.next.get(c));
        }

        return res;
    }
}
相关文章
相关标签/搜索