/* * Tree.java * */ import java.util.Iterator; import java.util.Vector; public abstract class Tree<E> implements Iterable<E> { protected class Node<T> { protected Node(T data) { this.data = data; } protected T data; protected Node<T> left; protected Node<T> right; } public void insert(E data) { Node<E> temp = new Node<E>(data); root = insert(root, temp); } protected abstract Node<E> insert(Node<E> curr, Node<E> node); public Iterator<E> iterator() { Vector<E> vector = new Vector<E>(); traverse(vector, root); return vector.iterator(); } public void remove(E data) { root = remove(root, data); } protected abstract Node<E> remove(Node<E> curr, E data) throws TreeException; public boolean search(E key) { return search(root, key); } protected abstract boolean search(Node<E> curr, E key); private void traverse(Vector<E> vector, Node<E> curr) { if (curr != null) { traverse(vector, curr.left); vector.add(curr.data); traverse(vector, curr.right); } } private Node<E> root; } /* * * TreeException.java * */ public class TreeException extends RuntimeException { public TreeException() {} public TreeException(String msg) { super(msg); } }
/* * * BinarySearchTree.java * */ public class BinarySearchTree<E extends Comparable<? super E>> extends Tree<E> { private Node insert(Comparable temp, Node curr) { if(curr == null) curr = new Node(temp); else if(temp.compareTo(curr.data) < 0) curr.left = insert(temp, curr.left); else if(temp.compareTo(curr.data) > 0) curr.right = insert(temp, curr.right); return curr; } private Node remove(Comparable temp, Node curr) { if(curr == null) throw new TreeException(temp.toString( )); if(temp.compareTo(curr.data) < 0) curr.left = remove(temp, curr.left ); else if(temp.compareTo(curr.data) > 0 ) curr.right = remove(temp, curr.right ); else if(curr.left != null && curr.right != null ) { curr.data = findMin(curr.right).data; curr.right = removeMin(curr.right ); } else curr = ( curr.left != null ) ? curr.left : curr.right; return curr; } private Node removeMin(Node curr) { if(curr == null) { throw new TreeException(); } else if(curr.left != null) { curr.left = removeMin(curr.left); return curr; } else { return curr.right; } } private Node findMin(Node curr) { if(curr != null) { while(curr.left != null) { curr = curr.left; } } return curr; } private Node search(Comparable temp, Node curr) { while(curr != null) { if(temp.compareTo(curr.data) < 0) curr = curr.left; else if(temp.compareTo(curr.data) > 0) curr = curr.right; else return curr; // Match } return null; // Not found } }
LTree$Node;
protected abstract Node<E> insert(Node<E> curr, Node<E> node);
private Node insert(Comparable temp, Node curr) {
public class BinarySearchTree<E extends Comparable<? super E>> extends Tree<E>
{
protected Node insert(Comparable temp, Node curr) {
//Node<E> temp= new Node<E>(node.data) ;
if(curr == null)
curr = new Node(temp);
else if(temp.compareTo(curr.data) < 0)
curr.left = insert(temp,curr.left);
else if(temp.compareTo(curr.data) > 0)
curr.right = insert(temp,curr.right);
return curr;
}
protected Node remove(Comparable temp, Node curr) {
if (curr == null)
throw new TreeException();
if (temp.compareTo(curr.data) < 0)
curr.left = remove(temp, curr.left);
else if(temp.compareTo(curr.data) > 0)
curr.right = remove(temp, curr.right);
return curr;
}
}
public class BinarySearchTree<E extends Comparable<? super E>> extends Tree<E> {
protected Node insert(Comparable temp, Node curr) {
//Node<E> temp= new Node<E>(node.data) ;
if(curr == null)
curr = new Node(temp);
else if(temp.compareTo(curr.data) < 0)
curr.left = insert(temp,curr.left);
else if(temp.compareTo(curr.data) > 0)
curr.right = insert(temp,curr.right);
return curr;
}
protected Node remove(Comparable temp, Node curr) {
if (curr == null)
throw new TreeException();
if (temp.compareTo(curr.data) < 0)
curr.left = remove(temp, curr.left);
else if(temp.compareTo(curr.data) > 0)
curr.right = remove(temp, curr.right);
return curr;
}
protected boolean search (Comparable temp, E key)
{
while (curr != null){
if(temp.data.compareTo(key) < 0)
{
temp = temp.left;
return true;
}
else if (temp.data.compareTo(key) > 0);
{
temp = temp.right;
return true;
}
else
return false;
}
return false;
}
}
C:\Program Files\Java\jdk1.6.0_10\bin>javac *.java
BinarySearchTree.java:42: 'else' without 'if'
else
^
1 error
C:\Program Files\Java\jdk1.6.0_10\bin>
Well Deven the error speaks for itself, let us look at your declaration of the method insert(Node,Node) (also I do not know why would you want two Nodes to be passed to the insert() method) in the Tree class: -
java Syntax (Toggle Plain Text)
protected abstract Node<E> insert(Node<E> curr, Node<E> node);
Now you have declared this as abstract, no hassles here, since you want to force the extending class to provide an implementation, But the only insert() method I see in the child class "BinarySearchTree" is this :-
java Syntax (Toggle Plain Text)
private Node insert(Comparable temp, Node curr) {
So you haven't actually provided an implementation for the insert(Node,Node) method of the Tree class and hence the compiler is complaining about it. Also I think you should get more errors when you are actually try to create an instance of the BinarySearchTree class.
One fix I see is changing the declaration of insert(Node,Node) in the parent Tree class to insert(Comparable,Node).
Also you are reducing the visibility of the method from "protected" to "private" in the child class, which **I think** should also cause problems.
Note I haven't paid too much attention to your design or the rest of the code, so you should be a better judge as to what solution you should opt for.
Same error message, and even correcting semicolons issue;]
Thanks
public class BinarySearchTree<E extends Comparable<? super E>> extends Tree<E>
{
protected Node insert(Comparable temp, Node curr) {
//Node<E> temp= new Node<E>(node.data) ;
if(curr == null)
curr = new Node(temp);
else if(temp.compareTo(curr.data) < 0)
curr.left = insert(temp,curr.left);
else if(temp.compareTo(curr.data) > 0)
curr.right = insert(temp,curr.right);
return curr;
}
protected Node remove(Comparable temp, Node curr) {
if (curr == null)
throw new TreeException();
if (temp.compareTo(curr.data) < 0)
curr.left = remove(temp, curr.left);
else if(temp.compareTo(curr.data) > 0)
curr.right = remove(temp, curr.right);
return curr;
}
protected boolean search (Node<E> curr, E key)
{
while (curr != null){
if(curr.data.compareTo(key) < 0)
{
curr = curr.left;
return true;
}
else if (curr.data.compareTo(key) > 0)
{
curr = curr.right;
return true;
}
else
{
return false;
}
return false;
}
}
}
C:\Documents and Settings\Deven\Desktop\Project 7>javac *.java
BinarySearchTree.java:1: BinarySearchTree is not abstract and does not override
abstract method remove(Tree<E>.Node<E>,E) in Tree
public class BinarySearchTree<E extends Comparable<? super E>> extends Tree<E> {
^
Note: BinarySearchTree.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
1 error
protected abstract Node<E> remove(Node<E> curr, E data) throws TreeException;
protected abstract boolean search(Node<E> curr, E key);
protected abstract Node<E> insert(Node<E> curr, Node<E> node);
@Override
protected Node<E> insert(Node<E> curr, Node<E> node) {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
protected Node<E> remove(Node<E> curr, E data) throws TreeException {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
protected boolean search(Node<E> curr, E key) {
throw new UnsupportedOperationException("Not supported yet.");
}public class BinarySearchTree<E extends Comparable<? super E>> extends Tree<E>
{
protected Node<E> insert(Node<E> curr, Node<E> node) {
throw new UnsupportedOperationException;
Node<E> temp= new Node<E>(node.data) ;
if( temp == null )
temp = new Node<E>(curr.data);
else if( (temp.data.compareTo(node.data))<0 )
temp.left = insert( curr.right, node.left );
else if( (temp.data.compareTo(node.data))>0 )
temp.right = insert( node.right, curr.left );
return temp;
}
protected Node<E> remove(Node<E> curr, E data)
{
throw new UnsupportedOperationException;
Node<E> temp= new Node<E>(curr.data) ;
if( temp == null )
throw new TreeException();
if( temp.data.compareTo( curr.data ) < 0 )
temp.left = remove( temp.left,data );
else if( temp.data.compareTo( curr.data ) >0)
temp.right = remove( temp.right,data );
return temp;
}
protected boolean search (Node<E> curr, E key)
{
throw new UnsupportedOperationException;
while (curr != null){
if(curr.data.compareTo(key) < 0)
{
curr = curr.left;
return true;
}
else if (curr.data.compareTo(key) > 0)
{
curr = curr.right;
return true;
}
else
{
return false;
}
return false;
}
}
}
C:\Documents and Settings\Deven\Desktop\Project 7>javac *.java
BinarySearchTree.java:3: '(' or '[' expected
throw new UnsupportedOperationException;
^
BinarySearchTree.java:17: '(' or '[' expected
throw new UnsupportedOperationException;
^
BinarySearchTree.java:32: '(' or '[' expected
throw new UnsupportedOperationException;
^
3 errors
C:\Documents and Settings\Deven\Desktop\Project 7>
Implement the insert, remove, and search methods of the BinarySearchTree class
This mean: override abstract methods of abstract class Tree<E>
In public class BinarySearchTree you need write:protected abstract Node<E> remove(Node<E> curr, E data) throws TreeException; protected abstract boolean search(Node<E> curr, E key); protected abstract Node<E> insert(Node<E> curr, Node<E> node);
@Override protected Node<E> insert(Node<E> curr, Node<E> node) { throw new UnsupportedOperationException("Not supported yet."); } @Override protected Node<E> remove(Node<E> curr, E data) throws TreeException { throw new UnsupportedOperationException("Not supported yet."); } @Override protected boolean search(Node<E> curr, E key) { throw new UnsupportedOperationException("Not supported yet."); }
throw new UnsupportedOperationException("Not supported yet.");
- replace this lines with own code
Use NetBeans to develop your program!
| DaniWeb Message | |
| Cancel Changes | |