Binary Search Tree Search algorithm


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Binary Search Tree Search algorithm

  1. #1
    Join Date
    Mar 2006
    Posts
    4

    Binary Search Tree Search algorithm

    Could someone please tell me if this is the correct way to implement the search algorithm of the Binary Search Tree? (Using a standard Binary Search Tree)

    abstract class BST {

    public int data;
    public BST left;
    public BST right;
    static public BST find(BST root, int value);

    }


    public class findBST extends BST {

    static public BST find(BST root, int value)
    {
    if (root == null)
    return null;

    if (value < root)
    return find(left, value);
    else if (value > root)
    return find(right, value);
    else
    return root;

    }

    }

  2. #2
    Join Date
    Apr 2005
    Posts
    9
    Kind of though you seem to be wanting to have some hold overs from c++ Here is how I would do it:

    Code:
    class BST{
    	public int data;
    	public BST left;
    	public BST right;
    
    	public static BST find(BST root, int value)
    	{
    		if (root == null)
    			return null;
    		if (value < root.data)
    			return find(root.left, value);
    		else if (value > root.data)
    			return find(root.right, value);
    		else
    			return root;
    	}
    }

Similar Threads

  1. Replies: 15
    Last Post: 03-21-2006, 06:53 AM
  2. Replies: 1
    Last Post: 12-17-2005, 02:50 PM
  3. Replies: 4
    Last Post: 12-05-2005, 06:58 PM
  4. Binary Tree Help
    By TheJVM_1970 in forum Java
    Replies: 2
    Last Post: 12-05-2005, 12:23 AM
  5. Binary Search Tree implementation
    By Liza in forum Java
    Replies: 0
    Last Post: 10-23-2001, 11:21 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center