Getting a list of words from a Trie

后端 未结 10 984
野性不改
野性不改 2021-01-02 15:29

I\'m looking to use the following code to not check whether there is a word matching in the Trie but to return a list all words beginning with the prefix inputted by the use

10条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-02 15:59

    I faced this problem while trying to make a text auto-complete module. I solved the problem by making a Trie in which each node contains it's parent node as well as children. First I searched for the node starting at the input prefix. Then I applied a Traversal on the Trie that explores all the nodes of the sub-tree with it's root as the prefix node. whenever a leaf node is encountered, it means that the end of a word starting from input prefix has been found. Starting from that leaf node I iterate through the parent nodes getting parent of parent, and reach the root of the subtree. While doing so I kept adding the keys of nodes in a stack. In the end I took the prefix and started appended it by popping the stack. I kept on saving the words in an ArrayList. At the end of the traversal I get all the words starting from the input prefix. Here is the code with usage example:

    class TrieNode
    {
        char c;
        TrieNode parent;
        HashMap children = new HashMap();
        boolean isLeaf;
    
        public TrieNode() {}
        public TrieNode(char c){this.c = c;}
    }
    

    -

    public class Trie
    {
        private TrieNode root;
        ArrayList words; 
        TrieNode prefixRoot;
        String curPrefix;
    
        public Trie()
        {
            root = new TrieNode();
            words  = new ArrayList();
        }
    
        // Inserts a word into the trie.
        public void insert(String word) 
        {
            HashMap children = root.children;
    
            TrieNode crntparent;
    
            crntparent = root;
    
            //cur children parent = root
    
            for(int i=0; i children = root.children; 
            TrieNode t = null;
            for(int i=0; i hstack = new Stack(); 
    
              while(altair != prefixRoot)
              {
                //println(altair.c);
                hstack.push( Character.toString(altair.c) );
                altair = altair.parent;
              }
    
              String wrd = curPrefix;
    
              while(hstack.empty()==false)
              {
                wrd = wrd + hstack.pop();
              }
    
              //println(wrd);
              words.add(wrd);
    
            }
    
             Set kset = node.children.keySet();
             //println(node.c); println(node.isLeaf);println(kset);
             Iterator itr = kset.iterator();
             ArrayList aloc = new ArrayList();
    
           while(itr.hasNext())
           {
            Character ch = (Character)itr.next();  
            aloc.add(ch);
            //println(ch);
           } 
    
         // here you can play with the order of the children
    
           for( int i=0;i

    Example

    Trie prefixTree;
    
    prefixTree = new Trie();  
    
      prefixTree.insert("GOING");
      prefixTree.insert("GONG");
      prefixTree.insert("PAKISTAN");
      prefixTree.insert("SHANGHAI");
      prefixTree.insert("GONDAL");
      prefixTree.insert("GODAY");
      prefixTree.insert("GODZILLA");
    
      if( prefixTree.startsWith("GO")==true)
      {
        TrieNode tn = prefixTree.searchNode("GO");
        prefixTree.wordsFinderTraversal(tn,0);
        prefixTree.displayFoundWords(); 
    
      }
    
      if( prefixTree.startsWith("GOD")==true)
      {
        TrieNode tn = prefixTree.searchNode("GOD");
        prefixTree.wordsFinderTraversal(tn,0);
        prefixTree.displayFoundWords(); 
    
      }
    

提交回复
热议问题