943,907 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 3023
  • Java RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Dec 17th, 2008
0

Re: Binary Search Tree

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.
Reputation Points: 123
Solved Threads: 106
Posting Pro
quuba is offline Offline
573 posts
since Nov 2008
Dec 17th, 2008
0

Re: Binary Search Tree

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.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Dec 17th, 2008
0

Re: Binary Search Tree

meantime relate to #10
java Syntax (Toggle Plain Text)
  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;
Reputation Points: 123
Solved Threads: 106
Posting Pro
quuba is offline Offline
573 posts
since Nov 2008
Dec 18th, 2008
0

Re: Binary Search Tree

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.
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) {
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:
Quote ...
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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
deven1974 is offline Offline
11 posts
since Nov 2008
Dec 20th, 2008
0

Re: Binary Search Tree

check your code:
java Syntax (Toggle Plain Text)
  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).
Reputation Points: 123
Solved Threads: 106
Posting Pro
quuba is offline Offline
573 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