algorithm - Java Tries : Need a better approach -


this question has answer here:

i new tries , testing how works. right building contact list. add 'prashanth' , 'pradep' trie , when search 'pra' should receive count two. approach having variable size in each node , returning when string of same length found. there unnecessary things/variables payload etc used debugging. problem found when store character , node in hashmap, empty node getting stored. getting 0 answer time.

 public class tries {  public static class node {      hashmap<character, node> children = new hashmap<>();     boolean endofword = false;     int size =0;     int payload = 10;       public void setnode(char c, node n) {       children.put(c,n);     }      public node getnode(char c) {         return children.get(c);     }      public void addnode( string s,int index) {         node current = children.get(s.charat(index));         size++;         if(index ==s.length()-1)         {             endofword = true;             return;         }         if(current== null)          {              current.payload = 11;             this.setnode(s.charat(index),current);         }         addnode(s,index+1);      }      public int findcount(string s, int index) {         node current = children.get(s.charat(index));          if(index ==s.length()-1)         {             current.endofword = true;             return current.size;         }         if(current == null)         {             return 0;         }           return findcount(s,index+1);     } } public static void main(string args[]) {     string c1 = "prashanth";     string c2=  "pradep";     node n = new node();     //tries t = new tries();      n.addnode(c1,0);     n.addnode(c2,0);      system.out.println(n.findcount("pra",0));   } 

}

error if run code. ( 0 if alter bit)

exception in thread "main" java.lang.nullpointerexception @ tries$node.addnode(tries.java:35) @ tries.main(tries.java:66) @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:57) @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43) @ java.lang.reflect.method.invoke(method.java:606) @ com.intellij.rt.execution.application.appmain.main(appmain.java:147) 

you can this:

import java.util.hashmap; import java.util.map;   public class contactlist {      map<string, string> contactlist;       public contactlist() {         //initialize map of contact lists.          contactlist = new hashmap<string, string>();     }      public void addcontact(string name, string number){         contactlist.put(name, number);     }      public map<string,string> findbyname(string name){          map<string,string> resultmap = new hashmap<string, string>();          for(string key : contactlist.keyset()){             //trim , make name lowercase, compare lowercased string             if(key.trim().tolowercase().contains(name.tolowercase())){                 resultmap.put(key, contactlist.get(key));             }         }         return resultmap;      }      public int countresult(string name){         //return size of found map         return findbyname(name).size();     }    } 

then use on main method:

public class contactmain {      public static void main(string args[])     {         string c1 = "prashanth";         string c2=  "pradep";         contactlist contacts = new contactlist();         contacts.addcontact(c1, "1234567890");         contacts.addcontact(c2, "12345678");         system.out.println(contacts.countresult("pra"));      } } 

Comments

Popular posts from this blog

php - How to add and update images or image url in Volusion using Volusion API -

javascript - jQuery UI Splitter/Resizable for unlimited amount of columns -

javascript - IE9 error '$'is not defined -