title: Binary Search Tree find method returning not found
body: <p>This is a homework assignment i'm working on and I'm having a little trouble with it. I've implemented my own version of a binary search tree rather than using JDK. I'm inserting multiple student objects into the binary search tree in terms of the student's ID, which is a type string. I'm not getting any compile errors but the program keeps returning that the value is not found when it should be the fourth student that was inserted into the tree. I've implemented all of the find methods already, but not sure where I'm going wrong with them, since the output should be saying that the value was found.</p>
  
  <p>Output:</p>
  
  <pre><code>run:
  11114 not found
  BUILD SUCCESSFUL (total time: 0 seconds)
  </code></pre>
  
  <p>Homework5.class / main:</p>
  
  <pre><code>package homework5;
  
  import java.util.LinkedList;
  
  public class Homework5 {
  
      static Roster rost = new Roster();
  
      public static void main(String[] args) {
  
          addStudent();
          lookupStudent("11114");
      }
  
       // add students to the roster
      static void addStudent() {
          rost.addStudent(new Student("11111", "Jon", "Benson"));
          rost.addStudent(new Student("11112", "Erick", "Hooper"));
          rost.addStudent(new Student("11113", "Sam", "Shultz"));
          rost.addStudent(new Student("11114", "Trent", "Black"));
          rost.addStudent(new Student("11115", "Michell", "Waters"));
          rost.addStudent(new Student("11116", "Kevin", "Johnson"));
      }
  
      // lookup a student in the roster
      static void lookupStudent(String id) {
          if (rost.find(id) != null) {
              System.out.println(id + " found");
          } else {
              System.out.println(id + " not found");
          } 
      }
  }
  </code></pre>
  
  <p>Student.class</p>
  
  <pre><code>class Student implements Comparable&lt;Student&gt; {
  
      String id;
      String firstName;
      String lastName;
  
      Student(String id, String fName, String lName) {
          this.id = id;
          this.firstName = fName;
          this.lastName = lName;
      }
  
      public String getName() {
          return lastName;
      }
  
      public void setName(String lName) {
          this.lastName = lName;
      }
  
      public String getId() {
          return id;
      }
  
      public void setId(String id) {
          this.id = id;
      }
  
      @Override
      public int compareTo(Student other) {    
          return this.getId().compareTo(other.getId());
      }
  
      public void addCourse(String id) {
          LinkedList list = new LinkedList();
          list.add(id);
      }
  }
  </code></pre>
  
  <p>Roster.class</p>
  
  <pre><code>class Roster {
  
      Student root;
      int numStudents;
  
      BST&lt;Student&gt; roster = new BST&lt;&gt;();
  
      public Roster() {
          root = null;
          numStudents = 0;
      }
  
      public void addStudent(Student st) {
          roster.insert(st);
          numStudents++;
      }
  
      public Student find(String id) {
          roster.find(id);
          return null;
      }
  </code></pre>
  
  <p>BST.java</p>
  
  <pre><code>package homework5;
  
  class BST&lt;Roster extends Comparable&gt; {
  
      private Node root;
  
      public BST() {
          root = null;
      }
  
      // Generic find method
      public Node find(String id) {
  
          Node current = root;
  
          while (id.compareTo(current.element.getId()) != 0) {
          if (id.compareTo(current.element.getId()) &lt; 0) {
              current = current.left;
          }
          else {
              current = current.right;
          }
          if (current == null) {
              return null;
          }
      }
      return current;
  
      }
  
      public void insert(Student st) {
          Node newNode = new Node(st);
  
          if (root == null) {
              root = newNode;
          } else {
              Node current = root;
              Node parent = null;
  
              while (true) {
                  parent = current;
                  if (st.compareTo(current.element) &lt; 0) {
                      current = current.left;
                      if (current == null) {
                          parent.left = newNode;
                          return;
                      }
                  } else {
                      current = current.right;
                      if (current == null) {
                          parent.right = newNode;
                          return;
                      }
                  }
              }
          }
      }
  
      // Recursive method - traverse generic BST 
      // While root is not equal to null visit left node and print value
      // of root, then visit right node. Repeat until root becomes null
      private void inOrder(Node localRoot) {
          if (localRoot != null) {
              inOrder(localRoot.left);
              System.out.print(localRoot.element + " ");
              inOrder(localRoot.right);
          }
      }
  
  }
  
  class Node {
  
      protected Student element;
      protected Node left;
      protected Node right;
  
      public Node(Student st) {
          element = st;
      }
  }
  </code></pre>