前缀树(字典树)Trie Java实现

本文介绍如何使用Java实现前缀树,也称为字典树,目前提供了适用于a-z字符的基础版本,后续将扩展到支持所有字符,并计划添加更多功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

仅适用a-z的版本

package com.algorithm.offer;
/**
 * Trie树(前缀树、字典树)
 * @author Lenovo
 *
 */
class TrieNode{
	// 假设字符只有a-z
	TrieNode[] children = new TrieNode[26];
	// 是否是末尾节点
	boolean isEnd;
}
public class Trie {
	static TrieNode trie;
	// 构建一棵Trie树
	public Trie() {
		trie = new TrieNode();
	}
	public void insert(String word) {
		// 从根节点开始搜索
		TrieNode cur = trie;
		char[] words = word.toCharArray();
		for(int i = 0;i < words.length;i++) {
			int idx = words[i] - 'a';
			// 说明不存在该节点
			if(cur.children[idx] == null) {
				TrieNode tmp = new TrieNode();
				if(i == words.length - 1) {
					tmp.isEnd = true;
				}
				cur.children[idx] = tmp;
			}
			cur = cur.children[idx];
		}
	}
	public boolean search(String word) {
		TrieNode cur = trie;
		char[] words = word.toCharArray();
		for(int i = 0;i < words.length;i++) {
			int idx = words[i] - 'a';
			// 说明不存在该节点
			if(cur.children[idx] == null) {
				return false;
			}
			else {
				cur = cur.children[idx];
			}
		}
		if(!cur.isEnd) return false;
 		return true;
	}
	public boolean isPrefix(String prefix) {
		TrieNode cur = trie;
		char[] words = prefix.toCharArray();
		for(int i = 0;i < words.length;i++) {
			int idx = words[i] - 'a';
			// 说明不存在该节点
			if(cur.children[idx] == null) {
				return false;
			}
			else {
				cur = cur.children[idx];
			}
		}
		if(cur.isEnd) return false;
 		return true;
	}
	
	
	public static void main(String[] args) {
		Trie trie = new Trie();
		trie.insert("abc");
		System.out.println(trie.search("abc"));
		System.out.println(trie.search("ab"));
		System.out.println(trie.search("abcd"));
		System.out.println(trie.isPrefix("ab"));
		System.out.println(trie.isPrefix("a"));
		System.out.println(trie.isPrefix("b"));
	}
	
}

适用所有字符的版本

package com.algorithm.offer;

import java.util.HashMap;
import java.util.Map;

/**
 * 实现动态增加的Trie,动态数组或者HashMap
 * @author Lenovo
 *
 */
class TrieNodeD{
	Map<Character,TrieNodeD> map = new HashMap<Character,TrieNodeD>();
	boolean isEnd;
}
public class TrieDynamic {
	TrieNodeD trie;
	public TrieDynamic() {
		trie = new TrieNodeD();
	}
	public void insert(String word) {
		TrieNodeD cur = trie;
		char[] words = word.toCharArray();
		for(int i = 0;i < words.length;i++) {
			if(cur.map.get(words[i]) == null) {
				TrieNodeD tmp = new TrieNodeD();
				if(i == words.length - 1) tmp.isEnd = true;
				cur.map.put(words[i],tmp);
			}
			cur = cur.map.get(words[i]);
		}
	}
	
	public boolean search(String word) {
		TrieNodeD cur = trie;
		char[] words = word.toCharArray();
		for(int i = 0;i < words.length;i++) {
			if(cur.map.get(words[i]) == null) {
				return false;
			}
			cur = cur.map.get(words[i]);
		}
		if(!cur.isEnd) return false;
		return true;
	}
	
	public boolean isPrefix(String word) {
		TrieNodeD cur = trie;
		char[] words = word.toCharArray();
		for(int i = 0;i < words.length;i++) {
			if(cur.map.get(words[i]) == null) {
				return false;
			}
			cur = cur.map.get(words[i]);
		}
		if(cur.isEnd) return false;
		return true;
	}
	
	public static void main(String[] args) {
		TrieDynamic trie = new TrieDynamic();
		trie.insert("abc");
		trie.insert("aB.S#");
		System.out.println(trie.search("ab"));
		System.out.println(trie.search("abc"));
		System.out.println(trie.isPrefix("ab"));
		System.out.println(trie.isPrefix("abc"));
		System.out.println(trie.search("aB.S#"));
		System.out.println(trie.isPrefix("aB."));	
	}
}

很多功能有待实现…

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

w͏l͏j͏

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值