Binary Search Tree

Reply

Join Date: Nov 2008
Posts: 11
Reputation: deven1974 is an unknown quantity at this point 
Solved Threads: 0
deven1974 deven1974 is offline Offline
Newbie Poster

Binary Search Tree

 
0
  #1
Dec 14th, 2008
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.


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

  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)
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 1,175
Reputation: stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light 
Solved Threads: 125
Featured Poster
stephen84s's Avatar
stephen84s stephen84s is offline Offline
Veteran Poster

Re: Binary Search Tree

 
0
  #2
Dec 15th, 2008
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: -

  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 :-
  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.
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."

"How to ask questions the smart way ?"
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 11
Reputation: deven1974 is an unknown quantity at this point 
Solved Threads: 0
deven1974 deven1974 is offline Offline
Newbie Poster

Re: Binary Search Tree

 
0
  #3
Dec 15th, 2008
okay I have updated the Binary Search Tree, but I'm still getting one message.

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

}
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 1,175
Reputation: stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light 
Solved Threads: 125
Featured Poster
stephen84s's Avatar
stephen84s stephen84s is offline Offline
Veteran Poster

Re: Binary Search Tree

 
0
  #4
Dec 15th, 2008
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 ???
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."

"How to ask questions the smart way ?"
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 11
Reputation: deven1974 is an unknown quantity at this point 
Solved Threads: 0
deven1974 deven1974 is offline Offline
Newbie Poster

Re: Binary Search Tree

 
0
  #5
Dec 16th, 2008
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:
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>
Originally Posted by stephen84s View Post
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: -

  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 :-
  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.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,454
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 511
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Binary Search Tree

 
0
  #6
Dec 16th, 2008
Check your semicolons prior to that else statement.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 11
Reputation: deven1974 is an unknown quantity at this point 
Solved Threads: 0
deven1974 deven1974 is offline Offline
Newbie Poster

Re: Binary Search Tree

 
0
  #7
Dec 16th, 2008
Same error message, and even correcting semicolons issue;

Thanks
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 11
Reputation: deven1974 is an unknown quantity at this point 
Solved Threads: 0
deven1974 deven1974 is offline Offline
Newbie Poster

Re: Binary Search Tree

 
0
  #8
Dec 16th, 2008
Originally Posted by deven1974 View Post
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;

}
}

}

Error message that I'm getting, can anybody help with this error message:
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
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 332
Reputation: quuba is on a distinguished road 
Solved Threads: 53
quuba quuba is offline Offline
Posting Whiz

Re: Binary Search Tree

 
0
  #9
Dec 16th, 2008
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!
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 11
Reputation: deven1974 is an unknown quantity at this point 
Solved Threads: 0
deven1974 deven1974 is offline Offline
Newbie Poster

Re: Binary Search Tree

 
0
  #10
Dec 16th, 2008
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:
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>
Originally Posted by quuba View Post
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!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC