Binary Search Tree

To create a binary search tree using Java OOP

Page 1 of 1

4 Replies - 2583 Views - Last Post: 25 April 2009 - 04:03 AM Rate Topic: -----

#1 porv24  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 23-April 09

Binary Search Tree

Posted 23 April 2009 - 07:10 AM

Hi, I want to do a Binary Search tree that imports the packages dataobjects and binarynodes. The class ObjectData and the class BNode are compiling well but the class BinSearchTree says that it "cannot find the variable key". I do not know how to correct this. Any help please? Thank you :)

******************************************************************************************************
 package dataobjects;
import java.util.*;

public class ObjectData {
	
	public int objNo;	//sequence no. of the object
	Scanner inp = new Scanner(System.in);
		
	// Constructor 
	public ObjectData() {
	}
	
	//Methods
	public void setObjNo(int num) {
		objNo = num;
	}
	
	public String getData() {
	//return data of the object in form of the string including its objectNumber
		return Integer.toString(objNo); 
	}
	
	public void editData() {	
	//to edit object data by entering the new values from the keyboard
		System.out.println("Enter number: ");
		int num = inp.nextInt();
		objNo = num;
	}
		
	
}

//**********************************************************************************************************
package binarynodes;
import dataobjects.*;
import java.util.*;

public class BNode extends ObjectData {


	public BNode left;
	public BNode right;
	public ObjectData data;
	public ObjectData temp;	
	Scanner inp = new Scanner(System.in);

	public BNode(ObjectData newObj) {
		left = null;
		right = null;
		data = newObj;
	}

	public void show()  {
		System.out.println(data.getData());
	}

	void showList() {
		BNode temp = left;
		  while (temp != null) {
			  temp.show();
			  temp = temp.right;
		  }
	}


	  public void editBNode(String keyName){
			  
		  System.out.println("Enter number: ");
		int num = inp.nextInt();
		objNo = num;

		System.out.println("Please enter new name:  ");
		keyName = inp.next();
		key = keyName;
		
		if (temp != null)
			  data.editData();
	  }

}

//**************************************************************************************************************
package nonLinearstructures;
import dataobjects.*;
import binarynodes.*;

public class BinSearchTree extends ObjectData {

	BNode root;

	public void BinSearchTree() {	//constructor
		root = null;
	}

	//adding new object to the tree keeping it sorted in order of a key (String type)
	protected void insert(ObjectData newObj) {
		BNode temp = new BNode(newObj);
		root = inorderBST(root,temp);
	}

	protected BNode inorderBST(BNode rt,BNode newNode) {
		if(rt == null)
			rt = newNode;
		else if(newNode.data.key.compareTo(rt.data.key) < 0)
				rt.left = inorderBST(rt.left,newNode);
			else rt.right = inorderBST(rt.right,newNode);
		return rt;
	}

	//b) searching for the object by the key value returning reference to the object if found otherwise null
	protected BNode searchBST(BNode rt, String keyName) {
		if (rt == null)
			return null;
		else if(keyName.compareTo(rt.data.key) == 0)
				return rt;
			else if(keyName.compareTo(rt.data.key) < 0)
					return searchBST(rt.left,keyName);
				else return searchBST(rt.right,keyName);
	}
	
	protected ObjectData search(String keyName) {
		BNode temp = searchBST(root,keyName);
		if(temp == null)
			return null;
		else return temp.data;
	}
}

//***************************************************************************************************************
import dataobjects.*;
import binarynodes.*;

import java.util.*;

public class MainProg {
	public static void main(String args[]) {

		BinSearchTree myBST = new BinSearchTree();
		Scanner myinp = new Scanner(System.in);
		
		for(int k=0; k<5; k++) {
			ObjectData newObj = new ObjectData();
			myBST.insertBST(newObj);
		}
		
		myBST.inorderBST();
		
		System.out.println("Enter Surname to search:");
		String key = myinp.next();
		ObjectData objFound = myBST.searchBST(key);
		if(objFound != null)
			objFound.editdata();	


	}
}




Is This A Good Question/Topic? 0
  • +

Replies To: Binary Search Tree

#2 Mikeyp926  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 29
  • View blog
  • Posts: 205
  • Joined: 20-March 09

Re: Binary Search Tree

Posted 23 April 2009 - 08:29 AM

Where are you getting this error? Which line in your code?
Was This Post Helpful? 0
  • +
  • -

#3 porv24  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 23-April 09

Re: Binary Search Tree

Posted 23 April 2009 - 08:32 AM

View PostMikeyp926, on 23 Apr, 2009 - 07:29 AM, said:

Where are you getting this error? Which line in your code?


The error comes when compiling BinSearchTree lines 22, 32 and 34. It says cannot find symbol variable key.
Was This Post Helpful? 0
  • +
  • -

#4 Mikeyp926  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 29
  • View blog
  • Posts: 205
  • Joined: 20-March 09

Re: Binary Search Tree

Posted 23 April 2009 - 09:53 AM

oh, right, that makes sense.
Well you are saying "key=keyname" but nowhere in your program have you declared a variable "key". So the question is, what are you trying to do with that statement? Should there be a member variable named "key"? What are you trying to accomplish? If you figure this out then you will know how to solve your problem. Maybe you don't even need the "key" variable at all?
Anyway, try and work through the code and figure out what it is actually doing, this should help you figure out what that line of code should be doing that is causing problems.

-Michael
Was This Post Helpful? 0
  • +
  • -

#5 porv24  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 23-April 09

Re: Binary Search Tree

Posted 25 April 2009 - 04:03 AM

Thanks, I will try to do that :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1