Results 1 to 3 of 3
- 12-21-2011, 05:13 AM #1
Member
- Join Date
- Dec 2011
- Posts
- 9
- Rep Power
- 0
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;
Starting to go cross-eyed doing thisJava 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; } }
- 12-21-2011, 05:33 AM #2
Member
- Join Date
- Dec 2011
- Posts
- 9
- Rep Power
- 0
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
- 12-21-2011, 08:14 AM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
Re: Homework Linked Binary Search Tree
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.required: javafoundations.BSTNode<T>
found: javafoundations.BTNode<T>
on the line BSTNode<T> node = root;
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>.
Again I don't know how you declared the abstract method. But perhaps you did so without the (unused) T target argument.I also get
javafoundations.LinkedBinarySearchTree is not abstract and does not override abstract method findMax() in javafoundations.BinarySearchTree
Similar Threads
-
Binary Tree of Linked Lists
By Bmorebob in forum Advanced JavaReplies: 1Last Post: 03-23-2011, 11:40 AM -
Data Structures(Binary Search Tree to AVL Tree)ASAP pls
By jfAdik in forum Forum LobbyReplies: 0Last Post: 04-04-2010, 07:40 AM -
Binary search tree search method
By chopo1980 in forum New To JavaReplies: 2Last Post: 12-10-2009, 01:42 AM -
Binary Search Tree
By anmadie in forum New To JavaReplies: 5Last Post: 11-17-2009, 02:39 AM -
Binary Search Tree
By michael_mke in forum New To JavaReplies: 3Last Post: 12-04-2008, 02:03 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks