理解前缀树

简介: 理解前缀树

Prefix tree

前缀树实现

class Node
{
    public int pass;
    public int end;
    public Node[] nexts;
    /******也可以用哈希表******/
    public Node()
    {
        pass = 0;
        end = 0;
        nexts = new Node[26];
    }
};
class Tris
{
    private Node root;
    public Tris(){root = new Node(); }
    public void Insert(string str)
    {
        if(str == null)
        {
            return;
        }
        char[] chs = str.toCharArray();
        Node node = root;
        node.pass++;
        int index = 0;
        for(int i = 0;i < charr.size(); i++)
        {
            index = chs[i] - 'a';
            if(node.nexts[index] == null)
            {
                node.nexts[index] = new Node();
            }
            node = node.nexts[index];
            node.pass++;
        }
        node.end++;
    }
    public void Desert(string str)
    {
        if(!Search(str)) return;
        char[] chs = str.toCharArray();
        Node node = root;
        int index = 0;
        node.pass--;
        for(int i = 0;i < chs.size(); i++)
        {
            index = chs[i] - 'a';
            if(--node.nexts[index].pass == 0)
            {
                node.nexts[index] = null;
                return;
                /******c++需要手动遍历释放节点******/
            }
            node = node.nexts[index];
        }
        node.end--;
    }
    public boolean Search(string str)
    {
        if(str == null) return true;
        char[] chs = str.toCharArray();
        Node node = root;
        int index = 0;
        for(int i = 0;i < chs.size(); i++)
        {
            index = chs[i] - 'a';
            if(node.nexts[index] == null) return false;
            node = node.nexts[index];
        }
        return node.end > 0;
    }
    /******查找有多少以str为前缀的字符串******/
    public int SearchPre(string str)
    {
        if(str == null) return 0;
        Node node = root;
        int index = 0;
        char[] chs = str.toCharArray();
        for(int i = 0;i < chs.size();i++)
        {
            index = chs[i] - 'a';
            if(node.nexts[index] == null) return 0;
            node = node.nexts[index];
        }
        return node.pass;
    }
}


目录
相关文章
|
4月前
哈夫曼编码和字典树
哈夫曼编码和字典树
39 0
|
4月前
|
搜索推荐
前缀树Trie
前缀树Trie
|
4月前
|
存储 C++
leetcode-208:实现 Trie (前缀树/字典树)
leetcode-208:实现 Trie (前缀树/字典树)
42 0
|
存储 自然语言处理 算法
一种好用的树结构:Trie树
一种好用的树结构:Trie树
163 0
一种好用的树结构:Trie树
|
存储 Java
Java数据结构之第十五章、Trie(前缀树/单词查找树)
1.前缀树的概念:前缀树又叫字典树或单词查找树(高效的存储和查找字符串集合的数据结构)。2.3.存储形式:存储的字符串可能:全是 小写字母 或全是 大写字母 或全是 数字 或全是 0和1。它是一棵,每个代表一个,从。字典树的根节点不包含字符,每个子节点代表一个字符,从根节点到任意一个节点所经过的路径上的字符连接起来即为该节点所代表的字符串。每个节点可以存储一个或多个字符串,通常使用一个标志来标记一个节点代表的字符串是否存在。当需要在一组字符串中查找某个字符串时,可以利用字典树来实现高效的查找操作。
60 0
|
存储 机器学习/深度学习 算法
|
存储
前缀树
Trie(发音类似 "try")或者说 前缀树 是一种树形数据结构,用于高效地存储和检索字符串数据集中的键。这一数据结构有相当多的应用情景,例如自动补完和拼写检查。
71 0
前缀树
|
机器学习/深度学习 存储 自然语言处理
Trie树
Trie树
95 0
|
存储 算法 Linux
秒懂算法 | 字典树
字典树是一种基础方法,请掌握字典树的静态数组存储方法,它在后缀树、回文树、AC自动机、后缀自动机中都要用到。
144 0
|
存储 自然语言处理 算法
字典树详解
字典树,顾名思义,是关于“字典”的一棵树。即:它是对于字典的一种存储方式(所以是一种数据结构而不是算法)。
156 0
字典树详解