Binary Search Tree

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

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
  #11
Dec 17th, 2008
once more - Use NetBeans to develop your program!
text throw new UnsupportedOperationException("Not supported yet."); - replace this lines with own code.
There should be invocations of your private methods with same name (sometime you need make some adaptation).
Use NetBeans , proper tool for this advanced java program. NetBeans helps you in develop.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,479
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: 514
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Binary Search Tree

 
0
  #12
Dec 17th, 2008
Deven, you really need to use [code] [/code] tags around any code that you post. Putting it in as a quote does nothing to preserve it's formatting.
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
  #13
Dec 17th, 2008
meantime relate to #10
  1. @Override
  2. protected boolean search(Node<E> curr, E key) {
  3. while (curr != null) {
  4. if (curr.data.compareTo(key) < 0) {
  5. curr = curr.left;
  6. return true;
  7. } else if (curr.data.compareTo(key) > 0) {
  8. curr = curr.right;
  9. return true;
  10. } else {
  11. return false;
  12. }
  13. //return false;// 1.Not proper position for return
  14. }
  15. return false;//2.Correct position for return
  16. }
Inside your methods, if you filled them , don't still use
throw new UnsupportedOperationException;
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
  #14
Dec 18th, 2008
Hello,
I want thank for your guys help with this diffcult program. I finally got the program to work, but I don't know the output is right. Can anybody tell me is this what output should be or something else. Is this output answer the question that is given from the program.
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) {
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;
}
}

Output Result:
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\Documents and Settings\Deven>cd Desktop

C:\Documents and Settings\Deven\Desktop>cd Project 7

C:\Documents and Settings\Deven\Desktop\Project 7>javac *.java

C:\Documents and Settings\Deven\Desktop\Project 7>java Main
0
8
9
7
5
3
1
1
9
4
0 => true
8 => true
9 => true
7 => true
15 => true
13 => true
11 => true
1 => true
19 => true
14 => true
4
4
4
4
4
4
4
4
4
4
Thanks for your help on this.
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
  #15
Dec 20th, 2008
check your code:
  1.  
  2. protected Node<E> insert(Node<E> curr, Node<E> node) {
  3. //throw new UnsupportedOperationException;
  4. System.out.println(" curr " + curr + " " + ((curr == null) ? "." : "data=" + curr.data));
  5. System.out.println(" node " + node + " " + ((node == null) ? "." : "data=" + node.data));
  6. Node<E> temp = new Node<E>(node.data);
  7. System.out.println(" temp " + temp + " " + ((temp == null) ? "." : "data=" + temp.data));
  8. if (temp == null) {
  9. temp = new Node<E>(curr.data);
  10. System.out.println(" temp == null ");
  11. } else if ((temp.data.compareTo(node.data)) < 0) {
  12. System.out.print("L ");
  13. temp.left = insert(curr.right, node.left);
  14. } else if ((temp.data.compareTo(node.data)) > 0) {
  15. System.out.print("R ");
  16. temp.right = insert(node.right, curr.left);
  17. }
  18.  
  19. return temp;
  20. }
Result--> always one element inside the tree (last inserted).
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