Question

Indexing With Trees Andrew Rosen Abstract a this lab you ate an ind r the wkd W Sa e (gt00.tat) althogh yos c tr w t yor abli


Abstract In this lab you create an index for the complete works of William Shakespeare (pg100.txt), although you can use it o
0 0
Add a comment Improve this question Transcribed image text
Answer #1

// IndexNode.java
import java.util.ArrayList;
import java.util.List;
public class IndexNode {

   // The word for this entry
   String word;
   // The number of occurrences for this word
   int occurrences;
   // A list of line numbers for this word
   List<Integer> list;
  
   IndexNode left;
   IndexNode right;
  
   // Constructors
   // Constructor should take in a word and a line number
   // it should initialize the list and set occurrences to 1
   public IndexNode(String word, int line_number)
   {
       this.word = word;
       this.occurrences = 1;
       list = new ArrayList<Integer>();
       list.add(line_number);
       left = null;
       right = null;
   }
  
   // return the word, the number of occurrences and the lines it appears on
   public String toString()
   {
       return("Word : "+word+" Occurrences : "+occurrences+" Line numbers : "+list.toString());
   }
}
//end of IndexNode.java

//IndexTree.java

public class IndexTree {

      

       private IndexNode root;

      

       public IndexTree()

       {

             root = null;

       }

      

       // method to add a word in the tree

       public void add(String word, int lineNumber)

       {

             root = add(root,word,lineNumber);

       }

      

       private IndexNode add(IndexNode root, String word, int lineNumber)

       {

             if(root == null)

             {

                    root = new IndexNode(word,lineNumber);

             }else

             {

                    if(word.toLowerCase().compareTo(root.word.toLowerCase()) < 0)

                    {

                           root.left = add(root.left,word,lineNumber);

                    }else if(word.toLowerCase().compareTo(root.word.toLowerCase()) > 0)

                    {

                           root.right = add(root.right,word,lineNumber);

                    }else

                    {

                           if(!root.list.contains(lineNumber))

                           {

                                 root.occurrences++;

                                 root.list.add(lineNumber);

                           }

                    }

             }

            

             return root;

       }

      

       //returns true if the word is in the index

       public boolean contains(String word)

       {

             if(root == null)

                    return false;

             IndexNode node = root;

             while(node != null)

             {

                    if(node.word.equalsIgnoreCase(word))

                           return true;

                    else if(word.toLowerCase().compareTo(node.word.toLowerCase()) < 0)

                           node = node.left;

                    else

                           node = node.right;

             }

            

             return false;

       }

      

       // method to delete a word

       public void delete(String word)

       {

             if(root == null)

             {

                    System.out.println(" Tree Empty");

             }else if(!contains(word))

                    System.out.println(word+" not found in tree");

             else {

                    root = delete(root,word);

                    System.out.println(word+" deleted from tree");

             }

       }

      

       //remove the word and all entries for the word

       private IndexNode delete(IndexNode root,String word)

       {

             IndexNode p,n;

            

             if(word.equalsIgnoreCase(root.word))

             {

                    IndexNode lt,rt;

                    lt = root.left;

                    rt = root.right;

                   

                    if(lt == null && rt == null)

                           return null;

                    else if(lt == null)

                           return rt;

                    else if(rt == null)

                           return lt;

                    else {

                           p = rt;

                           while(p.left != null)

                                 p = p.left;

                           p.left = lt;

                           return rt;

                    }

             }

             else if(word.toLowerCase().compareTo(root.word.toLowerCase()) < 0)

             {

                    n = delete(root.left,word);

                    root.left = n;

             }

             else

             {

                    n = delete(root.right,word);

                    root.right = n;

             }

            

             return root;

       }

      

       // prints all the words in the index in inorder order

       public void printIndex()

       {

             printIndex(root);

       }

      

       private void printIndex(IndexNode root)

       {

             if(root == null)

             {

                    return;

             }

             printIndex(root.left);

             System.out.println(root);

             printIndex(root.right);

       }

}

//end of IndexTree.java

Add a comment
Know the answer?
Add Answer to:
Indexing With Trees Andrew Rosen Abstract a this lab you ate an ind r the wkd...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Recursion and Trees Application – Building a Word Index Make sure you have read and understood...

    Recursion and Trees Application – Building a Word Index Make sure you have read and understood ·         lesson modules week 10 and 11 ·         chapters 9 and 10 of our text ·         module - Lab Homework Requirements before submitting this assignment. Hand in only one program, please. Background: In many applications, the composition of a collection of data items changes over time. Not only are new data items added and existing ones removed, but data items may be duplicated. A list data structure...

  • C++ Lab 1. Read in the contents of a text file up to a maximum of...

    C++ Lab 1. Read in the contents of a text file up to a maximum of 1024 words – you create your own input. When reading the file contents, you can discard words that are single characters to avoid symbols, special characters, etc. 2. Sort the words read in ascending order in an array (you are not allowed to use Vectors) using the Selection Sort algorithm implemented in its own function. 3. Search any item input by user in your...

  • C PROGRAM STRING AND FILE PROCESSING LEAVE COMMENTS! I WILL LEAVE POSITIVE REVIEW! THANK YOU :)...

    C PROGRAM STRING AND FILE PROCESSING LEAVE COMMENTS! I WILL LEAVE POSITIVE REVIEW! THANK YOU :) Use FILE I need a program that 1) Count all words in a file. A word is any sequence of characters delimited by white space or the end of a sentence, whether or not it is an actual English word. 2)Count all syllables in each word. To make this simple, use the following rules: •Each group of adjacent vowels (a, e, i, o, u,...

  • For this week's lab, you will use two of the classes in the Java Collection Framework:...

    For this week's lab, you will use two of the classes in the Java Collection Framework: HashSet and TreeSet. You will use these classes to implement a spell checker. Set Methods For this lab, you will need to use some of the methods that are defined in the Set interface. Recall that if set is a Set, then the following methods are defined: set.size() -- Returns the number of items in the set. set.add(item) -- Adds the item to the...

  • The language is C++. Code needs to be added to line 28 and 37. Could someone...

    The language is C++. Code needs to be added to line 28 and 37. Could someone provide some help with how to do it? CODE Write a program to unscramble words A file with list of words is provided along with a template program to get you started. 1. Define a struct with 2 string members: sorted and original 2. Declare an array of 200000 elements with data type of the struct 3. Read from a file a list of...

  • For this week's lab, you will use two of the classes in the Java Collection Framework:...

    For this week's lab, you will use two of the classes in the Java Collection Framework: HashSet and TreeSet. You will use these classes to implement a spell checker. Set Methods For this lab, you will need to use some of the methods that are defined in the Set interface. Recall that if set is a Set, then the following methods are defined: set.size() -- Returns the number of items in the set. set.add(item) -- Adds the item to the...

  • Hello! i need a program in C that uses FILE*! The instructions are below! Please dont...

    Hello! i need a program in C that uses FILE*! The instructions are below! Please dont copy paste another chegg post! Thank you! Will leave positive review! I need a program that 1) Count all words in a file. A word is any sequence of characters delimited by white space or the end of a sentence, whether or not it is an actual English word. 2)Count all syllables in each word. To make this simple, use the following rules: •Each...

  • In this lab you will write a spell check program. The program has two input files:...

    In this lab you will write a spell check program. The program has two input files: one is the dictionary (a list of valid words) and the other is the document to be spellchecked. The program will read in the words for the dictionary, then will read the document and check whether each word is found in the dictionary. If not, the user will be prompted to leave the word as is or type in a replacement word and add...

  • 26-ary tree for spell checker in JAVA You are asked to write some functionalities for a...

    26-ary tree for spell checker in JAVA You are asked to write some functionalities for a spelling checker inside Tree.java that has at least the following methods: /*Adds a word to a spelling checker’s collection of correctly spelled words*/ void add(String word) /*Returns true if the given word is spelled correctly*/ boolean check(String word) /*Returns back all words in the tree in alphabetical order*/ public String getDictionaryInAlphabeticalOrder() Store the collection of correctly spelled words in a 26-ary tree. The number...

  • Complete the following code: You are asked to write some functionalities for a spelling checker i...

    Complete the following code: You are asked to write some functionalities for a spelling checker inside Tree.java that has at least the following methods: /*Adds a word to a spelling checker’s collection of correctly spelled words*/ void add(String word) /*Returns true if the given word is spelled correctly*/ boolean check(String word) /*Returns back all words in the tree in alphabetical order*/ public String getDictionaryInAlphabeticalOrder() Store the collection of correctly spelled words in a 26-ary tree. The number 26 indicates that...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT