Write a cross-reference Java program that constructs a self-balancing binary search tree, such as an AVL tree, with all words included from a text file and records the line numbers on which these words were used. These line numbers should be stored on linked lists associated with the nodes of the tree. The same line should not be listed more than once for a particular word. Before processing the word, any punctuation adjacent to the word should be removed and all the characters in the word should be converted to lower case. After the input file has been processed, print in alphabetical order all words of the text file along with the corresponding list of numbers of the lines in which the words occur to an output file. The output file should be called index.txt.

Classes to create: If you do not want to use the AVL Tree then you can create your own, but it must be a balanced search tree.

WordData

Create a class WordData to use as the data type for your BinaryTreeNode. The class should contain a field for the word and a field for the list of lines that the word is found on. You should have a default constructor and probably a constructor that takes a string for the word as well as a copy constructor. You should include getters for the word and the list of lines. Include a method to add a line number to your list of line numbers. You will need to implement the Comparable interface and then implement the compareTo() method. You will also need to override the methods equals(), hashcode(), and toString().

I have the WordData class here, but need help with the driver

import java.util.Iterator;
import java.util.Objects;
import java.util.TreeSet;

public class WordData implements Comparable<WordData> {

    private String word;
    private TreeSet<Integer> lines;

    public WordData() {
        word = "";
        lines = new TreeSet<>();
    }

    public WordData(String word) {
        this.word = word;
        lines = new TreeSet<>();
    }

    public WordData(WordData data) {
        word = data.word;
        lines = new TreeSet<>();
        Iterator<Integer> it = data.getLines().iterator();
        while (it.hasNext()) {
            lines.add(it.next());
        }
    }

    public String getWord() {
        return word;
    }

    public void addLine(int line) {
        lines.add(line);
    }

    public TreeSet<Integer> getLines() {
        return lines;
    }

    @Override
    public int compareTo(WordData w) {
        return this.word.compareTo(w.word);
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 23 * hash + Objects.hashCode(this.word);
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final WordData other = (WordData) obj;
        if (!Objects.equals(this.word, other.word)) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        String result = word + ": ";

        Iterator<Integer> it = lines.iterator();
        if (it.hasNext())
            result += it.next();

        while (it.hasNext()) {
            result += ", " + it.next();
        }

        return result;
    }
}

Need help with this

import java.io.*;
import java.util.*;


public class CrossReferenceDriver {

    private static TreeSet<String> words;
    private static TreeSet<Integer> lines;


    public static void main(String[] args) throws Exception {

        String fileName = "GulliversTravelsExcerpt.txt";

        int lineNumber = 1;


        try {
            BufferedReader reader;

            reader = openFile(fileName);

            String st;
            TreeSet<String> words = new TreeSet<>();
            TreeSet<Integer> lines = new TreeSet<>();
            while ((st = reader.readLine()) != null) {

                lineNumber++;

                String wordsLine[] = st.split(" ");

                for (String word : wordsLine) {
                    word.replaceAll("\\w+", " ");
                    words.add(word);
                    WordData wordData = new WordData(word);
                    lines.add(lineNumber);

                    WordData wordData = new WordData(word, lines);
                }
            }


        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        for (String word : words){
            System.out.println(word + ": " + lineNumber);
        }


    }

    public static BufferedReader openFile(String fileName) throws FileNotFoundException {
        return new BufferedReader(new FileReader(fileName));
    }

    public static TreeSet<Integer> addLineNumber(int number) {
        lines.add(number);
        return lines;
    }
}

The program, once working, should produce an output file listing the words in alphabetical order and the line numbers that they appear on, which will look something like this:

a: 1, 2, 4, 6, 8, 10, 11, 12, 17, 18, 19, 22, 23, 26, 29, 35, 37, 42, 51

able: 35, 47

abounding: 20

about: 4, 10

above: 18

absurd: 51

abuses: 18

abusing: 25

academy: 7

0 Answers