Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Binary Search Tree Lookup Method

  1. #1
    Junior Member
    Join Date
    May 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Binary Search Tree Lookup Method

    Hi guys, I'm having a bit of trouble creating the search method for the BST. I've come up with the following:

        public AnyClass search(String key) {
     
            BNode currentNode = root;
     
            while (true) {
            	int cmp = newNode.compareTo(currentNode);
     
                if (cmp < 0) {
                	BNode nextNode = currentNode.getLeft();
                	if (nextNode == null) {
                		currentNode.getLeft();
                		return null;
                	}
                	currentNode = nextNode;
                }
                else if (cmp > 0) {
                	BNode nextNode = currentNode.getRight();
                	if (nextNode == null) {
                		currentNode.getRight();
                		return null;
                	}
                	currentNode = nextNode;    		
                }
                else if (cmp == 0) {
                	return cmp;
                }	
            }
        }

    BNode

    package binarynodes;
     
    import dataobjects.AnyClass;
     
    public class BNode {
    	public BNode left, right;
    	public AnyClass obj;
     
    	public BNode(AnyClass newObj){
    		left = null;
    		right = null;
    		obj = newObj;
    	}
     
    	public void setLeft(BNode left){
    		this.left = left;
    	}
     
    	public void setRight(BNode right){
    		this.right = right; 
    	}
     
    	public BNode getLeft(){return left;}
    	public BNode getRight(){return right;}
     
    	public void setKey(AnyClass newObj){this.obj = newObj;}
    	public AnyClass getKey(){return obj;}
     
    	public void print(){
     
    		System.out.println(obj.getData());	
    	}
     
    	public int compareTo(BNode other) {
    	    return obj.compareTo(other.obj);
    	}
     
    }

    I need to create a getKey method to BNode so that I can get the key of the currentNode so than I can compare string against string. How do I go about doing this?

    Thanks


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,372
    Thanks
    183
    Thanked 836 Times in 779 Posts
    Blog Entries
    5

    Default Re: Binary Search Tree Lookup Method

    I need to create a getKey method to BNode so that I can get the key of the currentNode so than I can compare string against string. How do I go about doing this?
    I'm not sure what you are asking...the BNode class you posted already has a getKey method.

Similar Threads

  1. Problem with Binary Search Tree
    By hawkflame in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 25th, 2012, 10:04 PM
  2. Building a binary search tree
    By Herah in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 28th, 2011, 07:29 AM
  3. Binary Search Tree
    By lex25288 in forum Algorithms & Recursion
    Replies: 3
    Last Post: January 19th, 2011, 09:10 AM
  4. Data Structures(Binary Search Tree to AVL Tree)ASAP
    By jfAdik in forum Algorithms & Recursion
    Replies: 2
    Last Post: April 5th, 2010, 04:58 AM
  5. Binary Search Tree
    By Koren3 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: November 12th, 2009, 09:27 AM