943,949 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 3024
  • Java RSS
You are currently viewing page 1 of this multi-page discussion thread
Dec 14th, 2008
0

Binary Search Tree

Expand Post »
Implement the insert, remove, and search methods of the BinarySearchTree
class. Included are the Main, Tree, and TreeException classes.
Do not make changes to those classes. Your tree should work with
them as shown. All operations should be implemented recursively.
Additional methods may be needed.


Java Syntax (Toggle Plain Text)
  1.  
  2. /*
  3. * Tree.java
  4.  *
  5.  */
  6.  
  7. import java.util.Iterator;
  8. import java.util.Vector;
  9.  
  10. public abstract class Tree<E> implements Iterable<E> {
  11.  
  12. protected class Node<T> {
  13.  
  14. protected Node(T data) {
  15. this.data = data;
  16. }
  17.  
  18. protected T data;
  19. protected Node<T> left;
  20. protected Node<T> right;
  21. }
  22.  
  23. public void insert(E data) {
  24.  
  25. Node<E> temp = new Node<E>(data);
  26.  
  27. root = insert(root, temp);
  28. }
  29.  
  30. protected abstract Node<E> insert(Node<E> curr, Node<E> node);
  31.  
  32. public Iterator<E> iterator() {
  33.  
  34. Vector<E> vector = new Vector<E>();
  35.  
  36. traverse(vector, root);
  37.  
  38. return vector.iterator();
  39. }
  40.  
  41. public void remove(E data) {
  42.  
  43. root = remove(root, data);
  44. }
  45.  
  46. protected abstract Node<E> remove(Node<E> curr, E data) throws TreeException;
  47.  
  48. public boolean search(E key) {
  49.  
  50. return search(root, key);
  51. }
  52.  
  53. protected abstract boolean search(Node<E> curr, E key);
  54.  
  55. private void traverse(Vector<E> vector, Node<E> curr) {
  56.  
  57. if (curr != null) {
  58. traverse(vector, curr.left);
  59. vector.add(curr.data);
  60. traverse(vector, curr.right);
  61. }
  62. }
  63.  
  64. private Node<E> root;
  65. }
  66.  
  67. /*
  68.  *
  69.  * TreeException.java
  70.  *
  71.  */
  72.  
  73. public class TreeException extends RuntimeException {
  74.  
  75. public TreeException() {}
  76. public TreeException(String msg) { super(msg); }
  77. }

Java Syntax (Toggle Plain Text)
  1. /*
  2.  *
  3.  * BinarySearchTree.java
  4.  *
  5.  */
  6.  
  7. public class BinarySearchTree<E extends Comparable<? super E>> extends Tree<E>
  8. {
  9. private Node insert(Comparable temp, Node curr) {
  10. if(curr == null)
  11. curr = new Node(temp);
  12. else if(temp.compareTo(curr.data) < 0)
  13. curr.left = insert(temp, curr.left);
  14. else if(temp.compareTo(curr.data) > 0)
  15. curr.right = insert(temp, curr.right);
  16. return curr;
  17. }
  18.  
  19. private Node remove(Comparable temp, Node curr) {
  20. if(curr == null)
  21. throw new TreeException(temp.toString( ));
  22. if(temp.compareTo(curr.data) < 0)
  23. curr.left = remove(temp, curr.left );
  24. else if(temp.compareTo(curr.data) > 0 )
  25. curr.right = remove(temp, curr.right );
  26. else if(curr.left != null && curr.right != null ) {
  27. curr.data = findMin(curr.right).data;
  28. curr.right = removeMin(curr.right );
  29. } else
  30. curr = ( curr.left != null ) ? curr.left : curr.right;
  31. return curr;
  32. }
  33. private Node removeMin(Node curr) {
  34. if(curr == null) {
  35. throw new TreeException();
  36. }
  37. else if(curr.left != null) {
  38. curr.left = removeMin(curr.left);
  39. return curr;
  40. }
  41. else {
  42. return curr.right;
  43. }
  44. }
  45.  
  46. private Node findMin(Node curr) {
  47. if(curr != null) {
  48. while(curr.left != null) {
  49. curr = curr.left;
  50. }
  51. }
  52. return curr;
  53. }
  54. private Node search(Comparable temp, Node curr) {
  55. while(curr != null) {
  56. if(temp.compareTo(curr.data) < 0)
  57. curr = curr.left;
  58. else if(temp.compareTo(curr.data) > 0)
  59. curr = curr.right;
  60. else
  61. return curr; // Match
  62. }
  63. return null; // Not found
  64. }
  65. }



Can anybody help with this program, I'm getting an error message after I'd complie it.

Here is the error message:


Exception in thread "main" java.lang.AbstractMethodError: Tree.insert(LTree$Node;LTree$NodeLTree$Node;
at Tree.insert(Tree.java:21)
at Main.main(Main.java:12)
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
deven1974 is offline Offline
11 posts
since Nov 2008
Dec 15th, 2008
0

Re: Binary Search Tree

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)
  1. 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)
  1. 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.
Last edited by stephen84s; Dec 15th, 2008 at 3:54 am.
Featured Poster
Reputation Points: 653
Solved Threads: 151
Nearly a Posting Virtuoso
stephen84s is offline Offline
1,316 posts
since Jul 2007
Dec 15th, 2008
0

Re: Binary Search Tree

okay I have updated the Binary Search Tree, but I'm still getting one message.

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

}
Reputation Points: 10
Solved Threads: 0
Newbie Poster
deven1974 is offline Offline
11 posts
since Nov 2008
Dec 15th, 2008
0

Re: Binary Search Tree

Please paste the compiler error or whatever message you are getting. I mean how can we help you solve your problem if we do not know what the problem is ???
Featured Poster
Reputation Points: 653
Solved Threads: 151
Nearly a Posting Virtuoso
stephen84s is offline Offline
1,316 posts
since Jul 2007
Dec 16th, 2008
0

Re: Binary Search Tree

Quote ...
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;

}
}

Error Message is:
Quote ...
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>
Click to Expand / Collapse  Quote originally posted by stephen84s ...
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)
  1. 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)
  1. 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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
deven1974 is offline Offline
11 posts
since Nov 2008
Dec 16th, 2008
0

Re: Binary Search Tree

Check your semicolons prior to that else statement.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Dec 16th, 2008
0

Re: Binary Search Tree

Same error message, and even correcting semicolons issue;

Thanks
Reputation Points: 10
Solved Threads: 0
Newbie Poster
deven1974 is offline Offline
11 posts
since Nov 2008
Dec 16th, 2008
0

Re: Binary Search Tree

Click to Expand / Collapse  Quote originally posted by deven1974 ...
Same error message, and even correcting semicolons issue;]

Thanks

Quote ...

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;

}
}

}

Error message that I'm getting, can anybody help with this error message:
Quote ...
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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
deven1974 is offline Offline
11 posts
since Nov 2008
Dec 16th, 2008
0

Re: Binary Search Tree

Implement the insert, remove, and search methods of the BinarySearchTree class
This mean: override abstract methods of abstract class Tree<E>
    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);
In public class BinarySearchTree you need write:
    @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!
Reputation Points: 123
Solved Threads: 106
Posting Pro
quuba is offline Offline
573 posts
since Nov 2008
Dec 16th, 2008
0

Re: Binary Search Tree

Quote ...
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;

}
}

}
Error Message:
Quote ...
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>
Click to Expand / Collapse  Quote originally posted by quuba ...
Implement the insert, remove, and search methods of the BinarySearchTree class
This mean: override abstract methods of abstract class Tree<E>
    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);
In public class BinarySearchTree you need write:
    @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!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
deven1974 is offline Offline
11 posts
since Nov 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Help understanding break and continue
Next Thread in Java Forum Timeline: Java random Unique numbers HELP





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC