This is one of three trees I am using to parse text and print out every unique word, the line numbers it appears on, and the number of times it appears. I have already used a hash and tree map and have both of them working correctly. My instructor said that we need to use an AVL tree for the third map and gave us code. This is where I am unsure where to go. I will include the method that creates/fills the AVL map as well as the AVL map class. If further code is needed, I will gladly include it.
AvlMap class
//AvlMap class. implements map and extends Comparable @SuppressWarnings("rawtypes") public class AvlMap <AnyType extends Comparable<? super AnyType>> implements Map { //construct the tree public AvlMap( ){ root = null; } //inserts object into the map public void insert( AnyType x, AnyType y ){ root = insert( x, y, root ); } //removes the key from the map public void remove( AnyType x ){ root = remove( x, root ); } //internal method to remove from a subtree private AvlNode<AnyType> remove( AnyType x, AvlNode<AnyType> t ){ if( t == null ) return t; // Item not found; do nothing int compareResult = x.compareTo( t.key ); if( compareResult < 0 ) t.left = remove( x, t.left ); else if( compareResult > 0 ) t.right = remove( x, t.right ); else if( t.left != null && t.right != null ) // Two children { t.key = findMin( t.right ).key; t.right = remove( t.key, t.right ); } else t = ( t.left != null ) ? t.left : t.right; return balance( t ); } . . . @Override public Object get(Object key) { //method in question }
WordStats class
import java.util.SortedSet; import java.util.TreeSet; //WordStats class public class WordStats { //local variable occurrences and creates new set with lineNumbers private int occurrences; private SortedSet<Integer> lineNumbers = new TreeSet<Integer>(); //adds line number to the set public void addOccurrence(int lineNumber){ occurrences++; lineNumbers.add(lineNumber); } //returns the occurrences variable public int getOccurrences(){ return occurrences; } //returns the line numbers stored in the set public SortedSet<Integer> getLines(){ return lineNumbers; } }
This is the method specifically called to run everything for the AVL Map
public void avlMap(String fileName){ //try/catch block to check for invalid file name try{ //creates new bufferedreader object for input file BufferedReader inFile = new BufferedReader(new FileReader(fileName)); //creates new treeMap object named avlMap Map avlMap = new AvlMap(); String oneLine; //Read the words and add them to the avlMap for (int lineNum = 1; (oneLine = inFile.readLine()) != null; lineNum++){ String delims = " !@#$%{}[]/^&*,.()-;:\'\"\t"; StringTokenizer st = new StringTokenizer(oneLine, delims); while (st.hasMoreTokens()){ String word = st.nextToken(); //vvvvvvvvvvvvvvv this is the line that is causing problems----------------------------------------------- WordStats stats = (WordStats)avlMap.(word); //if WordStats is empty/doesnt exist, //if exists, adds line numbers into a WordStats in the value //...field for the int's respective key if (stats == null){ stats = new WordStats(); avlMap.put(word, stats); } //WordStats already exists for the word //calls addOccurrence method and adds the line number stats.addOccurrence(new Integer(lineNum)); } } //creates a new iterator object to iterate entries Iterator itr = avlMap.entrySet().iterator(); //runs printEntry for each key in the treeMap while (itr.hasNext()){ printEntry((Map.Entry)itr.next()); } } //the file name used wasn't able to be reached catch(IOException e){ e.printStackTrace(); } }
I commented the line causing problems. I need to see if the wordstats already exists and return it, or return null if it doesnt.