My program is supposed to take in a text file of words, put the words on a binary search tree then print out the words in alphabetical order with their line number... can anyone help me with my errors?
/* Amanda Sgroi
Program #9
Due: November 16, 2006
This program is designed to take in a text file of words then output the words in alphabetical order along with the corresponding
line in which the word occured.*/
import java.io.*;
import java.util.Scanner;
import java.util.LinkedList;
/************************ BST.java **************************
* generic binary search tree
*/
class BST<T> {
protected BSTNode root = null;
public BST() {
}
publicvoid clear() {
root = null;
}
publicboolean isEmpty() {
return root == null;
}
publicvoid insert(Comparable el) {
BSTNode<T> p = root, prev = null;
while (p != null) { // find a place for inserting new node;
prev = p;
if (p.el.compareTo(el) < 0)
p = p.right;
else p = p.left;
}
if (root == null) // tree is empty;
root = new BSTNode<T>(el);
elseif (prev.el.compareTo(el) < 0)
prev.right = new BSTNode<T>(el);
else prev.left = new BSTNode<T>(el);
}
publicboolean isInTree(Comparable el) {
return search(el) != null;
}
public Comparable search(Comparable el) {
return search(root,el);
}
protected Comparable search(BSTNode<T> p, Comparable el) {
while (p != null)
if (el.equals(p.el))
return p.el;
elseif (el.compareTo(p.el) < 0)
p = p.left;
else p = p.right;
returnnull;
}
publicvoid preorder() {
preorder(root);
}
publicvoid inorder() {
inorder(root);
}
publicvoid postorder() {
postorder(root);
}
protectedvoid visit(BSTNode<T> p) {
System.out.print(p.el + " ");
}
protectedvoid inorder(BSTNode<T> p) {
if (p != null) {
inorder(p.left);
visit(p);
inorder(p.right);
}
}
protectedvoid preorder(BSTNode<T> p) {
if (p != null) {
visit(p);
preorder(p.left);
preorder(p.right);
}
}
protectedvoid postorder(BSTNode<T> p) {
if (p != null) {
postorder(p.left);
postorder(p.right);
visit(p);
}
}
}
/************************ BSTNode.java **************************
* node of a generic binary search tree
*/
class BSTNode<T> {
protected Comparable el;
protected BSTNode<T> left, right;
protected BSTNode<T> root = null;
public BSTNode() {
left = right = null;
}
public BSTNode(Comparable el) {
this(el,null,null);
}
public BSTNode(Comparable el, BSTNode lt, BSTNode rt) {
this.el = el; left = lt; right = rt;
}
}
class Tokens extends Word{
void readTokens2(String fInName) throws IOException{
int linecounter = 1;
BufferedReader fIn = new BufferedReader(new FileReader(fInName));
String s;
while((s = fIn.readLine()) != null){
java.util.StringTokenizer line = new java.util.StringTokenizer(s);
while(line.hasMoreTokens()){
BST<Word> tree = new BST<Word>();
tree.search(line.nextToken());
if(!(tree.isInTree(line.nextToken()))){
BSTNode<Word> node = new BSTNode<Word>();
tree.insert(node);
}
else
list.insertAtTail(linecounter);
System.out.println(linecounter + " " + line.nextToken());
tree.inorder();
}
}
++linecounter;
fIn.close();
}
}
class Word implements Comparable{
private String word = "";
publicint freq = 1;
LinkedList<Integer> list = new LinkedList<Integer>();
public Word(){
}
public Word(String s){
word = s;
}
publicboolean equals(Object el){
return word.equals(((Word)el).word);
}
publicint compareTo(Object el){
return word.compareTo(((Word)el).word);
}
public String toString(){
return word + ": " + freq + "";
}
}
class BSTTest extends BST<Word>{
publicstaticvoid main(String[] args)throws IOException{
Scanner kb = new Scanner(System.in);
System.out.println("Please Enter Your File Name: ");
String fInName = kb.next();
Tokens t = new Tokens();
t.readTokens2(fInName);
}
}
E:\BSTTest.java:144: insert(java.lang.Comparable) in BST<Word> cannot be applied to (BSTNode<Word>)
tree.insert(node);
^
E:\BSTTest.java:147: cannot find symbol
symbol : method insertAtTail(int)
location: class java.util.LinkedList<java.lang.Integer>
list.insertAtTail(linecounter);
^
Note: E:\BSTTest.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
2 errors
ok i understand number 2.. that was a stupid mistake
can you show me what you mean by number one because
my insert method looks like this
public void insert(Comparable el)
Exactly. It expects a Comparable. But the argument you are giving to it (BSTNode) is not Comparable, because BSTNode doesn't implement the Comparable interface like for example your class Word does. Just implement Comparable in your BSTNode class.
E:\BSTTest.java:111: BSTNode is not abstract and does not override abstract method compareTo(java.lang.Object) in java.lang.Comparable
class BSTNode<T> implements Comparable {
^
E:\BSTTest.java:111: BSTNode is not abstract and does
not override abstract method
compareTo(java.lang.Object) in java.lang.Comparable
class BSTNode<T> implements Comparable {
^
no i got this
Of course, you have to implement the method of that interface, like you did in the Word class.
void readTokens2(String fInName) throws IOException{
int linecounter = 1;
BufferedReader fIn = new BufferedReader(new FileReader(fInName));
String s;
String token; // a String for the token
BST<Word> tree = new BST<Word>(); //you must use the same tree for all the words
while((s = fIn.readLine()) != null){
java.util.StringTokenizer line = new java.util.StringTokenizer(s);
while(line.hasMoreTokens()){
token = line.nextToken(); // nextToken() must only be called once per cycle
tree.search(token);
if(!(tree.isInTree(token))){
BSTNode<Word> node = new BSTNode<Word>(token);
tree.insert(node);
}
else
list.addLast(linecounter);
//System.out.println(linecounter + " " + token); //no need to print this
}
linecounter++; //increment after the line is over
}
tree.inorder(); // printing the tree after all words are in
fIn.close();
}
also, in BSTNode:
//you have to provide a toString method in order to be able to use
// System.out.print(p.el + " ") in BST.visit
public String toString() {
return el.toString();
}
Ok, here's a working code. It's quite ugly, though. Compare it with what you have to see the differences, then you should clean it up a bit :)
/* Amanda Sgroi
Program #9
Due: November 16, 2006
This program is designed to take in a text file of words then output the words in alphabetical order along with the corresponding
line in which the word occured.*/
import java.io.*;
import java.util.Scanner;
import java.util.LinkedList;
/************************ BST.java **************************
* generic binary search tree
*/
class BST<T> {
protected BSTNode<T> root = null;
public BST() {
}
publicvoid clear() {
root = null;
}
publicboolean isEmpty() {
return root == null;
}
publicvoid insert(Comparable el) {
BSTNode<T> p = root, prev = null;
while (p != null) { // find a place for inserting new node;
prev = p;
if (p.el.compareTo(el) < 0) {
p = p.right;
} else {
p = p.left;
}
}
if (root == null) // tree is empty;
root = new BSTNode<T>(el);
elseif (prev.el.compareTo(el) < 0)
prev.right = new BSTNode<T>(el);
else prev.left = new BSTNode<T>(el);
}
publicboolean isInTree(Comparable el) {
return search(el) != null;
}
public Comparable search(Comparable el) {
return search(root,el);
}
protected Comparable search(BSTNode<T> p, Comparable el) {
while (p != null) {
if (p.el.compareTo(new BSTNode<T>(el)) == 0)
return p.el;
elseif (p.el.compareTo(new BSTNode<T>(el)) < 0)
p = p.right;
else p = p.left;
}
returnnull;
}
publicvoid preorder() {
preorder(root);
}
publicvoid inorder() {
inorder(root);
}
publicvoid postorder() {
postorder(root);
}
protectedvoid visit(BSTNode<T> p) {
System.out.println(p.el);
}
protectedvoid inorder(BSTNode<T> p) {
if (p != null) {
inorder(p.left);
visit(p);
inorder(p.right);
}
}
protectedvoid preorder(BSTNode<T> p) {
if (p != null) {
visit(p);
preorder(p.left);
preorder(p.right);
}
}
protectedvoid postorder(BSTNode<T> p) {
if (p != null) {
postorder(p.left);
postorder(p.right);
visit(p);
}
}
}
/************************ BSTNode.java **************************
* node of a generic binary search tree
*/
class BSTNode<T> implements Comparable{
protected Comparable el;
protected BSTNode<T> left, right;
protected BSTNode<T> root = null;
public BSTNode() {
left = right = null;
}
public BSTNode(Comparable el) {
this(el,null,null);
}
public BSTNode(Comparable el, BSTNode lt, BSTNode rt) {
this.el = el; left = lt; right = rt;
}
publicint compareTo(Object o) {
return el.compareTo(((BSTNode<T>) o).el);
}
//you have to provide a toString method in order to be able to use
// System.out.print(p.el + " ") in BST.visit
public String toString() {
return el.toString();
}
}
class Tokens extends Word{
void readTokens2(String fInName) throws IOException{
int linecounter = 1;
BufferedReader fIn = new BufferedReader(new FileReader(fInName));
String s;
BSTNode<Word> node;
String token; // a String for the token
BST<Word> tree = new BST<Word>(); //you must use the same tree for all the words
while((s = fIn.readLine()) != null){
java.util.StringTokenizer line = new java.util.StringTokenizer(s);
while(line.hasMoreTokens()){
token = line.nextToken(); // nextToken() must only be called once per cycle
//tree.search(token);
if(!(tree.isInTree(new Word(token)))){
node = new BSTNode<Word>(new Word(token));
tree.insert(node);
} else {
node = (BSTNode<Word>) tree.search(new Word(token));
}
((Word) (node.el)).list.addLast(linecounter);
//System.out.println(linecounter + " " + token); //no need to print this
}
linecounter++;
}
tree.inorder(); // printing the tree after all words are in
fIn.close();
}
}
class Word implements Comparable{
private String word = "";
publicint freq = 1;
LinkedList<Integer> list = new LinkedList<Integer>();
public Word(){
}
public Word(String s){
word = s;
}
publicboolean equals(Object el){
return word.equals(((Word)el).word);
}
publicint compareTo(Object el){
return word.compareTo(((Word)el).word);
}
public String toString(){
String result = word + "; lines: ";
for (int i = 0; i < list.size(); i++) {
result += list.get(i) + "; ";
}
return result;
}
}
publicclass BSTTest extends BST<Word>{
publicstaticvoid main(String[] args)throws IOException{
Scanner kb = new Scanner(System.in);
System.out.println("Please Enter Your File Name: ");
String fInName = kb.next();
Tokens t = new Tokens();
t.readTokens2(fInName);
}
}