Binary Search Tree Help Please!

Soldato
Joined
27 Aug 2004
Posts
17,111
Location
Geordieland
Please help, im stuck.

Basically, I have to use a binary serch tree to take values and insert them into a binary search tree in alphabetical order, then print out the values, but im kinda stumped.

This is what I need to do
You would insert at the root if the tree is empty, otherwise you would insert in the left subtree if the element is smaller than the root element and in the right subtree if the element is larger than the root element.

Once complete run/execute the programme again to produce the same results.

All of the code im stuck on goes into the insertR method at the bottom of the code.

BST Class
Code:
public class BST 
{
	private BSTNode root;
	public BST () 
           {
	            // Construct an empty BST.
		root = null;
	}
	
	public class BSTNode 
            {
	           // BSTNode constructor, sets up Node containing elem and two null links
		protected Comparable element;
		protected BSTNode left, right;
		protected BSTNode (Comparable elem) 
                        {
			element = elem;
			left = null;  right = null;
		}
	} 
	
	public BSTNode getRoot() 
           {
	            // return the root of the tree
		return root;
	}
	
	public void insert (Comparable elem) 
           {
	    // insert an elem into the tree
		int direction = 0;
		BSTNode parent = null, curr = root;
		for (;;) 
                       {
			if (curr == null) 
                                   {
				BSTNode ins = new BSTNode(elem);
				if (root == null)  root = ins;
				else if (direction < 0)
					parent.left = ins;
				else  parent.right = ins;
				return;
			} 
			direction = elem.compareTo(curr.element);
			if (direction == 0)  return;
			parent = curr;
			if (direction < 0)  curr = curr.left;
			else  curr = curr.right;
		}
	} 

	public void printInOrder (BSTNode root) 
           {
		// Print, in ascending order, all the elements in the BST subtree 
		// whose topmost node is root.
		if (root != null) 
                       {
			printInOrder(root.left);
			System.out.println(root.element);
			printInOrder(root.right);
		}
	} 
	
	[B]public void insertR (BSTNode root, Comparable elem)
    {    	
 		// insert an elem into the tree
		int direction = 0;
		BSTNode parent = null, curr = root;
		
		for (;;) 
        {
        	if (curr == null) 
            {
				BSTNode ins = new BSTNode(elem);
				if (root == null)  root = ins;
				
				direction = elem.compareTo(curr.element);

				if (direction < 0)
				{
					parent.left = ins;
				}
				if   (direction > 0)
				{
				parent.right = ins;
				}
				return;
			}
		}	
	}[/B]
}

BSTDriver
Code:
class BSTDriver
{
	public static void main(String[] args)
	{
		BST data = new BST();
		
		data.insertR("Lion");
		data.insertR("Fox");
		data.insertR("Rat");
		
		data.printInOrder(data.getRoot());
	}
}

Can anyone at least give me a hint as to the direction i need to be going in. I get an error upon compiling pointing at my driver where my insert commands are along the lines of insertR(BST.BSTNode,java.lang.Comparable) in BST cannot be applied to (java.lang.String)

Cheers for any help at all, as ive hit a bit of a brick wall.
 
Last edited:
Back
Top Bottom