Question:
help with java files please?
suma29
2009-04-24 01:44:57 UTC
1. The program should read input (from scores.txt) name and score. Based on name, you must check if this “name” already exists in the binary tree.
John 70
Sara 85
Jenny 75
Peter 45
John 89
Peter 70

If the name does not exist, ADD a new node to appropriate location for the binary search tree.
If the name does exist, UPDATE that node by adding the new score to it.


im not sure how to search the file multiple times in order to put the name and number of scores to the same object
Three answers:
Germann A
2009-04-24 04:09:52 UTC
It is VERY simple if you think about it...



Create your tree (empty)...



You read your file one line at a time, // this is a loop

split the data read from file into Name and Score,

if Name already exists in your tree

get Score from tree for given Name

increment the score by the value from the file

endif

store the Name/Score pair in your tree

endloop
elven_rangers
2009-04-24 02:11:45 UTC
You don't have to search the file multiple times. Just read the file once into an arraylist and after that just parse the array as many times as you want.



But the basic logic of a BST (binary search tree) is that you don't have to parse and search multiple times, you should use recursion.



You can use an array representation (for example) and have a Node object which consists of a string name and an array of scores.



The logic is this:

1. you parse the array you create after reading the file

2. for each pair of name/score you search the other array representing your tree, if you find a name then add the score to the node object otherwise create a new node object with the name and the score you have. You do this by having a verification functions that you must run recursively on each subtree.



Mind you, the file is not the tree, if just has data which you must put in a tree, it's just a source of data.



After that, when you have a compliant binary tree, you may (for example) write it back in the file, having each name just once and next to each name a list of scores.
deonejuan
2009-04-24 02:21:01 UTC
You don't indicate what type of datastore you are using. In any event, you will need Objects, and it looks like class Student IF you don't use String. String implements Comparable, a custom Object, like Student, would need code to do the same thing as insert in the proper order. Or, you can hard-code "buckets". Some of the classroom binary exercises I've seen recursively look for 'J', then the second char, etc. adding to a temp bucket.



But, you last statement, search a file multiple times? Use and ArrayList data to read the file one line at a time from the file into data. Then, you can read ArrayList data as often as you like.



class Student implements Comparable {

String name;

String score;

public Student( String dataline ) {

String[] flds = dataline.split(" ");

name = flds[0];

score = flds[1];

}

}


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...