Results 1 to 6 of 6
- 05-10-2011, 06:00 PM #1
Member
- Join Date
- May 2011
- Location
- Connecticut, USA
- Posts
- 4
- Rep Power
- 0
Any suggestions for My search method?
This is my Binary search tree search method
Anything would be helpful... I am not sure if it even works, I haven't implemented the graphical part of the BST.Java Code:public Node SearchBST(int number) { Node T = Root; if (T == null)//If tree is empty { return null; } else while(T != null){ if(number == T.data){ return T; //Key found } else if(number < T.data){//Checks if the key is smaller than the current T T = T.left; } else//Checks if the key is larger than the current T T = T.right; } if(T == null) return null; //Key not found }
- 05-10-2011, 06:48 PM #2
Looks good to me, aside from a few minor things: The last check if T==null, is it even needed? It seems to me that the only way it will ever reach that far down is if T is null, so it seems to be a redundant check. Also, you can probably get rid of the "else" after the first check if T == null; after all, if it is null, you return null and the method exits. As such, the else will always fire.
EDIT: Oh, and one thing that just came to me; it might complain that it might lack a return, which will be solved by removing the last check if T==null.Last edited by Toll; 05-10-2011 at 06:50 PM.
- 05-10-2011, 07:35 PM #3
Member
- Join Date
- May 2011
- Location
- Connecticut, USA
- Posts
- 4
- Rep Power
- 0
Thank you much, also if anyone can, this is My add method
If anyone sees anything strange, please tell me.Java Code:public boolean AddToBST(int number) { Node t = new Node(); t.left = null; t.right = null; t.data = number; //System.out.println(t.data); Node Temp = Root;//This pointer points to the root if (Temp == null) { Root = t; return true; } else { while (Temp!=null){ if(t.data == Temp.data){ System.out.println("Same Value added"); return true; } else if(t.data < Temp.data){ if(Temp.left == null){ Temp.left = t; } else Temp = Temp.left; } else//This is the if(t.data>Temp.data) if(Temp.right == null){ Temp.right = t; } else Temp = Temp.right; System.out.println(Temp.data); //System.out.println("In while loop"); } } return true; }
- 05-10-2011, 08:12 PM #4
I think you're missing a few returns there. Also, since you're returning a boolean... Is there any case where it should return false? Because it never does.
- 05-10-2011, 08:13 PM #5
Or perhaps you're not missing a few returns, on a closer look. The other question still stands though.
- 05-10-2011, 09:08 PM #6
Member
- Join Date
- May 2011
- Location
- Connecticut, USA
- Posts
- 4
- Rep Power
- 0
Similar Threads
-
I could use some suggestions for creating a new method
By drewtrcy in forum New To JavaReplies: 7Last Post: 05-05-2011, 05:53 AM -
Binary Search Method
By 8xjesterx8 in forum New To JavaReplies: 0Last Post: 04-09-2011, 01:33 AM -
need help to search 'method(s)' from java files
By doha786 in forum New To JavaReplies: 6Last Post: 03-25-2010, 09:18 AM -
Binary search tree search method
By chopo1980 in forum New To JavaReplies: 2Last Post: 12-10-2009, 01:42 AM -
possible to search files in project for use of a certain method?
By emceenugget in forum EclipseReplies: 4Last Post: 01-14-2009, 04:12 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks