Data structures 如何从使用trie实现的字典中获取给定长度(L)的随机单词?
上面的答案解释了如何选择第一个字符,但我不知道接下来将如何进行。我想要长度为L的单词,但当我开始遍历树时,我不知道正在遍历的分支是否具有深度L 字典Data structures 如何从使用trie实现的字典中获取给定长度(L)的随机单词?,data-structures,trie,Data Structures,Trie,上面的答案解释了如何选择第一个字符,但我不知道接下来将如何进行。我想要长度为L的单词,但当我开始遍历树时,我不知道正在遍历的分支是否具有深度L 字典 package com.FastDictionary; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList; import sun.rmi.runtime.L
package com.FastDictionary;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import sun.rmi.runtime.Log;
/**
* Dictionary implementation.
* Uses Trie Data Structure
* Creates a singleton object
*/
public class FastDictionary {
private int nineWordCount;
private int totalWordCount;
// Root Node
private DictionaryNode root;
// Singleton object
private static FastDictionary fastDictionary;
// Flag; True if words.txt has been processed once
private boolean isProcessed;
private FastDictionary() {
this.root = new DictionaryNode();
isProcessed = false;
this.nineWordCount = 0;
this.totalWordCount = 0;
}
private boolean sanitiseSearch(String text) {
if (text == null) {
return false;
}
else {
return text.matches("[a-zA-Z]");
}
}
/**
* Add a word to Dictionary
* @param word word to be added
*/
public void addWord(String word) {
if (word == null) {
throw new IllegalArgumentException("Word to be added to Dictionary can't be null");
}
// Sanitise input
if (word.contains(" ")) {
throw new IllegalArgumentException(
"Word to be added to Dictionary can't contain white spaces");
}
DictionaryNode currentNode = this.root;
for (char c: word.toCharArray()) {
DictionaryNode child = currentNode.getChild(c);
if (child == null) {
currentNode = currentNode.addChild(c);
}
else {
currentNode = child;
}
}
// Last node contains last character of valid word
// Set that node as Leaf Node for valid word
currentNode.setLeaf();
}
/**
*
* @param word String to be checked if it is a valid word
* @return True if valid word
*/
public boolean isWord(String word) {
if (word == null) {
throw new IllegalArgumentException("Word to be added to Dictionary can't be null");
}
// Sanitise input
if (word.contains(" ")) {
throw new IllegalArgumentException(
"Word to be added to Dictionary can't contain white spaces");
}
DictionaryNode currentNode = this.root;
for (char c: word.toCharArray()) {
DictionaryNode child = currentNode.getChild(c);
if (child == null) {
return false;
}
currentNode = child;
}
// Returns true if Last Character was leaf
return currentNode.isLeaf();
}
/**
*
* @param text String that needs to be searched
* @return List of Strings which are valid words searched using 'text'
*
*/
public ArrayList<String> getWords(String text) {
ArrayList<String> words = new ArrayList<String>();
DictionaryNode currentNode = this.root;
for (int i = 0; i < text.length() ; i++) {
DictionaryNode child = currentNode.getChild(text.charAt(i));
if (child == null) {
return words;
}
if (child.isLeaf()) {
words.add(text.substring(0,i+1));
}
currentNode = child;
}
return words;
}
/**
*
* @param inputFileStream Text file containing list of valid words
* Switches Flag isProcessed to True
*/
public void processFile(InputStream inputFileStream) {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(inputFileStream));
String line;
while((line = br.readLine()) != null) {
line = line.trim();
this.addWord(line);
// Nine Word test
if (line.length() == 9) {
this.nineWordCount++;
}
this.totalWordCount++;
}
}
catch(Exception e){
System.out.print(e);
}
this.isProcessed = true;
}
/**
*
* @return True if valid words text file has been processed
* Word file needs to be processed just once
*/
public boolean isProcessed() {
return this.isProcessed;
}
/**
* Factory method to create Singleton Object
* @return Singleton object
*/
public static FastDictionary getInstance() {
if (fastDictionary == null) {
fastDictionary = new FastDictionary();
}
return fastDictionary;
}
public int getNineWordCount() {
return this.nineWordCount;
}
}
**Node**
package com.FastDictionary;
import java.util.HashMap;
/**
* Node of the Trie Data Structure used for FastDictionary
*/
public class DictionaryNode {
// Character which the Node represents
private char nodeChar;
// Points to children
private HashMap<Character, DictionaryNode> children = new HashMap<Character,DictionaryNode>();
// Is Node the last character for a valid word
private boolean isLeaf;
/**
* To create Root Node
*/
DictionaryNode() {
this.nodeChar = '.';
this.isLeaf = false;
}
/**
* To create Child Node
* @param c Character that Node represents
*/
DictionaryNode(char c) {
this.nodeChar = c;
isLeaf = false;
}
/**
*
* @param c Character that Node represents
* @return Child Node which was created
*/
public DictionaryNode addChild(char c) {
DictionaryNode child = new DictionaryNode(c);
this.children.put(c, child);
return child;
}
/**
*
* @return true if Node is the last character for a valid word; default is false
*/
public boolean isLeaf() {
return this.isLeaf;
}
/**
* Set Node as Leaf Node for a valid word
*/
public void setLeaf() {
this.isLeaf = true;
}
/**
*
* @param c the character which the Child Node represnts
* @return Child Node representing character c; null if no such Child exists
*/
public DictionaryNode getChild(char c) {
DictionaryNode child = this.children.get(c);
return child;
}
}
package.com.FastDictionary;
导入java.io.BufferedReader;
导入java.io.InputStream;
导入java.io.InputStreamReader;
导入java.util.ArrayList;
导入sun.rmi.runtime.Log;
/**
*字典实现。
*使用Trie数据结构
*创建单例对象
*/
公共类快速字典{
私人整数九位数;
私有整数totalWordCount;
//根节点
私有字典;节点根;
//单态对象
私有静态快速字典;
//标志;如果words.txt处理过一次,则为True
私有布尔处理;
专用快速字典(){
this.root=new DictionaryNode();
isProcessed=false;
这个.nineWordCount=0;
this.totalWordCount=0;
}
专用布尔搜索(字符串文本){
if(text==null){
返回false;
}
否则{
返回text.matches(“[a-zA-Z]”);
}
}
/**
*在字典中添加一个单词
*@param-word待添加
*/
公共无效添加字(字符串字){
if(word==null){
抛出新的IllegalArgumentException(“要添加到字典中的单词不能为空”);
}
//消毒输入
if(word.contains(“”){
抛出新的IllegalArgumentException(
“要添加到词典的单词不能包含空格”);
}
DictionaryNode currentNode=this.root;
for(char c:word.toCharArray()){
dictionarynodechild=currentNode.getChild(c);
if(child==null){
currentNode=currentNode.addChild(c);
}
否则{
currentNode=子节点;
}
}
//最后一个节点包含有效字的最后一个字符
//将该节点设置为有效单词的叶节点
currentNode.setLeaf();
}
/**
*
*要检查的@param字字符串是否为有效字
*@如果是有效字,则返回True
*/
公共布尔字(字符串字){
if(word==null){
抛出新的IllegalArgumentException(“要添加到字典中的单词不能为空”);
}
//消毒输入
if(word.contains(“”){
抛出新的IllegalArgumentException(
“要添加到词典的单词不能包含空格”);
}
DictionaryNode currentNode=this.root;
for(char c:word.toCharArray()){
dictionarynodechild=currentNode.getChild(c);
if(child==null){
返回false;
}
currentNode=子节点;
}
//如果最后一个字符是叶,则返回true
返回currentNode.isLeaf();
}
/**
*
*需要搜索的@param文本字符串
*@返回使用“text”搜索的有效单词字符串列表
*
*/
公共ArrayList getWords(字符串文本){
ArrayList words=新的ArrayList();
DictionaryNode currentNode=this.root;
对于(int i=0;i
randomWord(tree.root,L,i)