3 Replies - 1064 Views - Last Post: 08 November 2010 - 09:32 PM Rate Topic: -----

#1 SnoBunny85  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 87
  • Joined: 02-March 10

Binary Search Trees

Posted 08 November 2010 - 12:44 PM

I am writing a java application that builds an index tree for an accounts database, and uses this index tree to perform the following functions:
- output the accounts records in the database in ascending order based on their account IDs(inorder traversal)
- read an account ID from the keyboard and output the corresponding account record.
//define a class to hold the account records
import java.io.*;
import java.util.Scanner;

public class Database {
	
   // define array to hold up to 25 account records
  	String AccountDB = new String[25];		--- getting incompatible types error for this line??
                                                           --- i also chose string as my array type because 
                                                of the information that was given to me in the file ACCTFILE.DAT
   // define an account index tree
   AccountDB[0] = element;			         --- not sure if this is right??
   // define an entry into the index tree (TreeNode)
   						        --- have no idea what to do here??
   														
   // define a variable to hold an account ID input by the user
   static Scanner scan = new Scanner(System.in);
   // define a variable to hold the record number
   static int recordNum;

   public static void main (String[] args) throws IOException {
     // initialize record num to 0
     recordNum = 0;
     // read in account id
     System.out.print("Please enter the account ID number:");
     int accountID = scan.nextInt();
     System.out.print(accountID);
     // while not at end  (id=-1)
        // read in account information (names and balance)
        // insert the account info into the array
        // insert the account index into the tree
        // read in account id
     // end while
     // output the acct records in ascending order based on their acct ids
     // read in account ids and return record info until the user enters -1
     // 
   } // main method

     // write a method to write out the account record that corresponds to a
     // particular record number
     

   
     // this method will perform an inorder traversal of the tree and
     // call the WriteTreeNode to write out each element.
   
   
} // class Database





I was also given this coding as well...
import java.text.*;

public class AccountRecord {
	private String id;
	private String firstname;
	private String lastname;
	private float balance;
	
	AccountRecord (String ident, String fn, String ln, float bal) {
		id = ident;
		firstname = fn;
		lastname = ln;
		balance = bal;
	}
	
	public String getId () {
		return id;
	}
	
	
	public String getFirstName() {
		return firstname;
	}
	
	public void setFirstName(String fn){
		firstname = fn;
	}
	
	public String getLastName() {
		return lastname;
	}
	
	public void setLastName(String ln){
		lastname = ln;
	}
	
	public float getBalance () {
		return balance;
	}
	
	public void setBalance(float bal) {
		balance = bal;
	}
	
	public String toString () {
		
		NumberFormat cf = NumberFormat.getCurrencyInstance();
		String result = "";
		result += "Account: " + id + "\t" + lastname + ", " + firstname;
		result += "     Balance: " + cf.format(balance);
		return result;
	}
}



This is the file ACCTFILE.DAT
6274
James
Johnson
415.56
2843
Marcus
Wilson
9217.23
4892
Maureen
Albright
1462.56
8337
Debra
Douglas
27.26
9523
Bruce
Gold
719.32
3165
John
Carlson
1496.24
1892
Mary
Smith
918.26
3924
Simon
Becker
386.85
6023
John
Edgar
9.65
5290
George
Truman
6110.68
8529
Ellen
Fairchild
86.77
1144
Donald
Williams
4114.26
-1
5290
9523
9999
1892
1000
-1



Is This A Good Question/Topic? 0
  • +

Replies To: Binary Search Trees

#2 javadork  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 32
  • View blog
  • Posts: 135
  • Joined: 21-August 10

Re: Binary Search Trees

Posted 08 November 2010 - 01:30 PM

Incompatible types because you are trying to create a String array and store in a String. Try this instead:

String[] AccountDB = new String[25];



Also, your code here

AccountDB[0] = element;



won't work because there is no such thing as 'element', to begin with.
Was This Post Helpful? 0
  • +
  • -

#3 SnoBunny85  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 87
  • Joined: 02-March 10

Re: Binary Search Trees

Posted 08 November 2010 - 02:25 PM

I changed my coding to
String[] AccountDB = new String[25];

and it works alright but i am not sure what to do about the following two steps of coding:

// define an account index tree

// define an entry into the index tree (TreeNode)
Was This Post Helpful? 0
  • +
  • -

#4 Luckless  Icon User is offline

  • </luck>
  • member icon

Reputation: 293
  • View blog
  • Posts: 1,146
  • Joined: 31-August 09

Re: Binary Search Trees

Posted 08 November 2010 - 09:32 PM

You need to build a Tree of Nodes with two leaves for each node in the tree (left and right)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1