ADD method for my Binary search tree

operation error, please help

Page 1 of 1

3 Replies - 7952 Views - Last Post: 21 October 2009 - 03:03 PM Rate Topic: -----

#1 jajuje2000  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 14-February 08

ADD method for my Binary search tree

Posted 20 October 2009 - 01:29 PM

Hi guys i need a quick help with my add method for my binary search tree. here is my add method i need help on.
i keep getting this error for it. please help.

BST.java:23: operator <= cannot be applied to int,java.lang.Object

public void add(int element)
	  {
			boolean done = false;
			BSTNode cursor = root;
			BSTNode newE = new BSTNode(element, null, null); 
			if (cursor == null) 
			{
				// the BSTNode is empty
	   		 root = newE;
			}
			 else 
			 {
				while (!done) 
				{
		   		if (element <= cursor.getData()) //error right here
					{
			   		if (cursor.getLeft() == null) 
							{
						   cursor.setLeft(newE);
						   done = true;
			   		} 
							else 
							{
				   		cursor = cursor.getLeft();
					   	}
		   		 } 
					 else 
					 {
					 		// go to the right branch
			   		if (cursor.getRight() == null) 
							{
				   		cursor.setRight(newE);
				   		done = true;
			   		} 
							else 
							{
						   cursor = cursor.getRight();
			   		}
		   		 }   
			   }			
		  }  
	  }



and here is my BSTNode code
import java.io.*;
import java.util.Scanner;

public class BSTNode<E>
{
	private E data;
   private BSTNode<E> left, right;
	
	public BSTNode(E initialData, BSTNode<E> initialLeft, BSTNode<E> initialRight)
	{
   	data = initialData;
		left = initialLeft;
		right = initialRight;
	}

	// Getting and Setting Data Links
	public E getData()
	{
		return data;
	}
	public BSTNode<E> getLeft()
	{
		return left;
	}
	public BSTNode<E> getRight()
	{
		return right;
	}
	public void setData(E newData)
	{
		data = newData;
	} 
	public void setLeft(BSTNode<E> newLeft)
	{
		left = newLeft;
	}
	public void setRight(BSTNode<E> newRight)
	{
		right = newRight;
	}
	public BSTNode<E> removeLeftmost()
	{
   	 // the node that activates removeLeftmost has no left child, thus is itself the leftmost node of the subtree
   	 if (left == null) 
		 {
			return right;
		 } 
		 else 
		 {
   	 	// a recursive call removes the leftmost node from my left subtree
	 	 	left = left.removeLeftmost();
		  return this;
		 }
	}
	
	public BSTNode<E> removeRightmost()
	{
   	 // the node that activates removeLeftmost has no left child, thus is itself the leftmost node of the subtree
   	 if (right == null) 
		 {
			return left;
		 } 
		 else 
		 {
   	 	// a recursive call removes the leftmost node from my left subtree
	 	 	right = right.removeLeftmost();
		  return this;
		 }
	}
	
	public E getLeftmostData()
	{
		if(left == null)
		 return null;
		 else
		 return left.getLeftmostData(); 
	} 
	public E getRightmostData()
	{
		if(right == null)
		 return null;
		 else
		 return right.getRightmostData();
	}

	public void preOrderPrint()
		{
			System.out.println(data);
			if (left != null)
				left.preOrderPrint();
			if (right != null)
				right.preOrderPrint();
		}
		
		public void postOrderPrint()
		{
			if(left != null)
				left.postOrderPrint();
			if(right != null)
				right.postOrderPrint();
			System.out.println(data);
		}
		
		public void inOrderPrint()
		{
			if(left != null)
				left.inOrderPrint();
			System.out.println(data);
			if(right != null)
				right.inOrderPrint();
		}
			
}



thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: ADD method for my Binary search tree

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: ADD method for my Binary search tree

Posted 20 October 2009 - 05:43 PM

element <= cursor.getData())

element is a int
getData() returns an object of type E

you cannot compare them using <=

Actually it is not logic that your addmethod receives an int as paramater it should receice a BSTNode object as parameter

The BSTNode class should implement Comparable and have a compareTo() method to compare to BSTNode objects and return -1, 0, 1 to determine if the first one should go before or after the one received as parameter
Was This Post Helpful? 0
  • +
  • -

#3 jajuje2000  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 14-February 08

Re: ADD method for my Binary search tree

Posted 21 October 2009 - 12:25 PM

thanks pbl i tried creating a method with comparTo() but after that i couldnt make the remove method. so i started from scratch and im a little stuck on the remove method for the binary tree part. here is my code so far with the new node class and my new BST class.

import java.util.Scanner;
import java.io.*;
public class BST 
{
	

  public static void main(String[] args) 
  {
	
	 	int element, num2;
		String input = "";	
		// create scanner
		Scanner kb = new Scanner(System.in);
   
	   // build the simple tree from chapter 11.
		  System.out.println("Please enter the initial sequence of values:");
		input = kb.nextLine();
		String[] num = input.split(" ");
  
	 	Node root = new Node(Integer.parseInt(num[0]));
	 
	 	for(int i = 1; i < num.length; i++)
			insert(root, Integer.parseInt(num[i]));
			  
		System.out.print("Pre-order: ");
		preOrderPrint(root);
		System.out.println();
		System.out.print("In-order: ");
		inOrderPrint(root);
		System.out.println();
		System.out.print("Post-order: ");
		postOrderPrint(root);
		System.out.println();
		
		while (input.equalsIgnoreCase("exit") != true)
				{
					System.out.print("Command? ");
					input = kb.next();
					if(input.equalsIgnoreCase("i"))
					{
						num2 = kb.nextInt();
						insert(root, num2);
						System.out.print("In-order: ");
						inOrderPrint(root);
						System.out.println();
					}
					else if(input.equalsIgnoreCase("d"))
					{
						num2 = kb.nextInt();
						System.out.println();
					}
					else if(input.equalsIgnoreCase("h"))
					{
							// this is my help menu
							System.out.println("I \t Insert a value");
	   					System.out.println("D \t Delete a value");
							System.out.println("E \t Exit the program");
							System.out.println("h \t Display this message");
					}
					else if(input.equalsIgnoreCase("e"))
					{
							System.exit(0);
					}
					else
					{
						System.err.print("\nSorry, but " + input + " is not a valid command. Try again.\n\n");
					}
				}
  }

  static class Node 
  {
	Node left;

	Node right;
	 
	 Node parent;

	int data;

	public Node(int data) 
	 {
	  this.data = data;
	}
  }

 
  public static void insert(Node node, int data) 
  {
	if (data < node.data) 
	 {
	  if (node.left != null) 
		insert(node.left, data); 
		else 
		{
			Node newN = new Node(data);
		  node.left = newN;
			newN.parent = node;
			}
			
	}
	 else if (data > node.data) 
	 {
	  if (node.right != null) 
		insert(node.right, data); 
		else 
		{
			Node newN = new Node(data);
			node.right = newN;
			newN.parent = node;	   
	  }
	}
  }
  
 /* public Node parentOfCursor(Node cursor)
  {
		  Node parent = root;
		  
		  
		  if(parent.data < cursor)
		  {		
				return parent.;
			else if(
  }
  
  */
  public int getRightmostData(Node cursor)
  {
		  if(cursor.right == null)
			return cursor.data;
		return getRightmostData(cursor.right);
  }
  
  /*
   public int removeRightmost(Node cursor)
  {
		  if(cursor.right == null)
			return;
		else
			
		
  }
  */
 
 
  public boolean remove(Node root, int data)
  {
		  Node cursor = root;		
		if(root == null)
			return false;
		
		if(root.left == null)
		{	
			root = root.right;
			return true; 	
		}
		while(cursor != null)
		{
			if(data == cursor.data)
			{
				if(cursor.left == null)
					
			}
			else if(data < cursor.data)
			{
				cursor = cursor.left;
			}
			else if(data > cursor.data)
			{
				cursor = cursor.right;
			}
				 
		}
		return false;
			
  }


  
  public static void inOrderPrint(Node node)
  {
	if (node != null) 
	 {
	  inOrderPrint(node.left);
	  System.out.print(node.data + " ");
	  inOrderPrint(node.right);
	}
  }
  
  public static void preOrderPrint(Node node)
  {	
		  if(node != null)
		{		
			System.out.print(node.data + " ");
			preOrderPrint(node.left);
			preOrderPrint(node.right);
		}
		
  }
  
  public static void postOrderPrint(Node node)
  {
		  if(node != null)
		{
			postOrderPrint(node.left);
			postOrderPrint(node.right);
			System.out.print(node.data + " ");
		}
  }

  


}// end BST










my insert method works really fine but i got confused and stuck while trying to implement the remove method.

i don't know where to go for case 3/4 because i have a book that describes 4 cases that can happen when trying to delete a element from a node from a binary tree. here is what i have so far.

 public boolean remove(Node root, int data)
  {
		  Node cursor = root;		
		if(root == null)
			return false;
		
		if(root.left == null)
		{	
			root = root.right;
			return true; 	
		}
		while(cursor != null)
		{
			if(data == cursor.data)
			{
				if(cursor.left == null)
					
			}
			else if(data < cursor.data)
			{
				cursor = cursor.left;
			}
			else if(data > cursor.data)
			{
				cursor = cursor.right;
			}
				 
		}
		return false;
			
  }



Was This Post Helpful? 0
  • +
  • -

#4 jajuje2000  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 14-February 08

Re: ADD method for my Binary search tree

Posted 21 October 2009 - 03:03 PM

nevermind i finished the remove method thanks for helping out pbl.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1