编写一个字典树Trie的java代码
时间: 2023-08-31 19:38:08 浏览: 190
### 回答1:
以下是字典树的Java代码示例:
```
class TrieNode {
private TrieNode[] children;
private boolean isEnd;
public TrieNode() {
children = new TrieNode[26];
Arrays.fill(children, null);
isEnd = false;
}
}
class Trie {
private TrieNode root;
public Trie() {
root = new TrieNode();
}
public void insert(String word) {
TrieNode node = root;
for (int i = 0; i < word.length(); i++) {
int index = word.charAt(i) - 'a';
if (node.children[index] == null) {
node.children[index] = new TrieNode();
}
node = node.children[index];
}
node.isEnd = true;
}
public boolean search(String word) {
TrieNode node = root;
for (int i = 0; i < word.length(); i++) {
int index = word.charAt(i) - 'a';
if (node.children[index] == null) {
return false;
}
node = node.children[index];
}
return node.isEnd;
}
public boolean startsWith(String prefix) {
TrieNode node = root;
for (int i = 0; i < prefix.length(); i++) {
int index = prefix.charAt(i) - 'a';
if (node.children[index] == null) {
return false;
}
node = node.children[index];
}
return true;
}
}
```
这是一个简单的字典树实现,它包含三个方法:插入单词、搜索单词和搜索前缀。
### 回答2:
下面是一个简单的Trie字典树的Java代码:
public class Trie {
private TrieNode root;
// 初始化字典树
public Trie() {
root = new TrieNode();
}
// 插入一个单词到字典树中
public void insert(String word) {
TrieNode node = root;
for (int i = 0; i < word.length(); i++) {
char currentChar = word.charAt(i);
TrieNode nextNode = node.get(currentChar);
if (nextNode == null) {
nextNode = new TrieNode();
node.put(currentChar, nextNode);
}
node = nextNode;
}
node.setEnd();
}
// 搜索一个单词是否在字典树中
public boolean search(String word) {
TrieNode node = searchPrefix(word);
return node != null && node.isEnd();
}
// 搜索一个单词的前缀是否在字典树中
public boolean startsWith(String prefix) {
TrieNode node = searchPrefix(prefix);
return node != null;
}
// 在字典树中搜索一个单词或前缀的最后一个节点
private TrieNode searchPrefix(String word) {
TrieNode node = root;
for (int i = 0; i < word.length(); i++) {
char currentChar = word.charAt(i);
node = node.get(currentChar);
if (node == null) {
return null;
}
}
return node;
}
}
class TrieNode {
private TrieNode[] links;
private final int R = 26;
private boolean isEnd;
public TrieNode() {
links = new TrieNode[R];
}
public boolean containsKey(char c) {
return links[c - 'a'] != null;
}
public TrieNode get(char c) {
return links[c - 'a'];
}
public void put(char c, TrieNode node) {
links[c - 'a'] = node;
}
public void setEnd() {
isEnd = true;
}
public boolean isEnd() {
return isEnd;
}
}
以上是一个简单的Trie字典树的Java代码,包含了插入单词、搜索单词和搜索前缀的功能。字典树可以用来高效地存储和搜索大量的字符串,对于自动补全、拼写检查等应用场景非常有用。
### 回答3:
下面是一个用Java编写的字典树Trie的代码示例:
```java
class TrieNode {
private TrieNode[] children;
private boolean isWord;
public TrieNode() {
children = new TrieNode[26];
isWord = false;
}
public void insert(String word) {
TrieNode node = this;
for (char c : word.toCharArray()) {
int index = c - 'a';
if (node.children[index] == null) {
node.children[index] = new TrieNode();
}
node = node.children[index];
}
node.isWord = true;
}
public boolean search(String word) {
TrieNode node = this;
for (char c : word.toCharArray()) {
int index = c - 'a';
if (node.children[index] == null) {
return false;
}
node = node.children[index];
}
return node.isWord;
}
public boolean startsWith(String prefix) {
TrieNode node = this;
for (char c : prefix.toCharArray()) {
int index = c - 'a';
if (node.children[index] == null) {
return false;
}
node = node.children[index];
}
return true;
}
}
public class Trie {
private TrieNode root;
public Trie() {
root = new TrieNode();
}
public void insert(String word) {
root.insert(word);
}
public boolean search(String word) {
return root.search(word);
}
public boolean startsWith(String prefix) {
return root.startsWith(prefix);
}
}
```
在这个实现中,`TrieNode` 类代表 Trie 的节点,每个节点包含一个用于保存子节点的数组 `children` 和一个表示是否为单词结尾的布尔值 `isWord`。`Trie` 类则是 Trie 的主类,它拥有一个根节点 `root`,提供了插入单词、查找单词和查找前缀的方法。
这段代码使用英文小写字母构建了一个字典树,通过 `insert` 方法插入一个单词,`search` 方法查找一个单词是否在字典树中,`startsWith` 方法则用于查找字典树中是否存在以指定前缀开头的单词。
希望以上代码能够帮助到你!
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)