字典树Java代码实现
时间: 2023-09-23 21:06:50 浏览: 47
### 回答1:
字典树的Java实现可以使用递归来构建。首先需要定义一个TrieNode类,其中包含一个Map用于存储子节点,以及一个布尔值表示当前节点是否是一个单词的结尾。然后,定义一个Trie类来维护根节点。在Trie类中实现insert、search和startsWith方法。
下面是一个简单的字典树Java代码实现示例:
```java
class TrieNode {
Map<Character, TrieNode> children;
boolean isEndOfWord;
public TrieNode() {
children = new HashMap<>();
isEndOfWord = false;
}
}
class Trie {
private TrieNode root;
public Trie() {
root = new TrieNode();
}
public void insert(String word) {
TrieNode current = root;
for (int i = 0; i < word.length(); i++) {
char ch = word.charAt(i);
TrieNode node = current.children.get(ch);
if (node == null) {
node = new TrieNode();
current.children.put(ch, node);
}
current = node;
}
current.isEndOfWord = true;
}
public boolean search(String word) {
TrieNode current = root;
for (int i = 0; i < word.length(); i++) {
char ch = word.charAt(i);
TrieNode node = current.children.get(ch);
if (node == null) {
return false;
}
current = node;
}
return current.isEndOfWord;
}
public boolean startsWith(String prefix) {
TrieNode current = root;
for (int i = 0; i < prefix.length(); i++) {
char ch = prefix.charAt(i);
TrieNode node = current.children.get(ch);
if (node == null) {
return false;
}
current = node;
}
return true;
}
}
```
这是一个简单的Trie字典树的Java代码实现,其中包括了插入、查询和查询前缀三个常用操作。
### 回答2:
字典树,也称为前缀树或Trie树,是一种用于高效存储和检索字符串的数据结构。下面是一个用Java实现字典树的代码示例:
```java
// Trie节点
class TrieNode {
private final int ALPHABET_SIZE = 26;
TrieNode[] children;
boolean isEndOfWord;
public TrieNode() {
children = new TrieNode[ALPHABET_SIZE];
isEndOfWord = false;
}
}
// 字典树
class Trie {
private TrieNode root;
public Trie() {
root = new TrieNode();
}
// 插入字符串到字典树
public void insert(String word) {
TrieNode current = root;
for (int i = 0; i < word.length(); i++) {
char ch = word.charAt(i);
int index = ch - 'a';
if (current.children[index] == null) {
current.children[index] = new TrieNode();
}
current = current.children[index];
}
current.isEndOfWord = true;
}
// 搜索字典树中是否存在某个字符串
public boolean search(String word) {
TrieNode current = root;
for (int i = 0; i < word.length(); i++) {
char ch = word.charAt(i);
int index = ch - 'a';
if (current.children[index] == null) {
return false;
}
current = current.children[index];
}
return current != null && current.isEndOfWord;
}
}
// 测试代码
public class Main {
public static void main(String[] args) {
Trie trie = new Trie();
trie.insert("apple");
System.out.println(trie.search("apple")); // 输出 true
System.out.println(trie.search("app")); // 输出 false
trie.insert("app");
System.out.println(trie.search("app")); // 输出 true
}
}
```
这是一个基本的字典树实现,包括在字典树中插入字符串和搜索字符串的功能。可以通过创建一个`Trie`对象,并使用`insert`方法插入字符串,然后使用`search`方法来搜索字典树中是否存在某个字符串。在上面的测试代码中,首先插入了"apple",然后测试了搜索"apple"、"app"和"app"的结果。
### 回答3:
字典树,也被称为前缀树或Trie树,是一种常用于字符串的数据结构,用于高效地存储和搜索字符串集合。
下面是一个Java代码的示例,实现了字典树:
```java
class TrieNode {
private static final int ALPHABET_SIZE = 26;
TrieNode[] children;
boolean isEndOfWord;
public TrieNode() {
children = new TrieNode[ALPHABET_SIZE];
isEndOfWord = false;
}
}
class Trie {
TrieNode root;
public Trie() {
root = new TrieNode();
}
public void insert(String word) {
TrieNode curr = root;
for (int i = 0; i < word.length(); i++) {
int index = word.charAt(i) - 'a';
if (curr.children[index] == null)
curr.children[index] = new TrieNode();
curr = curr.children[index];
}
curr.isEndOfWord = true;
}
public boolean search(String word) {
TrieNode curr = root;
for (int i = 0; i < word.length(); i++) {
int index = word.charAt(i) - 'a';
if (curr.children[index] == null)
return false;
curr = curr.children[index];
}
return curr.isEndOfWord;
}
public boolean startsWith(String prefix) {
TrieNode curr = root;
for (int i = 0; i < prefix.length(); i++) {
int index = prefix.charAt(i) - 'a';
if (curr.children[index] == null)
return false;
curr = curr.children[index];
}
return true;
}
}
```
在上面的代码中,`TrieNode`表示字典树的节点,它包含一个`children`数组,用于存储子节点。`Trie`类包含`insert`、`search`和`startsWith`三个方法,分别用于向字典树中插入字符串、搜索字符串和判断是否存在以某个字符串为前缀的字符串。
使用以上示例代码,可以方便地实现字典树,并实现常见的插入、搜索和前缀搜索操作。