| | |
Binary Search Tree
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Nov 2008
Posts: 332
Reputation:
Solved Threads: 53
once more - Use NetBeans to develop your program!
text
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.
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.
•
•
Join Date: Nov 2008
Posts: 332
Reputation:
Solved Threads: 53
meantime relate to #10
Inside your methods, if you filled them , don't still use
java Syntax (Toggle Plain Text)
@Override 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;// 1.Not proper position for return } return false;//2.Correct position for return }
throw new UnsupportedOperationException; •
•
Join Date: Nov 2008
Posts: 11
Reputation:
Solved Threads: 0
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.
Output Result:
Thanks for your help on this.
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
•
•
Join Date: Nov 2008
Posts: 332
Reputation:
Solved Threads: 53
check your code:
Result--> always one element inside the tree (last inserted).
java Syntax (Toggle Plain Text)
protected Node<E> insert(Node<E> curr, Node<E> node) { //throw new UnsupportedOperationException; System.out.println(" curr " + curr + " " + ((curr == null) ? "." : "data=" + curr.data)); System.out.println(" node " + node + " " + ((node == null) ? "." : "data=" + node.data)); Node<E> temp = new Node<E>(node.data); System.out.println(" temp " + temp + " " + ((temp == null) ? "." : "data=" + temp.data)); if (temp == null) { temp = new Node<E>(curr.data); System.out.println(" temp == null "); } else if ((temp.data.compareTo(node.data)) < 0) { System.out.print("L "); temp.left = insert(curr.right, node.left); } else if ((temp.data.compareTo(node.data)) > 0) { System.out.print("R "); temp.right = insert(node.right, curr.left); } return temp; }
![]() |
Similar Threads
- deleting a Binary search tree (C++)
- Reading a file into a binary search tree (C++)
- Binary Search Tree (C++)
- searching and inserting node in a binary search tree (C)
- recursive findaverage function for Binary search tree (C)
- Binary search tree removal (C++)
- Insertion in a binary search tree (C++)
Other Threads in the Java Forum
- Previous Thread: Help understanding break and continue
- Next Thread: Java random Unique numbers HELP
| Thread Tools | Search this Thread |
911 addball addressbook android api append applet application array arrays automation binary bluetooth button character chat class classes client code component consumer css csv database eclipse ee error event exception fractal ftp game givemetehcodez graphics gui html ide image input integer j2me japplet java javaarraylist javaprojects jmf jni jpanel julia jvm key linked linux list loan loop map method methods mobile netbeans newbie objects oriented output panel phone print printf problem program programming project projects recursion replaydirector reporting researchinmotion robot rotatetext scanner screen se server service set size sms software sort sql string swing test threads transfer tree ubuntu windows






