Hello, I am having a difficult time getting my Binary Search Tree class to compile. I added 2 functions "add" and "search". Before I added the 2 functions. It compiled successfully. If someone can help me in the right direction. I will definitely appreciate it!


2 Functions:

public BTNode<E> search(E target) 
{
    if (this.data == target) 
    {
    	return this;
    }
    else if (target < this.data && this.left != null) 
    {
    	return this.left.search(target);
    }
    else if (target >= this.data && this.right != null) 
    {
    	return this.right.search(target);
    }
    return null;
    }
    
    public boolean add(int value) 
   { 

       if (value == this.value) 

        return false; 

       else if (value <this.value) 
       { 

          if (left == null) 
         { 

            left = new BTNode(value); 

             return true; 

         } 
        else 

        return left.add(value); 

         } 
        else if (value > this.value) 
       { 

       if (right == null) 
       { 

        right = new BTNode(value); 

        return true; 

        } 
        else 

        return right.add(value); 

        } 

      return false; 

     }

Binary Search Tree Class:

package edu.colorado.nodes;

import java.util.Scanner;
import java.io.*;

public class BTNode<E>
{

   private E data;
   private BTNode<E> left, right;   


   public BTNode(E initialData, BTNode<E> initialLeft, BTNode<E> initialRight)
   {
      data = initialData;
      left = initialLeft;
      right = initialRight;
   }       
   
   
   public E getData( )   
   {
      return data;
   }
   

   public BTNode<E> getLeft( )
   {
      return left;                                               
   } 
   
   
   public E getLeftmostData( )
   {
      if (left == null)
         return data;
      else
         return left.getLeftmostData( );
   }
      

   public BTNode<E> getRight( )
   {
      return right;                                               
   } 

   public E getRightmostData( )
   {
      if (left == null)
         return data;
      else
         return left.getRightmostData( );
   }
   
   
   public void inorderPrint( )
   {
      if (left != null)
         left.inorderPrint( );
      System.out.println(data);
      if (right != null)
         right.inorderPrint( );
   }  


   public boolean isLeaf( )
   {
      return (left == null) && (right == null);                                               
   } 


   public void preorderPrint( )
   {
      System.out.println(data);
      if (left != null)
         left.preorderPrint( );
      if (right != null)
         right.preorderPrint( );
   } 
   

   public void postorderPrint( )
   {
      if (left != null)
         left.postorderPrint( );
      if (right != null)
         right.postorderPrint( );
      System.out.println(data);
   }   


   public void print(int depth)
   {
      int i;
   
      for (i = 1; i <= depth; i++)
         System.out.print("    ");
      System.out.println(data);
  
      if (left != null)
         left.print(depth+1);
      else if (right != null)
      {
         for (i = 1; i <= depth+1; i++)
            System.out.print("    ");
         System.out.println("--");
      }

      if (right != null)
         right.print(depth+1);
      else if (left != null)
      {
         for (i = 1; i <= depth+1; i++)
            System.out.print("    ");
         System.out.println("--");
      }
   }
   
   public BTNode<E> removeLeftmost( )
   {
      if (left == null)
         return right;
      else
      {
         left = left.removeLeftmost( );
         return this;
      }
   }

   public BTNode<E> removeRightmost( )
   {
      if (right == null)
         return left;
      else
      {
         right = right.removeRightmost( );
         return this;
      }
   }
       
   public void setData(E newData)   
   {
      data = newData;
   }                                                               
   

   public void setLeft(BTNode<E> newLeft)
   {                    
      left = newLeft;
   }
    
   public void setRight(BTNode<E> newRight)
   {                    
      right = newRight;
   }  

   
   public BTNode<E> search(E target) 
   	{
    if (this.data == target) 
    {
    	return this;
    }
    else if (target < this.data && this.left != null) 
    {
    	return this.left.search(target);
    }
    else if (target >= this.data && this.right != null) 
    {
    	return this.right.search(target);
    }
    return null;
    }
    
    public boolean add(int value) 
   { 

       if (value == this.value) 

        return false; 

       else if (value <this.value) 
       { 

          if (left == null) 
         { 

            left = new BTNode(value); 

             return true; 

          } 
        else 

        return left.add(value); 

         } 
        else if (value > this.value) 
       { 

       if (right == null) 
       { 

        right = new BTNode(value); 

        return true; 

        } 
        else 

        return right.add(value); 

        } 

      return false; 

     } 

    
  public static void main(String[] args) throws IOException
{
  



}
}

what error messages do you get?

Sorry that I forgot to provide that information. Here it is..

operator < cannot be applied to E,E line 165
operator >= cannot be applied to E,E line 169
cannot find symbol variable value line 179
cannot find symbol variable value line 183


Below, I copied the class from my jcreator, so you can specifically see where the lines are. The first 2 error lines are pointing to the 1st and 2nd "else if" lines in the search function near the bottom of the class. The last 2 error lines are pointing to the 1st "if" and "else if" in the add function. I'm still trying to figure it out. Also, before I added these functions, the program compiled properly.

Binary Search Tree Class:

package edu.colorado.nodes;

import java.util.Scanner;
import java.io.*;

public class BTNode<E>
{

   private E data;
   private BTNode<E> left, right;   


   public BTNode(E initialData, BTNode<E> initialLeft, BTNode<E> initialRight)
   {
      data = initialData;
      left = initialLeft;
      right = initialRight;
   }       
   
   
   public E getData( )   
   {
      return data;
   }
   

   public BTNode<E> getLeft( )
   {
      return left;                                               
   } 
   
   
   public E getLeftmostData( )
   {
      if (left == null)
         return data;
      else
         return left.getLeftmostData( );
   }
      

   public BTNode<E> getRight( )
   {
      return right;                                               
   } 

   public E getRightmostData( )
   {
      if (left == null)
         return data;
      else
         return left.getRightmostData( );
   }
   
   
   public void inorderPrint( )
   {
      if (left != null)
         left.inorderPrint( );
      System.out.println(data);
      if (right != null)
         right.inorderPrint( );
   }  


   public boolean isLeaf( )
   {
      return (left == null) && (right == null);                                               
   } 


   public void preorderPrint( )
   {
      System.out.println(data);
      if (left != null)
         left.preorderPrint( );
      if (right != null)
         right.preorderPrint( );
   } 
   

   public void postorderPrint( )
   {
      if (left != null)
         left.postorderPrint( );
      if (right != null)
         right.postorderPrint( );
      System.out.println(data);
   }   


   public void print(int depth)
   {
      int i;
   
      for (i = 1; i <= depth; i++)
         System.out.print("    ");
      System.out.println(data);
  
      if (left != null)
         left.print(depth+1);
      else if (right != null)
      {
         for (i = 1; i <= depth+1; i++)
            System.out.print("    ");
         System.out.println("--");
      }

      if (right != null)
         right.print(depth+1);
      else if (left != null)
      {
         for (i = 1; i <= depth+1; i++)
            System.out.print("    ");
         System.out.println("--");
      }
   }
   
   public BTNode<E> removeLeftmost( )
   {
      if (left == null)
         return right;
      else
      {
         left = left.removeLeftmost( );
         return this;
      }
   }

   public BTNode<E> removeRightmost( )
   {
      if (right == null)
         return left;
      else
      {
         right = right.removeRightmost( );
         return this;
      }
   }
       
   public void setData(E newData)   
   {
      data = newData;
   }                                                               
   

   public void setLeft(BTNode<E> newLeft)
   {                    
      left = newLeft;
   }
    
   public void setRight(BTNode<E> newRight)
   {                    
      right = newRight;
   }  

   
   public BTNode<E> search(E target) 
   	{
    if (this.data == target) 
    {
    	return this;
    }
    else if (target < this.data && this.left != null) 
    {
    	return this.left.search(target);
    }
    else if (target >= this.data && this.right != null) 
    {
    	return this.right.search(target);
    }
    return null;
    }
    
    public boolean add(int value) 
   { 

       if (value == this.value) 

        return false; 

       else if (value <this.value) 
       { 

          if (left == null) 
          	{ 

            left = new BTNode(value); 

             return true; 

            } 
        else 

        return left.add(value); 

         } 
        else if (value > this.value) 
       { 

       if (right == null) 
       	{ 

        right = new BTNode(value); 

        return true; 

        } 
        else 

        return right.add(value); 

        } 

      return false; 

     } 

    
  public static void main(String[] args) throws IOException
{
  



}
}
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.