java 中的 trie 数据结构-ag捕鱼王app官网

java 中的 trie 数据结构

作者:迹忆客 最近更新:2023/08/05 浏览次数:

本文介绍了 java 中的 trie 数据结构。


java 中的 trie 数据结构

trie 词是从单词 retrieval 中提取出来的,它是一种用于存储字符串集合的排序数据结构。 trie可以定义为指针数量等于每个节点中字符数量的数据结构。

借助单词前缀,trie 数据结构可用于从字典中搜索单词。 在这种情况下,如果字典中的所有单词都由 26 个英文字母组成,则每个 trie 节点将有 26 个点。

trie 数据结构也称为前缀树或数字树,其中节点的位置将决定该节点将连接的键。 我们尝试通过下图来了解 trie 的工作原理:

trie 数据结构 java

上图包含了单词 deal、delft 和 delete 的树结构。

trie 数据结构的属性

以下是字符串 trie 集的主要属性:

  1. 根节点始终为空节点。
  2. 每个子节点都将按字母顺序排序。
  3. 由于英文字母的原因,每个节点不能有超过 26 个子节点。
  4. 每个节点可以存储字母表中的一个字母。

trie数据结构的应用

让我们看看trie数据结构的一些主要应用:

  1. 当需要假设所有字母都是小写时,可以使用 trie。
  2. 当我们只能使用 a-z 的字母,没有连字符,没有标点符号等时。
  3. 当单词的长度有限时。 例如,如果我们正在处理 2x2 的板,则字长不能超过 4,因此可以丢弃这些字。
  4. 字典中的许多单词具有相同的词干,例如草坪和割草机。 trie 可以用于这些词形变化的单词。

trie数据结构的基本操作

trie 具有三个基本操作:

插入操作

第一个操作是向 trie 中插入一个节点。 为此,我们需要了解以下几点:

  1. 输入单词的每个字母都将作为个体插入到 trie 节点中。
  2. 字符长度将决定 trie 的长度。
  3. 键字符数组将充当子项的索引。
  4. 如果当前节点有对当前字母的引用,则设置引用该节点的当前节点。 否则,应创建一个新节点。

搜索操作

trie 的第二个基本操作是搜索节点。 这个操作也和插入类似。

我们只需使用相同的方法搜索节点即可。

删除操作

第三个操作是删除节点。 这也是一个简单的操作。

在开始删除之前我们必须考虑两点:

  1. 如果搜索时没有找到节点键,则删除操作将停止并退出。
  2. 如果在搜索 trie 时找到节点键,则删除操作将删除该键。

trie数据结构的java实现

我们来尝试实现trie数据结构,它只支持小写a-z英文字符。 参见示例:

package jiyik;
import java.util.arraylist;
import java.util.collections;
import java.util.list;
class trie_node {
    // 26 characters for a – z
    private static final int character_size = 26;
    private boolean is_leaf;
    private list trie_children = null;
    // constructor
    trie_node(){
        is_leaf = false;
        trie_children= new arraylist<>(collections.ncopies(character_size, null));
    }
    // function for insertion
    public void insertion(string node_key) {
        system.out.println("inserting the key \""   node_key   "\"");
        // root node
        trie_node root_node = this;
        // repeat for every character of the key
        for (char character: node_key.tochararray()) {
            // create a new trie node if the path does not exist
            if (root_node.trie_children.get(character - 'a') == null) {
                root_node.trie_children.set(character - 'a', new trie_node());
            }
            // going to the next node
            root_node = root_node.trie_children.get(character - 'a');
        }
        //current node as a leaf
        root_node.is_leaf = true;
    }
    // method for search
    public boolean searching(string node_key) {
        system.out.print("searching the key \""   node_key   "\" : ");
        trie_node current_node = this;
        // repeat for the character of the key
        for (char character: node_key.tochararray()) {
            // going to the next node
            current_node = current_node.trie_children.get(character - 'a');
            // reach out to the end of a path in the trie if the string is invalid
            if (current_node == null) {
                return false;
            }
        }
        return current_node.is_leaf;
    }
}
public class example {
    public static void main (string[] args) {
        // construct a new trie node
        trie_node new_trie = new trie_node();
        new_trie.insertion("delft");
        new_trie.insertion("delf");
        new_trie.insertion("del");
        system.out.println(new_trie.searching("del"));            // true
        system.out.println(new_trie.searching("delf"));           // true
        system.out.println(new_trie.searching("delft"));          // true
        system.out.println(new_trie.searching("jiyik"));   // false
        new_trie.insertion("jiyik");
        system.out.println(new_trie.searching("del"));            // true
        system.out.println(new_trie.searching("delf"));           // true
        system.out.println(new_trie.searching("delft"));          // true
        system.out.println(new_trie.searching("jiyik"));   // true
    }
}

上面的代码实现了trie数据结构的插入和查找操作。 查看输出:

inserting the key "delft"
inserting the key "delf"
inserting the key "del"
searching the key "del" : true
searching the key "delf" : true
searching the key "delft" : true
searching the key "jiyik" : false
inserting the key "jiyik"
searching the key "del" : true
searching the key "delf" : true
searching the key "delft" : true
searching the key "jiyik" : true

现在,trie 数据结构的空间复杂度为 o(n × m × c),其中:

  • n - 字符串数量
  • m - 字符串的最大长度。
  • c - 字母表的大小

因此,在具有上述空间复杂度的java中,可能会出现存储问题。

我们还可以利用java中的hashmap来实现trie来解决存储问题。 让我们尝试使用 hashmap 来实现 trie:

package jiyik;
import java.util.hashmap;
import java.util.map;
class trie_node{
    private boolean is_leaf;
    private map trie_children;
    // constructor
    trie_node()
    {
        is_leaf = false;
        trie_children = new hashmap<>();
    }
    // insertion
    public void insertion(string node_key)
    {
        system.out.println("inserting the key \""   node_key   "\"");
        // root node
        trie_node root_node = this;
        for (char character: node_key.tochararray()) {
            // if the path doesn't exist, create a new node.
            root_node.trie_children.putifabsent(character, new trie_node());
            // going to the next node
            root_node = root_node.trie_children.get(character);
        }
        root_node.is_leaf = true;
    }
    // searching
    public boolean searching(string node_key) {
        system.out.print("searching the key \""   node_key   "\" : ");
        trie_node current_node = this;
        // repeat
        for (char character: node_key.tochararray()) {
            // going to the next node
            current_node = current_node.trie_children.get(character);
            if (current_node == null) {
                return false;
            }
        }
        return current_node.is_leaf;
    }
}
public class example {
    public static void main (string[] args) {
        // construct a new trie node
        trie_node new_trie = new trie_node();
        new_trie.insertion("delft");
        new_trie.insertion("delf");
        new_trie.insertion("del");
        system.out.println(new_trie.searching("del"));            // true
        system.out.println(new_trie.searching("delf"));           // true
        system.out.println(new_trie.searching("delft"));          // true
        system.out.println(new_trie.searching("jiyik"));   // false
        new_trie.insertion("jiyik");
        system.out.println(new_trie.searching("del"));            // true
        system.out.println(new_trie.searching("delf"));           // true
        system.out.println(new_trie.searching("delft"));          // true
        system.out.println(new_trie.searching("jiyik"));   // true
    }
}

该代码执行相同的工作,但以更节省内存的方式进行。 查看 java 中 trie 的输出:

inserting the key "delft"
inserting the key "delf"
inserting the key "del"
searching the key "del" : true
searching the key "delf" : true
searching the key "delft" : true
searching the key "jiyik" : false
inserting the key "jiyik"
searching the key "del" : true
searching the key "delf" : true
searching the key "delft" : true
searching the key "jiyik" : true

上一篇:

下一篇:

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

如何在 java 中延迟几秒钟的时间

发布时间:2023/12/17 浏览次数:217 分类:java

本篇文章主要介绍如何在 java 中制造程序延迟。本教程介绍了如何在 java 中制造程序延时,并列举了一些示例代码来了解它。

如何在 java 中把 hashmap 转换为 json 对象

发布时间:2023/12/17 浏览次数:187 分类:java

它描述了允许我们将哈希图转换为简单的 json 对象的方法。本文介绍了在 java 中把 hashmap 转换为 json 对象的方法。我们将看到关于创建一个 hashmap,然后将其转换为 json 对象的详细例子。

如何在 java 中按值排序 map

发布时间:2023/12/17 浏览次数:171 分类:java

本文介绍了如何在 java 中按值对 map 进行排序。本教程介绍了如何在 java 中按值对 map 进行排序,并列出了一些示例代码来理解它。

如何在 java 中打印 hashmap

发布时间:2023/12/17 浏览次数:192 分类:java

本帖介绍了如何在 java 中打印 hashmap。本教程介绍了如何在 java 中打印 hashmap 元素,还列举了一些示例代码来理解这个主题。

在 java 中更新 hashmap 的值

发布时间:2023/12/17 浏览次数:146 分类:java

本文介绍了如何在 java 中更新 hashmap 中的一个值。本文介绍了如何在 java 中使用 hashmap 类中包含的两个方法-put() 和 replace() 更新 hashmap 中的值。

java 中的 hashmap 和 map 之间的区别

发布时间:2023/12/17 浏览次数:79 分类:java

本文介绍了 java 中的 hashmap 和 map 接口之间的区别。本教程介绍了 java 中 map 和 hashmap 之间的主要区别。在 java 中,map 是用于以键值对存储数据的接口,

在 java 中获取用户主目录

发布时间:2023/12/17 浏览次数:218 分类:java

这篇文章向你展示了如何在 java 中获取用户主目录。本教程介绍了如何在 java 中获取用户主目录,并列出了一些示例代码以指导你完成该主题。

java 中 size 和 length 的区别

发布时间:2023/12/17 浏览次数:179 分类:java

这篇文章教你如何知道 java 中大小和长度之间的区别。本教程介绍了 java 中大小和长度之间的区别。我们还列出了一些示例代码以帮助你理解该主题。

java 中的互斥锁

发布时间:2023/12/17 浏览次数:111 分类:java

了解有关 java 中互斥锁的一切,在计算机科学领域,互斥或互斥被称为并发控制的属性。每台计算机都使用称为线程的最小程序指令序列。有一次,计算机在一个线程上工作。为了更好地理解,

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便
网站地图