DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 2 of 2
  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

Bookmarks

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


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links