Results 1 to 3 of 3
  1. #1
    brlau is offline Member
    Join Date
    Dec 2011
    Posts
    9
    Rep Power
    0

    Default Homework Linked Binary Search Tree

    It's me again
    I completed the Findmax and Findmin statement below but I am getiing a
    incompatible types
    required: javafoundations.BSTNode<T>
    found: javafoundations.BTNode<T>

    on the line BSTNode<T> node = root;

    Java Code:
    //*******************************************************************
    //  LinkedBinarySearchTree.java       Java Foundations
    //
    //  Implements the binary tree using a linked representation.
    //*******************************************************************
    
    package javafoundations;
    
    import javafoundations.*;
    import javafoundations.exceptions.*;
    
    public class LinkedBinarySearchTree<T extends Comparable<T>>
       extends LinkedBinaryTree<T> implements BinarySearchTree<T>
    {
       //-----------------------------------------------------------------
       //  Creates an empty binary search tree.
       //-----------------------------------------------------------------
       public LinkedBinarySearchTree()
       {
          super();
       }
       //-----------------------------------------------------------------
       //  Creates a binary search tree with the specified element at its
       //  root.
       //-----------------------------------------------------------------
       public LinkedBinarySearchTree (T element)
       {
          root = new BSTNode<T>(element);
       }
    
       //-----------------------------------------------------------------
       //  Adds the specified element to this binary search tree.
       //-----------------------------------------------------------------
       public void add (T item)
       {
          if (root == null)
             root = new BSTNode<T>(item);
          else
             ((BSTNode)root).add(item);
       }
       //-----------------------------------------------------------------
       //  Removes and returns the element matching the specified target
       //  from this binary search tree. Throws an ElementNotFoundException
       //  if the target is not found.
       //-----------------------------------------------------------------
       public T remove (T target)
       {
          BSTNode<T> node = null;
    
          if (root != null)
             node = ((BSTNode)root).find(target);
    
          if (node == null)
             throw new ElementNotFoundException ("Remove operation failed. "
                + "No such element in tree.");
    
          root = ((BSTNode)root).remove(target);
    
          return node.getElement();
       }
       
       public T findMin(T target) {
           //Traverse the left side to the last element
           T result = null;
           if (root == null)
             throw new EmptyCollectionException ("FindMin operation "
                + "failed. The tree is empty.");
           else
           {
               BSTNode<T> node = root;
               
               While (BSTNode.getLeft() != null)
                       node  = BSTNode.getLeft();
                       
                       result = node.getElement();
           }
           return result;
           }
           
            
             
       public T findMax(T target) {
           T result = null;
           if (root == null)
             throw new EmptyCollectionException ("FindMin operation "
                + "failed. The tree is empty.");
           else
           {
               BSTNode<T> node = root;
               
               While (BSTNode.getRight() != null)
                       node  = BSTNode.getRight();
                       
                       result = node.getElement();
           }
           return result;
           }
       }
    Starting to go cross-eyed doing this

  2. #2
    brlau is offline Member
    Join Date
    Dec 2011
    Posts
    9
    Rep Power
    0

    Default Re: Homework Linked Binary Search Tree

    I also get
    javafoundations.LinkedBinarySearchTree is not abstract and does not override abstract method findMax() in javafoundations.BinarySearchTree

    on the Public class line

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Homework Linked Binary Search Tree

    required: javafoundations.BSTNode<T>
    found: javafoundations.BTNode<T>

    on the line BSTNode<T> node = root;
    You don't show the declaration of root. Assuming that it is declared in the super class and is of type BTNode<T>. In that case the compiler won't let you assign it to something declared to be BSTNode<T>. If BSTNode<T> is a subclass of BTNode<T> you could cast root. Of course at runtime it had better be the right sort of node.

    As an alternative consider making root private in the superclass. (Is there some reason why it isn't?) And declaring root as an instance variable in the LinkedBinarySearchTree<...> class of the right type viz BSTNode<T>.

    I also get
    javafoundations.LinkedBinarySearchTree is not abstract and does not override abstract method findMax() in javafoundations.BinarySearchTree
    Again I don't know how you declared the abstract method. But perhaps you did so without the (unused) T target argument.

Similar Threads

  1. Binary Tree of Linked Lists
    By Bmorebob in forum Advanced Java
    Replies: 1
    Last Post: 03-23-2011, 12:40 PM
  2. Replies: 0
    Last Post: 04-04-2010, 08:40 AM
  3. Binary search tree search method
    By chopo1980 in forum New To Java
    Replies: 2
    Last Post: 12-10-2009, 02:42 AM
  4. Binary Search Tree
    By anmadie in forum New To Java
    Replies: 5
    Last Post: 11-17-2009, 03:39 AM
  5. Binary Search Tree
    By michael_mke in forum New To Java
    Replies: 3
    Last Post: 12-04-2008, 03:03 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •