Recently tried to completely out of C + +, instead of using only Java brush problems. This is the first tree I made in Java. The title is here. Say a few ways to do the problem.
1. The use of hashtable certificates of violence is clearly a waste of space, each of which should be stored only once. Violence is matched by a regex and applies only to a hash dictionary. Each letter in each order only once, in the brain to think about it, you can think of this is to build a 26-fork tree.
2. TreeNode In addition to the alphabetic value and size 26 of the TreeNode array, it should also contain an integer min, used to indicate the length of the last letter of the current letter to the shortest word in the branch, such as ABC, then the min of the letter B should be 1, indicating the time of the lookup, You'll need to look down another layer before you can find AB and it will return true, but in fact we haven't stored the word AB.
3. A typical recursive lookup is used to find the tree structure. Match '. ' The difference from matching the other 26 letters is that I need to find all the TreeNode subtrees that are currently in use, and only one Shang tree can find success.
4. The most important is the design of the TreeNode class:
public class TreeNode {
char val;
int min;
Treenode[] Next;
TreeNode (char c) {
val = c;
Next = new treenode[26];
}
The most important of these is the TreeNode constructor, when we execute
Next = new TREENODE[26];
, we just declared the size of the next array, not allocating memory, which is exactly what we want. (If you create an array of non basic types, you create a reference array.) -Java Programming Idea 5.8 array initialization)
Paste Code:
public class Worddictionary {public class TreeNode {char val;
int min;
Treenode[] Next;
TreeNode (char c) {val = c;
Next = new TREENODE[26];
} public TreeNode root;
Public Worddictionary () {root = new TreeNode (');
}//Adds a word into the data structure.
public void Addword (String word) {int len = word.length ();
int i;
int index;
char c;
TreeNode current = root;
int min_len = len;
for (i=0;i<len;i++) {min_len--;
c = Word.charat (i);
index = (int) (c-97);
if (current.next[index]==null) {Current.next[index] = new TreeNode (c);
Current.next[index].min = Min_len;
else if (current.next[index].min>min_len) current.next[index].min = Min_len;
current = Current.next[index]; }//ReturnsIf the word is in the data structure.
A Word could//contain the dot character '. ' to represent any one letter.
public boolean search (String word) {return Searchtree (word, root);
public boolean searchtree (String Word, TreeNode current) {int len = word.length ();
if (len==0) {if (current.min>0) return false;
else return true;
char c = word.charat (0); if (c!= '. ')
{int index = (int) (c-97);
if (current.next[index]==null) return false;
else if (current.next[index].val!=c) return false;
else {return Searchtree (word.substring (1), Current.next[index]);
} else {int i; for (i=0;i<26;i++) {if (current.next[i]!=null) {if (Searchtree) (word.substring (1),
Current.next[i]) ==true return true; } return FalsE }}//Your Worddictionary object would be instantiated and called as such://worddictionary worddictionary = new W
Orddictionary ();
Worddictionary.addword ("word"); Worddictionary.search ("pattern");