
public class Tree
{
   private int myData;

   private Tree myLeftChild;
   private Tree myRightChild;

   public Tree( int someData )
   {
      myData = someData;
   }

   public void insert( int someData )
   {
      if ( someData < myData )
      {
         if ( myLeftChild == null )
         {
            myLeftChild = new Tree( someData );
         }
         else
         {
            myLeftChild.insert( someData );
         }
      }
      else
      {
         if ( myRightChild == null )
         {
            myRightChild = new Tree( someData );
         }
         else
         {
            myRightChild.insert( someData );
         }
      }
   }

   public void print()
   {
      if ( myLeftChild != null )
      {
         myLeftChild.print();
      }

      System.out.println( myData );

      if ( myRightChild != null )
      {
         myRightChild.print();
      }
   }

   public int add()
   {
      int total = myData;
      if ( myLeftChild != null )
      {
         total += myLeftChild.add();
      }
      if ( myRightChild != null )
      {
         total += myRightChild.add();
      }
      return total;
   }

   public boolean search( int someData )
   {
      if ( myData == someData )
      {
         return true;
      }
      else
      {
         if ( someData < myData )
         {
            if ( myLeftChild == null )
            {
               return false;
            }
            else
            {
               return myLeftChild.search( someData );
            }
         }
         else
         {
            if ( myRightChild == null )
            {
               return false;
            }
            else
            {
               return myRightChild.search( someData );
            }
         }
      }
   }

   public static void main( String[] args )
   {
      Tree theTree = new Tree( 4 );
      theTree.insert( 5 );
      theTree.insert( 15 );
      theTree.insert( 3 );
      theTree.insert( 42 );
      theTree.insert( 1 );
      theTree.insert( -7 );

      theTree.print();

      System.out.println( theTree.add() );

      System.out.println( theTree.search( 42 ) );
      System.out.println( theTree.search( 35 ) );
   }
}
 
