i want to create a dictionary that implements a tree that has 26 branches, representing all the English letters, how can i do this?
Dictionary that implements a tree that has 26 branches
Collapse
X
-
Use following code.
Google for more information.
RegardsCode:boolean retVal = (new File("dirname")).mkdir();
Dheeraj Joshi
Note:For recursive directories implement a logic. -
i do not understand that, let me be clear. I want to create a dictionary that implements a tree that has 26 branches, representing all the English letters, how can i do this? as in if a person writes a word starting with B, it would start searching for the word in the second branch......Comment
-
Strangely, I recently did a similar project. The tree code was something like:
Hope that gives you an idea of how you can implement your ownCode:static class Tree{ Tree[] branch; int count; Tree(){ count = 0; } /** * @param s a non-null string. **/ Tree(String s){ if(s.length()==0){ count = 1; return; } else { branch = new Tree[26]; branch[s.charAt(0)-'a'] = new Tree(s.substring(1)); } } /** * @param s a non-null string. **/ void Process(String s) { if(s.length()==0){ count++; } else if (branch == null){ branch = new Tree[26]; branch[s.charAt(0)-'a'] = new Tree(s.substring(1)); }else{ branch[s.charAt(0)-'a'].Process(s.substring(1)); } } }Comment
-
that nextBranch, what kind of data type is thatStrangely, I recently did a similar project. The tree code was something like:
Hope that gives you an idea of how you can implement your ownCode:static class Tree{ Tree[] branch; int count; Tree(){ count = 0; } /** * @param s a non-null string. **/ Tree(String s){ if(s.length()==0){ count = 1; return; } else { branch = new Tree[26]; branch[nextBranch] = new Tree(s.substring(1)); } } /** * @param s a non-null string. **/ void Process(String s) { if(s.length()==0){ count++; } else if (branch == null){ branch = new Tree[26]; branch[nextBranch] = new Tree(s); }else{ branch[s.charAt(0)-'a'].Process(s.substring(1)); } } }Comment
-
Thanks so far, I hope I am not asking to much, however this is just a tree with 26 branches as I asked but It is not implemented with a dictionary. How can I load a dictionary file called “dictionary.txt ” into this tree with all the words starting with A inserted in the first branch and so on till the 26th branch. If possible, I would also like to know how to compare the document file called “document. txt” with the “Dictionary.txt ” to check spelling errors. thank youComment
-
Take a root node; add the word by calling Process(s)
eg Process ("draw")
Creates a Tree for d -> Creates a subtree for r -> creates a subtree for a -> creates a subtree for w -> set count to 1.
So the root has 26 branches, and the 4th branch goes to the subtree for d.
the tree at d has 26 branches with a subbranch for the subtree for r, etc...
Then when you get to the w node, the count is 1, meaning there is an actual word there.
Notice at each of the subtrees which aren't at the end, the count is 0.
Then if you added 'drawback', you would recurse down this same subtree until you reached the w node. Then you would create a subtree for b -> create a subtree for a -> create a subtree for c -> create a subtree for k, set count to 1.
So you do this for all the dictionary nodes.Comment
-
Loading:
Then take each word in the document. Write a function much like process, recursing through the branches. we get one of these results:Code:root = new Tree(); for each word in dictionary root.Process(word);
1. We reach the end tree and :
a. count is 0. That means this is not a full word. Return false.
b. count is 1. That means this is a word in the dictionary! Return true.
2. We reach a node that has no branches. Then return false.
3. We reach a node that has branches, but not at our node. Then return false.
Examples: Say we've loaded only the word "draw".
1a. Search draw. -> we find it!
1b. Search dra -> we get up to the node for A, but count is 0 here, so we return false.
2. Search draws. -> we get up to draw, but the W node has no branches, so we return false
3. Search dragon -> we get up to the A node, but there is only a W node, not a G node, so we return false.
I'm not writing the complete search function for you, so please don't ask. If you can't figure out how to write it, then you shouldn't use the base code yet.
.. complicated? How did you expect to write this code? Or did you only want a single tree branch index, with 26 seperate dictionaries structures; one for each letter?Comment
Comment