Results 1 to 6 of 6
  1. #1
    kevkid is offline Member
    Join Date
    May 2011
    Location
    Connecticut, USA
    Posts
    4
    Rep Power
    0

    Question Any suggestions for My search method?

    This is my Binary search tree search method
    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
    }
    Anything would be helpful... I am not sure if it even works, I haven't implemented the graphical part of the BST.

  2. #2
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    382
    Rep Power
    3

    Default

    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.

  3. #3
    kevkid is offline Member
    Join Date
    May 2011
    Location
    Connecticut, USA
    Posts
    4
    Rep Power
    0

    Default

    Thank you much, also if anyone can, this is My add method
    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;
        	}
    If anyone sees anything strange, please tell me.

  4. #4
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    382
    Rep Power
    3

    Default

    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.

  5. #5
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    382
    Rep Power
    3

    Default

    Or perhaps you're not missing a few returns, on a closer look. The other question still stands though.

  6. #6
    kevkid is offline Member
    Join Date
    May 2011
    Location
    Connecticut, USA
    Posts
    4
    Rep Power
    0

    Default

    I do thank you :D I am almost done with my project.
    again Thanks :D

Similar Threads

  1. Replies: 7
    Last Post: 05-05-2011, 05:53 AM
  2. Binary Search Method
    By 8xjesterx8 in forum New To Java
    Replies: 0
    Last Post: 04-09-2011, 01:33 AM
  3. need help to search 'method(s)' from java files
    By doha786 in forum New To Java
    Replies: 6
    Last Post: 03-25-2010, 09:18 AM
  4. Binary search tree search method
    By chopo1980 in forum New To Java
    Replies: 2
    Last Post: 12-10-2009, 01:42 AM
  5. Replies: 4
    Last Post: 01-14-2009, 04:12 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •