字典树相关代码


package com.myway.study;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
* 字典树 城市相关查询 (现针对26个英文字母)
* User: zhangyong
* Date: 14-8-10
* Time: 上午11:21
* To change this template use File | Settings | File Templates.
*/
public class DictionaryTree {

private static char SPACE = ' ';

private static char[] BASE_CHARS = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};

private TrieNode root = null;

public DictionaryTree() {
root = new TrieNode(SPACE);
}

public class TrieNode {

private TrieNode[] childrenNodes = null;

private char charValue;

private boolean wordEnd = false;

public TrieNode(char charValue) {
this.charValue = charValue;
childrenNodes = new TrieNode[26];
}

public void setWordEnd(boolean wordEnd) {
this.wordEnd = wordEnd;
}

public boolean isWordEnd() {
return wordEnd;
}
}

public void insert(String word) {
char[] charArray = word.toCharArray();
TrieNode currentNode = root;
for (char temp : charArray) {
int index = (temp - 'a');
TrieNode trieNode = currentNode.childrenNodes[index];
if (trieNode == null) {
trieNode = new TrieNode(temp);
currentNode.childrenNodes[index] = trieNode;
}
currentNode = trieNode;
}
currentNode.setWordEnd(true); //是一个单词
}

public boolean search(String key) {
boolean flag = true;
char[] charArray = key.toCharArray();
TrieNode currentNode = root;
for (char temp : charArray) {
int index = (temp - 'a');
TrieNode trieNode = currentNode.childrenNodes[index];
if (trieNode == null) {
flag = false;
break;
} else {
currentNode = trieNode;
}
}
return flag;
}

public Set<String> travel(String key) {
char[] charArray = key.toCharArray();
TrieNode currentNode = root;
boolean flag = true;
for (char temp : charArray) {
int index = (temp - 'a');
TrieNode trieNode = currentNode.childrenNodes[index];
if (trieNode == null) {
flag = false;
break;
} else {
currentNode = trieNode;
}
}
if (flag) {
Set<String> set = new HashSet<String>();
TrieNode[] childrenNodes = currentNode.childrenNodes;
for (TrieNode child : childrenNodes) {
if (child != null) {
Recursion(child, key, set);
}
}
return set;
}
return null;
}

public void Recursion(TrieNode trieNode, String append, Set<String> set) {
TrieNode currentNode = trieNode;
if (currentNode != null) {
String temp = append + currentNode.charValue;
if (currentNode.isWordEnd()) {
set.add(temp);
}
TrieNode[] childrenNodes = currentNode.childrenNodes;
for (TrieNode child : childrenNodes) {
if (child != null) {
Recursion(child, temp, set);
}
}
}
}

public static void main(String[] args) {
//比如查询 b 输出 beijing beidaihe baoding
DictionaryTree dictionaryTree = new DictionaryTree();
dictionaryTree.insert("shanghai");
dictionaryTree.insert("shenzhen");
dictionaryTree.insert("beijing");
dictionaryTree.insert("beidaihe");
dictionaryTree.insert("baoding");
dictionaryTree.insert("guangzhou");
dictionaryTree.insert("hangzhou");
System.out.println(dictionaryTree.search("beijing"));
System.out.println(dictionaryTree.travel("b"));
System.out.println(dictionaryTree.travel("guang"));
}


}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
字典树的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代码实现,其中包括了插入、查询和查询前缀三个常用操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值