博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Trie树
阅读量:4687 次
发布时间:2019-06-09

本文共 3387 字,大约阅读时间需要 11 分钟。

2018-09-06 16:19:17

Trie树,也被称为单词查找树,是一种树形结构。典型应用是用于统计和排序大量的字符串(但不限于字符串),所以经常被搜索引擎用于文本的词频统计。它的优点是可以最大限度的减少无谓字符的比较,查询效率比较高。

Trie的核心思想是空间换时间,利用字符串的公共前缀来降低查询时间的开销以达到提高效率的目的。

它有3个基本性质:

  1. 根节点不包含字符,除根节点外每一个节点都只包含一个字符。
  2. 从根节点到某一节点,路径上经过的字符连接起来,为该节点对应的字符串。
  3. 每个节点的所有子节点包含的字符都不相同。

问题描述:

问题求解:

public class Trie {    TrieNode root;    /**     * Initialize your data structure here.     */    public Trie() {        root = new TrieNode(' ');    }    /**     * Inserts a word into the trie.     */    public void insert(String word) {        TrieNode cur = root;        for (int i = 0; i < word.length(); i++) {            if (cur.next[word.charAt(i) - 'a'] == null) {                cur.next[word.charAt(i) - 'a'] = new TrieNode(word.charAt(i));            }            cur = cur.next[word.charAt(i) - 'a'];        }        cur.isWord = true;    }    /**     * Returns if the word is in the trie.     */    public boolean search(String word) {        TrieNode cur = root;        for (int i = 0; i < word.length(); i++) {            if (cur.next[word.charAt(i) - 'a'] == null) return false;            cur = cur.next[word.charAt(i) - 'a'];        }        return cur.isWord;    }    /**     * Returns if there is any word in the trie that starts with the given prefix.     */    public boolean startsWith(String prefix) {        TrieNode cur = root;        for (int i = 0; i < prefix.length(); i++) {            if (cur.next[prefix.charAt(i) - 'a'] == null) return false;            cur = cur.next[prefix.charAt(i) - 'a'];        }        return true;    }}class TrieNode {    public char val;    public boolean isWord;    public TrieNode[] next;    public TrieNode(char c) {        this.val = c;        this.isWord = false;        next = new TrieNode[26];    }}/** * Your Trie object will be instantiated and called as such: * Trie obj = new Trie(); * obj.insert(word); * boolean param_2 = obj.search(word); * boolean param_3 = obj.startsWith(prefix); */

 

一、Add and Search Word - Data structure design

问题描述:

问题求解:

public class WordDictionary {    public class TrieNode {        public TrieNode[] children = new TrieNode[26];        public boolean isWord = false;    }        private TrieNode root = new TrieNode();    public void addWord(String word) {        TrieNode node = root;        for (char c : word.toCharArray()) {            if (node.children[c - 'a'] == null) {                node.children[c - 'a'] = new TrieNode();            }            node = node.children[c - 'a'];        }        node.isWord = true;    }    public boolean search(String word) {        return match(word.toCharArray(), 0, root);    }        private boolean match(char[] chs, int k, TrieNode node) {        if (k == chs.length) return node.isWord;           if (chs[k] != '.') {            return node.children[chs[k] - 'a'] != null && match(chs, k + 1, node.children[chs[k] - 'a']);        } else {            for (int i = 0; i < node.children.length; i++) {                if (node.children[i] != null) {                    if (match(chs, k + 1, node.children[i])) {                        return true;                    }                }            }        }        return false;    }}/** * Your WordDictionary object will be instantiated and called as such: * WordDictionary obj = new WordDictionary(); * obj.addWord(word); * boolean param_2 = obj.search(word); */

 

转载于:https://www.cnblogs.com/TIMHY/p/9598687.html

你可能感兴趣的文章
C#匿名函数的坑
查看>>
标记页面控件尺寸
查看>>
批处理文件中的路径问题
查看>>
appium+python 环境搭建
查看>>
WampServer下修改和重置MySQL密码
查看>>
hibernate出现No row with the given identifier exists问题
查看>>
为什么wait()和notify()属于Object类
查看>>
Part2_lesson3---ARM寄存器详解
查看>>
深入理解vsto,开发word插件的利器
查看>>
PHP 在5.1.* 和5.2.*之间 PDO数据库操作中的不同!
查看>>
导入properties时的坑
查看>>
python——网络编程
查看>>
Spark的39个机器学习库
查看>>
Electron学习笔记(一)
查看>>
Java并发编程:CountDownLatch、CyclicBarrier和Semaphore
查看>>
配置NRPE的通讯
查看>>
VS2005编译VTK5.10.1
查看>>
shp系列(一)——利用C++进行shp文件的读(打开)与写(创建)开言
查看>>
总结上海永辉云商高级前端职位面试题集
查看>>
中国计算机学会推荐国际学术会议和期刊目录
查看>>