Results 1 to 3 of 3
  1. #1
    Join Date
    Sep 2011
    Posts
    14
    Rep Power
    0

    Default Problems creating a new BinarySearchTree class object

    Hello,

    I am trying to build a binary search tree and am getting an error back that I can't figure out. Below is the method that's causing the error, though I have not finished the method yet either:

    Java Code:
    	private static void buildBST(CreateObject[] array, String key)
    	{
    		BinarySearchTree tree;
    		
    		tree=new BinarySearchTree(key, array);
    	}
    and it is coming back with this annoying error:

    cannot find symbol
    symbol : constructor BinarySearchTree(java.lang.String,CreateObject[])
    location: class BinarySearchTree
    tree=new BinarySearchTree(key, array);
    ^
    and my BinarySearchTree class looks like this:

    Java Code:
    import java.util.*;
    
    public class BinarySearchTree
    {
    	private class TreeNode
    	{
    		private String key;
    		private CreateObject[] value;
    		private TreeNode leftChild, rightChild;
    		
    		public TreeNode(String inKey, CreateObject[] inVal)
    		{
    			key=inKey;
    			value=inVal;
    			rightChild=null;
    			leftChild=null;
    		}
    		
    		public String getKey()
    		{
    			return key;
    		}
    		
    		public void setKey(String inKey)
    		{
    			if (inKey==null) 
    			{
    				throw new IllegalArgumentException("Key cannot be null");
    			}
    			key=inKey;
    		}
    		
    		public CreateObject[] getValue()
    		{
    			return value;
    		}
    		
    		public void setValue(CreateObject[] inValue)
    		{
    			value=inValue;
    		}
    		
    		public TreeNode getLeft()
    		{
    			return leftChild;
    		}
    		
    		public void setLeft(TreeNode inLeft)
    		{
    			leftChild=inLeft;
    		}
    	
    		public TreeNode getRight()
    		{
    			return rightChild;
    		}
    		
    		public void setRight(TreeNode inRight)
    		{
    			rightChild=inRight;
    		}
    	}
    	
    	private TreeNode root;
    	
    	public BinarySearchTree()
    	{
    		root=null;
    	} 
    	
    	public Object find(String key)
    	{
    		TreeNode currNode;
    		
    		currNode=root;
    		while ((currNode!=null)&&(key.equals(currNode.getKey())==false)) 
    		{
    			if (key.compareTo(currNode.getKey())<0) 
    			{
    				currNode=currNode.getLeft();
    			}
    			else 
    			{
    				currNode=currNode.getRight();
    			}
    		}
    		if (currNode==null) 
    		{
    			throw new NoSuchElementException("Key "+key+" not found");
    		}
    		return currNode.getValue();
    	}
    	
    	public void insert(String key, CreateObject[] value)
    	{
    		TreeNode currNode, parentNode, newNode;
    		int comparison;
    		
    		currNode=root;
    		parentNode=null;
    		while (currNode!=null) 
    		{
    			parentNode=currNode;
    			comparison=key.compareTo(currNode.getKey());
    			if (comparison==0) 
    			{
    				throw new IllegalArgumentException("Duplicate key: "+key);
    			}
    			else if (comparison<0)
    			{
    				currNode=currNode.getLeft();
    			}
    			else 
    			{
    				currNode=currNode.getRight();
    			}
    		}
    		
    		newNode=new TreeNode(key, value);
    		if (parentNode==null) 
    		{
    			root=newNode;
    		}
    		else if (key.compareTo(currNode.getKey())<0)
    		{
    			parentNode.setLeft(newNode);
    		}
    		else 
    		{
    			parentNode.setRight(newNode);
    		}
    
    	}
    	
    	public void delete(String key)
    	{
    		TreeNode currNode, parentNode;
    		
    		currNode=root;
    		parentNode=null;
    		while ((currNode!=null)&&(key.equals(currNode.getKey())==false)) 
    		{
    			parentNode=currNode;
    			if (key.compareTo(currNode.getKey())<0)
    			{
    				currNode=currNode.getLeft();
    			}
    			else 
    			{
    				currNode=currNode.getRight();
    			}
    		}
    		
    		if (currNode==null) 
    		{
    			throw new NoSuchElementException("Key "+key+" not found");
    		}
    		else if ((currNode.getLeft()==null)||(currNode.getRight()==null))
    		{
    			deleteNodeWithOneOrZeroChildren(currNode, parentNode);
    		}
    		else 
    		{
    			deleteNodeWithTwoChildren(currNode, parentNode);
    		}
    	}
    				
    	public void deleteNodeWithOneOrZeroChildren(TreeNode currNode, TreeNode parentNode)
    	{
    		TreeNode promoteNode;
    		
    		if (currNode.getLeft()!=null) 
    		{
    			promoteNode=currNode.getLeft();
    		}
    		else 
    		{
    			promoteNode=currNode.getRight();
    		}
    			
    		if (parentNode==null) 
    		{
    			root=promoteNode;
    		}
    		else if (parentNode.getLeft()==currNode)
    		{
    			parentNode.setLeft(promoteNode);
    		}
    		else 
    		{
    			parentNode.setRight(promoteNode);
    		}
    	}
    		
    	public void deleteNodeWithTwoChildren(TreeNode currNode, TreeNode parentNode)
    	{
    		TreeNode successorNode, succParentNode;
    		
    		successorNode=currNode.getRight();
    		succParentNode=parentNode;
    		
    		while (successorNode.getLeft()!=null) 
    		{
    			succParentNode=successorNode;
    			successorNode=successorNode.getLeft();
    		}
    		deleteNodeWithOneOrZeroChildren(successorNode, parentNode);
    		
    			currNode.setKey(successorNode.getKey());
    			currNode.setValue(successorNode.getValue());
    	}
    		
    	/* Why are there two height methods? */
    	public int height()
    	{
    		return height(root, 0);
    	}
    	private int height(TreeNode startNode, int htSoFar)
    	{
    		int leftHt, rightHt;
    		if (startNode==null) 
    		{
    			return htSoFar;
    		}
    		else 
    		{
    			leftHt=height(startNode.getLeft(), htSoFar+1);
    			rightHt=height(startNode.getRight(), htSoFar+1);
    		
    			if (leftHt>rightHt) 
    			{
    				return leftHt;
    			}
    			else 
    			{
    				return leftHt;
    			}
    		}
    	}
    }
    Any help in sorting this out will be greatly appreciated :)

  2. #2
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    620
    Rep Power
    4

    Default Re: Problems creating a new BinarySearchTree class object

    You didn't make a constructor for the BinarySearchTree and try to use it and a compiler doesn't understand it.
    Skype: petrarsentev
    http://TrackStudio.com

  3. #3
    Join Date
    Sep 2011
    Posts
    14
    Rep Power
    0

    Default Re: Problems creating a new BinarySearchTree class object

    Good point! Thanks for that :)

Similar Threads

  1. Replies: 9
    Last Post: 07-29-2011, 02:18 PM
  2. binarysearchtree problems help!
    By runachoi in forum New To Java
    Replies: 1
    Last Post: 12-03-2010, 03:35 AM
  3. Replies: 3
    Last Post: 09-29-2010, 09:26 AM
  4. Replies: 2
    Last Post: 03-08-2010, 10:29 AM
  5. Creating object of Type Object class
    By venkatv in forum New To Java
    Replies: 3
    Last Post: 07-17-2007, 03:33 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •