title: Populating a binary search tree with a text file
body: <p>I'm trying to fill in a binary search tree with a text file, but i'm having alot of trouble implementing my insert function. Am i reading the input correctly or is it my code?</p>
  
  <p>Code for reading file:</p>
  
  <pre><code>import java.io.*;
  import java.util.Scanner;
  
  public class Driver {
      public static void main(String[]args) throws IOException
      {
          //Checks if there is a correct number of arguments passed through the command line.
          if (args.length != 1)
          {
              quitError("Tree command word arguments expected");
          } 
  
          String inputFile = args[0];
          BST btree = new BST();  
  
          try
          {
              BufferedReader input = new BufferedReader(new FileReader(inputFile));
  
              //Scans each word from the input and prints it out
              String word = input.readLine();
              while (word != null)
              {
                  btree.insert(word);
                  word = input.readLine();
              }
              return;
  
          } catch(FileNotFoundException filenotfoundexception) //Catches file not found exception
          {
              System.out.println("File not found.");
          }
          catch(IOException ioexception) //Catches input/output exception
          {
              System.out.println("File input error occured!");
          }
  
      }
  
      //Displays an error message, program exits
      public static void quitError(String msg)
      {
          System.out.println(msg);
          System.out.println("Program will now quit.");
          System.exit(0);
      }
  }
  </code></pre>
  
  <p>Code for the binary search tree node:</p>
  
  <pre><code>public class BSTNode {
      protected String data;
      protected BSTNode left, right;
  
      public BSTNode() 
      {
          left = null;
          right = null;
      }
  
      public BSTNode(String data)
      {
          this(data,null,null);
      }
  
      public BSTNode(String data, BSTNode lt, BSTNode rt) 
      {
          this.data = data; 
          left = lt; 
          right = rt;
      }
  }
  </code></pre>
  
  <p>Code for the binary search tree:</p>
  
  <pre><code>public class BST {
      protected BSTNode root = null;
      public BST(){}
  
      public void clear()
      {
          root = null;
      }
  
      public boolean isEmpty() 
      {
          return root == null;
      }
  
  
      public void insert(String data) 
      {
  BSTNode p = root, prev = null;
      while (p != null) {
          prev = p;
          if (p.data.compareTo(data) &lt; 0)
              p = p.right;
          else p = p.left;
      }
      if (root == null)
          root = new BSTNode(data);
      else if (prev.data.compareTo(data) &lt; 0)
          prev.right = new BSTNode(data);
      else prev.left  = new BSTNode(data);
      }   
  
      public void inorder()
      {
          inorder(root);
      }
  
      private void inorder(BSTNode p) 
      {
          if (p != null) 
          {
              inorder(p.left);
              System.out.print(p.data + " ");
              inorder(p.right);
          }
      }
  
      public void breadthFirst() 
      {
          BSTNode p = root;
          Queue queue = new Queue();
          if (p != null) 
          {
              queue.enqueue(p);
              while (!queue.isEmpty()) 
              {
                  p = (BSTNode) queue.dequeue();
                  System.out.print(p.data + " ");
                  if (p.left != null)
                      queue.enqueue(p.left);
                  if (p.right != null)
                      queue.enqueue(p.right);
              }
          }
      }
  
  
  }
  </code></pre>