Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

binary search tree

807569Sep 23 2006 — edited Sep 28 2006
Hi!

I'm pretty new to Java, so pls try to understand. I'm having some trouble fixing the insert method in my code. I think I got the find method working fine, not sure though.
Here is my search tree:
/**
 * Proxy class for the binary search tree that can be used to protect the tree and
 * to offer a non-changing root point for the tree.
 *
 * You can use this class to test your BSTNode class. 
 */

public class BinarySearchTree {

	private BSTNode root = null;
    
    public BSTNode getRoot(){
        return root;
    }
	    
	public void add(Comparable c) {
		if (root == null) 
			root = new BSTNode(c);
		else 
			root = root.insert(c);
	}

    /**
     * A tree can be cleared by just dropping the reference to its root.
     */
	public void clear() {
		root = null;
	}


	public boolean contains(Comparable c) {
		return (isEmpty()) ? false : root.contains(c);
	}

	public boolean isEmpty() {
		return root == null;
	}


	public void remove(Comparable c) {
		if (root != null)
			root = root.delete(c);
	}

}
Here is the Node:
import java.util.*;

public class BinaryTreeNode {

	private BinaryTreeNode left;
	private BinaryTreeNode right;
	private Object data;

	public final BinaryTreeNode getRight() {
		return right;
	}

	public final void setRight(BinaryTreeNode tree) {
		right = tree;
	}

	public final BinaryTreeNode getLeft() {
		return left;
	}

	public final void setLeft(BinaryTreeNode tree) {
		left = tree;
	}

	public final Object getData() {
		return data;
	}

	public final void setData(Object data) {
		this.data = data;
	}
	
}
And here is the code im trying to fix (methods "contains", "insert" and "printInorder"
import java.util.*;
import java.lang.String;

class BSTNode extends BinaryTreeNode {

	public BSTNode() {
        setData(null);
	}
	
	public BSTNode(Comparable data) {
        setData(data);
	}

	public BSTNode insert(Comparable data) {
   	}
         //Looks if the data is in this node and its childrens'
	public boolean contains(Comparable data) {
   
		if(data.compareTo(this.getData()) == 0) {
			return true;
		}
		if(data.compareTo(this.getData()) > 0) {
			BinaryTreeNode rightNode = this.getRight();
			Object info = rightNode.getData();
			Comparable info2 = (Comparable)info ;
			return contains(info2);
		}
                  if(data.compareTo(this.getData()) < 0) {
                            BinaryTreeNode leftNode = this.getLeft();
			Object information = leftNode.getData();
			Comparable information2 = (Comparable)information ;
			return contains(information2);
              }
                 return false;
	}


    public void printInorder(java.io.PrintStream pw){
    
    }
	

}
First of all, is my "contains"-method looking fine?
I have some ideas for the insert method, but I just can't figure it out...any tips? I don't want the code to be made for me, just some ideas. The biggest problem is that I can't figure out how to communicate between BSTNode and BinaryTreeNode.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 26 2006
Added on Sep 23 2006
20 comments
652 views