title: BST in Java -- search Nodes by String
body: <p><em>I am creating a binary search tree using Nodes with MovieInfo objects as keys. MovieInfo objects are objects with three fields: ID, fullName, and shortName. The binary search tree will store information on an input text file containing every movie listed on IMDB. The insert would be based on an ID randomly associated with each movie. The search features would have a user input a String and pull up the other data of the object (shortName, ID, etc).
  <strong>Now -- the errors I am having are with the findExact method.</strong>
  First, no matter what my input is -- I get the message "Current Node is null" which means my starting currentNode is ALWAYS going null. The other issue is, I am not sure how to make sure my root node (the first currentNode searched for in the tree) is initialized properly. I have a feeling this might have something to do with my problem.
  The other problem might lie in how I am inserting the nodes in the first place.
  And for reference, the way I am testing this code/running in the text input file is IndexTester.java.</em></p>
  
  <p><strong>UPDATE</strong>: Okay. Everything works now. The only issue I am having now is that my findExact method seems to change the ID field of the MovieInfo class. So my search does not end up working. For example, if I input:</p>
  
  <pre><code>1568482 2 White Kids with Problems  2 White Kids with Problems
  1568487 Disorient   Disorient
  1568488 DIY Playgrounds DIY Playgrounds
  </code></pre>
  
  <p>and search "disorient," the return is "1568488 Disorient Disorient" (with the ID for the "2 White Kids with Problems" object). Additionally, since the ID is taken...DIY Playgrounds can't be searched for succesfully. The insertion method works, but the findExact method is giving me issues. Any ideas as to what might be causing this change in the ID?</p>
  
  <p><strong>The MovieInfo class (separate .java file) -- can't be edited</strong></p>
  
  <pre><code>public class MovieInfo {
      public String shortName;
      public String fullName;
      static int ID;
      public String key;
  
      public MovieInfo(int id, String s, String f) {
          ID = id;
          shortName = s;
          fullName = f;
      }
  }
  </code></pre>
  
  <p>BSTIndex.java -- the class in which I create the internal BST</p>
  
  <pre><code>import java.util.*;
  import java.io.*;
  
  public class BSTIndex extends MovieInfo {
  
      public Node root;
      public class Node{
          public MovieInfo movie;
          public Node left, right;
  
      public Node(MovieInfo movie)
      {
          this.movie = movie;
          //this.val = val;
          //this.N = N;
          }
      }
  
      public BSTIndex()
      /**
       * constructor to initialize the internal binary search tree. 
       * The data element should be an object of the type MovieInfo, described above.
       */
      {   
          super(0, "", "");
  
      }
  
  
      public MovieInfo findExact(String key) {
      return findExact(root, key);
  }
  
  private MovieInfo findExact(Node x, String key) {
      if (x == null) return null;
      int cmp = key.compareToIgnoreCase(x.movie.fullName);
      if      (cmp &lt; 0) return findExact(x.left, key);
      else if (cmp &gt; 0) return findExact(x.right, key);
      else              return x.movie;
  }
  
      public void insert(MovieInfo data)
      {
          if (data == null) {return; }
          root = insert(root, data);
      }
  
      public Node insert(Node x, MovieInfo data)
      {   
          if (x == null) return new Node(data);
          int cmp = data.ID - x.movie.ID;
          if (cmp &gt; 0 )       x.right = insert(x.right, data);
          else if (cmp &lt; 0)   x.left = insert(x.left, data);
          else if (cmp == 0)  x.movie = data;
          return x;
      }
  }
  </code></pre>
  
  <p><strong>IndexTester.java</strong></p>
  
  <pre><code>import java.io.FileInputStream;
  import java.io.FileNotFoundException;
  import java.util.Scanner;
  
  
  public class IndexTester {
  
      // Test program
      public static void main( String [ ] args ) throws FileNotFoundException
      {
          BSTIndex t = new BSTIndex( );
          Scanner scan = new Scanner(new FileInputStream(args[0]));
          long start = System.currentTimeMillis();
          int i=0;
  
          while(scan.hasNext()){
              String line = scan.nextLine();
              String [] fields = line.split("\t");
              int id = Integer.parseInt(fields[0].trim());
              String shortName = fields[1].trim();
              String fullName = fields[2].trim();
              MovieInfo info = new MovieInfo(id, shortName, fullName);
  
              t.insert(info);
              i++;
              if(i % 10000 == 0){
                  System.out.println("Inserted " + i + " records.");
              }
          }
          long end = System.currentTimeMillis();
          System.out.println("Index building complete. Inserted " + i + 
              " records.Elapsed time = " + (end - start )/1000 + " seconds.");
  
          Scanner input = new Scanner(System.in);
  
          System.out.println("Enter search string, end in a '*' for 
              prefix search. q to quit");
          while(input.hasNext()){
              String search = input.nextLine().trim();
              if(search.equals("q")) break;
              if(search.indexOf('*')&gt;0){
                  //call prefix search. 
  
                  MovieInfo item = t.findPrefix(search);
                  if(item==null) System.out.println("Not Found"); 
                  else System.out.println(item.ID + " " + item.shortName + " 
                              " + item.fullName);
  
              }
              else{
                  //call exact search, modify to return MovieInfo
                  MovieInfo item = t.findExact(search); 
                  if(item==null) System.out.println("Not Found");
                  else System.out.println(item.ID + " " + item.shortName + " 
                              " + item.fullName);
              }
          }
  
  
      }   
  } 
  </code></pre>