Trie Tree 前缀树的实现

Trie

Trie [traɪ] 读音和 try 相同,也称字典树,前缀树,单词查找树等。
前缀树是一种树形数据结构,用于高效地存储和检索字符串数据集中的键。这一数据结构有相当多的应用场景,例如自动补完和拼写检查。

Trie的节点

Trie是一颗非典型的多叉树模型,它和一般的多叉树不一样,尤其在节点的数据结构设计上,比如一般的多叉树的定义是这样的:(以二叉树为例)

public class TreeNode {
     int val;        //节点值
     TreeNode left;  //指向左孩子节点
     TreeNode right; //指向右孩子节点
}

而Trie的定义是这样的:

public class Trie(){
	Trie[] children;  //字母映射表 
	boolean isEnd;    //该节点是否为字符串的结尾
}

可见,Trie节点中并没有直接保存字符值的数据成员,而是通过孩子节点——字母映射表children来体现。children是指向子节点的指针数组,对于纯小写字母的字符串而言,数组长度为26。此时children[0]对应a,children[1]对应小写字母b,…,children[25]对应小写字母z。
Trie[] children中保存了当前结点的下一个可能出现的所有字符的链接,因此可以通过一个父结点来预知它所有子结点的值:

if(trieNode.children[i]==NULL){
	当前节点的后一个字母不可为children[i]对应字母
}else{
	当前节点的后一个字母可为children[i]对应字母
}

例子:包含三个单词“sea”、“shell”、“she”的Trie如下图所示:

Trie 中一般都含有大量的空链接,因此在绘制一棵单词查找树时一般会忽略空链接,同时为了方便理解我们可以画成这样:

实际并非如此,但可这样理解

Trie的常用方法

定义类Trie

class Trie{
	private Trie[] children;
	private boolean isEnd;
}

初始化

Trie(){
	children=new Trie[26];
	isEnd=false;
}

插入

描述:向Trie中插入一个单词word
实现:首先从根结点的子结点开始与 word 第一个字符进行匹配,一直匹配到前缀链上没有对应的字符,这时开始不断开辟新的结点,直到插入完 word 的最后一个字符,同时还要将最后一个结点isEnd = true;,表示它是一个单词的末尾。

void insert(String word){
	Trie node=this;
    for(int i=0;i<word.length();i++){
        char c=word.charAt(i);
        int index=c-'a';
        if(node.children[index]==null){
        	node.children[index]=new Trie();
        }
        node=node.children[index];
    }
    node.isEnd=true;
}

查找

描述:查找 Trie 中是否存在单词 word

实现:从根结点的子结点开始,一直向下匹配即可,如果出现结点值为空就返回 false,如果匹配到了最后一个字符,那我们只需判断 node.isEnd即可。

boolean search(String word){
	Trie node=this;
	for (int i = 0; i < word.length(); i++) {
		char ch = word.charAt(i);
		int index = ch - 'a';
		if (node.children[index] == null) {
			return false;
		}
        node = node.children[index];
	}
	return node.isEnd;
}

前缀匹配

描述:判断 Trie 中是或有以 prefix 为前缀的单词

实现:和 search 操作类似,只是不需要判断最后一个字符结点的isEnd,因为既然能匹配到最后一个字符,那后面一定有单词是以它为前缀的。

boolean startWith(String prefix){
	Trie node=this;
	for (int i = 0; i < prefix.length(); i++) {
		char ch = prefix.charAt(i);
        int index = ch - 'a';
        if (node.children[index] == null) {
            return false;
        }
        node = node.children[index];
    }
    return true;
}

总结

通过以上介绍和代码实现我们可以总结出 Trie 的几点性质:

  1. Trie 的形状和单词的插入或删除顺序无关,也就是说对于任意给定的一组单词,Trie 的形状都是唯一的。
  2. 查找或插入一个长度为 L的单词,访问 next 数组的次数最多为 L+1,和 Trie 中包含多少个单词无关。
  3. Trie的每个结点中都保留着一个字母表,这是很耗费空间的。如果 Trie 的高度为 n,字母表的大小为 m,最坏的情况是 Trie中还不存在前缀相同的单词,那空间复杂度就为 O(m^n)。

Trie 的应用场景:一次建树,多次查询。

全部代码

class Trie {
    private Trie[] children;
    private boolean isEnd;

    public Trie() {
        children = new Trie[26];
        isEnd = false;
    }
    
    public void insert(String word) {
        Trie node = this;
        for (int i = 0; i < word.length(); i++) {
            char ch = word.charAt(i);
            int index = ch - 'a';
            if (node.children[index] == null) {
                node.children[index] = new Trie();
            }
            node = node.children[index];
        }
        node.isEnd = true;
    }
    
    public boolean search(String word) {
        Trie node = searchPrefix(word);
        return node != null && node.isEnd;
    }
    
    public boolean startsWith(String prefix) {
        return searchPrefix(prefix) != null;
    }

    private Trie searchPrefix(String prefix) {
        Trie node = this;
        for (int i = 0; i < prefix.length(); i++) {
            char ch = prefix.charAt(i);
            int index = ch - 'a';
            if (node.children[index] == null) {
                return null;
            }
            node = node.children[index];
        }
        return node;
    }
}

以上内容总结自 huwt
如有侵权请联系我删除,谢谢!

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值