-1

I am new to tries and testing how it works. Right now I am building a contact list. I add 'Prashanth' and 'Pradep' to the trie and when I search 'Pra' I should receive the count as two. My approach is having the variable size in each node and returning it when the string of same length is found. There are some unnecessary things/variables like payload etc which I used for debugging. The problem I found is when I store a character and a node in hashmap, empty node is getting stored. So I am getting 0 as answer all the 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 which I get If i run this code. ( I will get 0 if I alter a bit)

Exception in thread "main" java.lang.NullPointerException
at tries$Node.addNode(tries.java:35)
at tries.main(tries.java:66)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)

2 Answers2

0

You can do something like this:

import java.util.HashMap;
import java.util.Map;


public class ContactList {

    Map<String, String> contactList; 

    public ContactList() {
        //initialize the 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 and make the name lowercase, then compare it to the 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 it 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"));

    }
}
triForce420
  • 669
  • 12
  • 29
0

You check if the current is null and then if it is, you call something on it. See this block:

   if(current== null)
    {
         current.payload = 11;
        this.setNode(s.charAt(index),current);
    }

Notice the current == null and then current.payload. You can't call anything on null, it's a representation of nothing :)

What you probably want to do is create a new Node, initialize it, put it into the map and then assign it to current

Michael Bláha
  • 363
  • 3
  • 8