Search code examples
javanodescountertrie

Contains method and counter update


So I am finalizing my program where the test goes through a list of words in a test program and using prefixes, I weed through it and only return the ones matching.

Here is the following code for my contains method and snippets of the testing method, I am just wondering what am I doing wrong?

//contains method

public boolean containsKey(TrieMapNode current, String curKey) {
    // recursively get the value for the current node
    String value = get(current,curKey);
    // if value if null or empty, key is not found return false
    if(value == null) {
        return false;
    }else if (value.equals("")) {
        return false;
    } else {
        return true;
    }
}

//test program

    import java.util.HashSet;
    import java.util.ArrayList;

public class TriMapTester{
    public static void main(String[] args){
    HashSet<String> posWords = getPositiveWords();
    HashSet<String> negWords = getNegativeWords();
    TrieMapInterface map = new TrieMap();

    //Add all of the positive words
    for(String word: posWords){
      map.put(word, word);
    }

    int countErrors = 0;
    int totalErrors = 0;

    //Check that the map contains all positive words
    for(String s: posWords){
      if(!map.containsKey(s)){
        countErrors++;
      }
    }
    System.out.println("Number of missed keys after searching all positive words: " + countErrors);

    totalErrors += countErrors;
    countErrors = 0;

    //Check that the correct value is associated with each key
    for(String s: posWords){
      if(!map.containsKey(s) || !map.get(s).equals(s)){
        countErrors++;
      }
    }
    System.out.println("Number of incorrect values for keys: " + countErrors);

    totalErrors += countErrors;
    countErrors = 0;

    //Check negative words. Make sure they do not show up in map if they are not also in positive words.
    for(String s: negWords){
      if(map.containsKey(s) && !posWords.contains(s)){
        countErrors++;
      }
    }
    System.out.println("Number of incorrectly found keys after searching all negative words: " + countErrors);
    totalErrors += countErrors;

    //Check the values returned for a number of prefix values
    //Compares the returned result to the true result
    String[] searchPrefixes = {"add", "bril", "cat", "cri", "derri", "mar", "tra", "lor", "marveled", "rit"};
    for(String prefix: searchPrefixes){
      System.out.println("Getting values for prefix: " + prefix);
      ArrayList<String> realResult = matchPrefix(prefix, posWords);
      ArrayList<String> yourResult = map.getValuesForPrefix(prefix);
      System.out.println("Result should contain: " + realResult);
      System.out.println("    Your map returned: " + yourResult);
      countErrors = countDifference(realResult, yourResult);
      totalErrors += countErrors;
      System.out.println("Error Count: " + countErrors);
    }
    System.out.println("Total Errors: " + totalErrors);
  }

  public static int countDifference(ArrayList<String> realResult, ArrayList<String> yourResult){
    int result = 0;
    for(String s: realResult){
      if(!yourResult.contains(s)){
        result++;
      }
    }

    for(Object s: yourResult){
      if(!realResult.contains((String)s)){
        result++;
      }
    }
    return result;
  }

  public static ArrayList<String> matchPrefix(String prefix, HashSet<String> words){
   ArrayList<String> result = new ArrayList<String>();

    for(String s: words){
      if(s.startsWith(prefix)){
        result.add(s);
      }
    }
   return result;
  }

  public static HashSet<String> getPositiveWords(){
HashSet<String> result = new HashSet<String>();
String[] pos = new String[]{"abound", "abounds", "abundance", "abundant", "accessable", "accessible", "acclaim", "acclaimed", "acclamation"}; 
for(String s: pos){
  result.add(s);
}
return result;
 }

  public static HashSet<String> getNegativeWords(){
HashSet<String> result = new HashSet<String>();
String[] neg = new String[]{"abnormal", "abolish", "abominable", "abominably", "abominate", "abomination", "abort", "aborted", "aborts", "abrade", "abrasive", "abrupt", "abruptly", "abscond", "absence", "absent-minded"}; 
for(String s: neg){
  result.add(s);
}
return result;
  }

Now the output I get when running the test is the correct containing words however the number of errors is still 0, despite the contains method so I am unsure what I am doing wrong. Any help would be greatly appreciated


Solution

  • Issue is with this line

    if(map.containsKey(s) && !posWords.contains(s))
       countErrors++;
    

    map & posWords is containing the same data, and you are once checking it to be true and also checking it to be false which will never execute countErrors++. This is why countErrors is 0.