I am working on implementing a binary search tree as an array implementation, but I am having trouble with correctly printing out all of the students' records using the showAll() method and my application also doesn't print out anything for the insert and fetch methods. What am I doing wrong? Here is my application so far. I also have one other question. Do my java program correctly implements a binary search tree as an array implementation? I need constructive feedback.

public class Listing implements Comparable<Listing>

{ private String name;  // key field
  private int ID;
   private double GPA;
  public Listing(String n, int id, double gpa)
  {  name = n;

      ID = id;
     GPA = gpa;

  }

  public String toString()
  {   return("Name is " + " "  + name +

                    "\nID is" + " " + ID +
                    "\nGPA is" + " "  + GPA + "\n");

   }
   public Listing deepCopy()

   {  Listing clone = new Listing(name, ID, GPA);
      return clone;
   }
   public int compareTo(Listing other)
   {  
       return this.name.compareTo(other.getKey());
   }
   public String getKey()
   {  
       return name;
   }
 }// end of class Listing

 public class BinaryTreeArray
{ 
   private Listing[] data; 
   private int size; 
   private int i = 0; 

  public BinaryTreeArray()
  { 
      size = 100; 
      data = new Listing[size]; 
  }
  public void showAll(){

    for(; i<size; i++)
      System.out.print(data[i] + " ");

   } 
  public boolean insert(Listing newListing)
  { 
     while(i < size && data[i]!= null)
     { 
       if(data[i].getKey().compareTo(newListing.getKey()) > 0)
         i = 2 * i + 1; 
        else 
         i = 2 * i + 2; 
     } 
     if(i >= size) 
       return false; 
     else 
     { 
         data[i] = newListing.deepCopy();
          return true;
      } 
  }
  public Listing fetch(String targetKey)
  {  
    while(i< size && data[i]!= null && data[i].getKey()!=targetKey)
    {
        if(data[i].getKey().compareTo(targetKey) > 0)
          i = 2 * i + 1;
       else
          i = 2 * i + 2;   
    }
    if(i >= size || data[i] == null)
      return null; 
    else 
      return data[i].deepCopy();
  }

} 

import java.util.Scanner;

public class MainBinaryTreeArray
{
  public static void main(String[] args)
  {  

     int choice; 
     Scanner scan= new Scanner(System.in);
     BinaryTreeArray data = new BinaryTreeArray(); 

     Listing l1  = new Listing("Carol",    4354,  3.2);
     Listing l2  = new Listing("Timothy",  2353,  4.0);
     Listing l3  = new Listing("Dean",     4544,  2.4);
     Listing l4  = new Listing("Sue",      3445,  3.0);

   do
   { 

       // Choose which operation by entering a number 

      System.out.println("*****************(Menu Operations:)******************"); 
      System.out.println(" (Press 1). Insert."); 
      System.out.println(" (Press 2). Fetch."); 
      System.out.println(" (Press 5). Output all student's records."); 
      System.out.println(" (Press 6). Exit the program."); 
      System.out.println("Enter your choice: "); 
      choice = scan.nextInt(); 
      switch(choice)
      { 
        case 1: 
        System.out.println("The student's info that's inserted: " + data.insert(l1)); 
        break; 
        case 2:
        System.out.println("The student's info that's fetched: " + data.fetch("Timothy"));
         break; 
        case 5: 
        System.out.print("All students' info that is output: " ); 
        data.showAll();
      }

  }while(choice!=6);

 }

}

Recommended Answers

All 10 Replies

How did you get this post? Do you think you can help still?

How. Just googled a few lines of your code to see if others are commenting as duplicating answers and effort is not a good thing.

As to your issue, you are in Java so you would debug it. How?
Read https://www.eclipse.org/forums/index.php/t/81620/ so you can step through your code, a line at a time and see where your variable or items go off the rails.

Few will read 100+ lines of code. Why? Read https://www.daniweb.com/programming/threads/435023/read-this-before-posting-a-question

Reduce your code to just where the problem is.

Compiling is not the issue, but more semantics of the program. Meaning it doesn't output the information that I wanted it to. All of it compiles fine though. Here are the lines of code that I don't know why they aren't printing student's info instead of true or false boolean values and also I don't know why I can't output every single student using my showAll() method. Here is the updated latest code.

 switch(choice)
      { 
        case 1: 

        System.out.println("The student's info that's inserted: " + data.insert(l1)); 
        break; 
        case 2:
        System.out.println("The student's info that's fetched: " + data.fetch("Timothy"));
         break; 
        case 5: 
        System.out.print("All students' info that is output: " ); 
        data.showAll();
      }

Line 49 you fail to initialise i (which is also used in many other methods) so your loop may or may not execute any number of times.
Insert prints "true" because that’s what the insert method returns

as to your remark: debugging has nothing to do with verifying whether it compiles or not.
it means it compiled and it can run.

debugging is running an application, but with the possibility to enter breakpoints.
This can come in handy for several reasons:

  1. If your flow does not stop on a breakpoint, that means that this line of code is never reached (so that would also explain why it doesn't behave as expected)
  2. you have the possibility of going through the flow of your application step by step, by going over every single line of code your application runs, so you can verify your code runs in the order you assume it does
  3. at each point, you are able to verify the value of the variables. You can check them and see whether or not they actually contain the values you assume they would/should contain at that point in the flow.

@ stultuske I figured it out by myself. That's for all input!

It was the showAll() that was printing a whole lot of nulls which didn't need to be in my program. The solution was to do this:

for(int i=0; i<size; i++) 

     {
            if (data[i] != null)
              System.out.print(data[i] + " "); 
      }

However, not sure why its prints the student, Carol, twice in the showAl() method. Any suggestions?

Given the simplicity of the showAll loop, there really are two instances of Carol, so either that's what the test run input data had, or its a bug in the insert method. Without seeing the actual test data I can't comment on that.
But your insert and fetch methods have the same uninitialised loop var i problem as showAll had.

ps: Why the deepCopies? If I put something in a tree and later retrieve it I would expect the original object, not a copy of it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.