Dictionary that implements a tree that has 26 branches

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jozule
    New Member
    • Apr 2010
    • 6

    #1

    Dictionary that implements a tree that has 26 branches

    i want to create a dictionary that implements a tree that has 26 branches, representing all the English letters, how can i do this?
  • Dheeraj Joshi
    Recognized Expert Top Contributor
    • Jul 2009
    • 1129

    #2
    Use following code.
    Google for more information.

    Code:
    boolean retVal = (new File("dirname")).mkdir();
    Regards
    Dheeraj Joshi

    Note:For recursive directories implement a logic.

    Comment

    • jozule
      New Member
      • Apr 2010
      • 6

      #3
      Originally posted by dheerajjoshim
      Use following code.
      Google for more information.

      Code:
      boolean retVal = (new File("dirname")).mkdir();
      Regards
      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

      • jkmyoung
        Recognized Expert Top Contributor
        • Mar 2006
        • 2057

        #4
        Strangely, I recently did a similar project. The tree code was something like:
        Code:
        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));
        		}
        	}
        }
        Hope that gives you an idea of how you can implement your own

        Comment

        • jozule
          New Member
          • Apr 2010
          • 6

          #5
          Originally posted by jkmyoung
          Strangely, I recently did a similar project. The tree code was something like:
          Code:
          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));
          		}
          	}
          }
          Hope that gives you an idea of how you can implement your own
          that nextBranch, what kind of data type is that

          Comment

          • jkmyoung
            Recognized Expert Top Contributor
            • Mar 2006
            • 2057

            #6
            Was an index I calculated somewhere else (s.charAt(0)-'a'). Have edited the code accordingly.

            Comment

            • jozule
              New Member
              • Apr 2010
              • 6

              #7
              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 you

              Comment

              • jkmyoung
                Recognized Expert Top Contributor
                • Mar 2006
                • 2057

                #8
                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

                • jozule
                  New Member
                  • Apr 2010
                  • 6

                  #9
                  wow, this seems complicated, please show me a sample code....please

                  Comment

                  • jkmyoung
                    Recognized Expert Top Contributor
                    • Mar 2006
                    • 2057

                    #10
                    Loading:
                    Code:
                    root = new Tree();
                    
                    for each word in dictionary
                      root.Process(word);
                    Then take each word in the document. Write a function much like process, recursing through the branches. we get one of these results:

                    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

                    Working...