用java写一段trie树代码并详细解释下
时间: 2023-08-31 09:24:39 浏览: 56
### 回答1:
Trie树是一种树形数据结构,它主要用于字符串的存储和查找。Trie树的每个节点代表一个字符,根节点不存储任何字符,其他节点代表一个字符串的一部分。在Trie树中,如果一个节点的所有子节点都是叶节点,则该节点表示一个完整的字符串。
下面是一段Java代码,实现了一个简单的Trie树:
```
class TrieNode {
private TrieNode[] children = new TrieNode[26];
private boolean isEnd;
public TrieNode() {}
public TrieNode[] getChildren() {
return children;
}
public void setEnd() {
isEnd = true;
}
public boolean isEnd() {
return isEnd;
}
}
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 j = word.charAt(i) - 'a';
if (node.getChildren()[j] == null) {
node.getChildren()[j] = new TrieNode();
}
node = node.getChildren()[j];
}
node.setEnd();
}
public boolean search(String word) {
TrieNode node = root;
for (int i = 0; i < word.length(); i++) {
int j = word.charAt(i) - 'a';
if (node.getChildren()[j] == null) {
return false;
}
node = node.getChildren()[j];
}
return node.isEnd();
}
public boolean startsWith(String prefix) {
TrieNode node = root;
for (int i = 0; i < prefix.length(); i++) {
int j = prefix.charAt(i) - 'a';
if (node.getChildren()[j] == null) {
return false;
}
node = node.getChildren()[j];
}
return true;
}
}
```
该代码实现了一个Trie树,其中TrieNode
### 回答2:
Trie树,也叫字典树或前缀树,是一种用于高效存储和检索字符串的数据结构。下面是使用Java编写的一个简单Trie树的代码示例:
```java
class TrieNode {
private TrieNode[] children;
private boolean isEndOfWord;
public TrieNode() {
children = new TrieNode[26]; // 26个字母
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.isEndOfWord;
}
public boolean startsWith(String prefix) {
TrieNode current = root;
for (int i = 0; i < prefix.length(); i++) {
char ch = prefix.charAt(i);
int index = ch - 'a';
if (current.children[index] == null) {
return false;
}
current = current.children[index];
}
return true;
}
}
```
在上述代码中,我们使用了两个类:TrieNode和Trie。TrieNode表示Trie树中的每个节点,包含一个26个元素的数组来存储子节点和一个布尔变量isEndOfWord来表示当前节点是否是一个单词的结尾。Trie类是Trie树的实现,包含了插入、搜索和前缀搜索三个方法。
在插入方法中,我们首先从根节点开始,遍历插入的字符串的每个字符。通过计算字符在字母表中的位置,我们可以将其作为TrieNode数组的索引,以此构造Trie树的路径。最后,将叶子节点的isEndOfWord设置为true,表示该路径对应的字符串在Trie树中存在。
在搜索方法中,我们同样从根节点开始,遍历待搜索的字符串的每个字符。如果遇到某个字符在Trie树当前节点的子节点中不存在,则说明该字符串不存在于Trie树中,返回false。如果成功遍历完所有字符,并且叶子节点对应的isEndOfWord为true,则说明该字符串存在于Trie树中,返回true。
在前缀搜索方法中,与搜索方法类似,只是在遍历完所有字符后不进行isEndOfWord的判断,始终返回true。这是因为前缀搜索只需要判断给定字符串的前缀是否存在于Trie树中,无需判断是否是一个完整单词。
通过上述样例代码,我们可以实现一个简单的Trie树,并且能够进行插入、搜索和前缀搜索等操作。Trie树的优势在于其高效的字符串存储和检索性能,特别适用于需要进行前缀搜索的场景,如自动补全、拼写纠错等应用。
### 回答3:
Trie树(也称为字典树或前缀树)是一种树形数据结构,用于高效地存储和搜索字符串集合。下面是用Java编写的简单Trie树代码段:
```java
class TrieNode {
private TrieNode[] children;
private boolean isEndOfWord;
public TrieNode() {
children = new TrieNode[26]; // 26个小写字母
isEndOfWord = false;
}
}
public class Trie {
private TrieNode root;
public Trie() {
root = new TrieNode();
}
public void insert(String word) {
TrieNode currentNode = root;
for (int i = 0; i < word.length(); i++) {
int index = word.charAt(i) - 'a';
if (currentNode.children[index] == null) {
currentNode.children[index] = new TrieNode();
}
currentNode = currentNode.children[index];
}
currentNode.isEndOfWord = true;
}
public boolean search(String word) {
TrieNode currentNode = root;
for (int i = 0; i < word.length(); i++) {
int index = word.charAt(i) - 'a';
if (currentNode.children[index] == null) {
return false;
}
currentNode = currentNode.children[index];
}
return currentNode != null && currentNode.isEndOfWord;
}
public boolean startsWith(String prefix) {
TrieNode currentNode = root;
for (int i = 0; i < prefix.length(); i++) {
int index = prefix.charAt(i) - 'a';
if (currentNode.children[index] == null) {
return false;
}
currentNode = currentNode.children[index];
}
return true;
}
}
```
上述代码实现了一个简单的Trie树。Trie树的核心部分是`TrieNode`类,每个节点包含一个长度为26的子节点数组`children`,以及一个`boolean`类型的`isEndOfWord`字段,表示当前节点是否为一个单词的结尾。
`Trie`类是Trie树的主要类,主要提供插入、搜索和前缀搜索功能。在插入操作中,我们从根节点开始遍历字符串的每个字符,并根据字符的ASCII值索引到对应的子节点位置。如果当前节点的子节点为空,则创建一个新节点,并将当前节点更新为新节点。插入完成后,我们将最后一个节点的`isEndOfWord`字段设置为`true`,表示一个单词的结尾。
在搜索操作中,我们也是从根节点开始遍历字符串的每个字符,并根据字符的ASCII值索引到对应的子节点位置。如果遍历过程中发现某个字符的子节点为空,则返回`false`,表示找不到对应的单词。最后,我们检查最后一个字符的节点是否为空,并查看其`isEndOfWord`字段是否为`true`,来确定搜索操作的结果。
在前缀搜索操作中,逻辑与搜索操作类似,只是在找到对应的子节点位置时不需要判断`isEndOfWord`字段,只需保证一直存在就行。
通过这样的Trie树数据结构,我们可以高效地存储和搜索大量的字符串集合,具有较低的时间复杂度。