Lab 8: Tips and Tricks
This document should help you to organize your design and plan your implementation for the Super Lexicon Lab. Be sure to read the lab description thoroughly!
Super Lexicon Classes
Main.java: driver program. No need to modify!Lexicon.java: Interface. No need to modify!LexiconTrie.java: implements interface. You will make changes here.LexiconNode.java: a node in the trie. You will make changes here. Methods here do not need to be recursive! Also, if you use a list-like data structure to store children, then when you add, be sure to add nodes in order. You don’t necessarily have to use anOrderedStructure, although anOrderedStructurecould work.
Miscellaneous Notes
- Unlike our
BinaryTreeimplementation, ourLexiconTrieconsists ofLexiconNodes. So in some ways, this is similar to ourSinglyLinkedListimplementation. - Only
matchRegexandsuggestCorrectionshave to be recursive. Other methods can be done recursively, but you will not be penalized in any way if you choose to implement them iteratively. Do not make the other methods overly complicated! Test your code frequently!
Suggested Approach
- Start with the section titled Managing Trie Node Children and implement
LexiconNode. Pick a data structure that will store the children of the node. Work through the methods inLexiconNode. Note that a simple way to comparechars is to subtract one character from another. - After completing
LexiconNode.java, move on toLexiconTrie.java. You’ll need to add a constructor. The constructor should create a singleLexiconNodethat has the character assigned to be' '(a blank space). - The section Searching for Prefixes and Words describes
containsWordandcontainsPrefix. The technique used in both of these methods is basically the same.containsWordperforms one additional test before returning to see if theisWordflag is set to betrue. You may find that creating a helper methodLexiconNode find(String word)that returnsnullor aLexiconNodeto be helpful here. Note that you can implement this method with or without recursion! Either way is acceptable. - Move on to
addWordandaddWordsFromFile.addWordsFromFilewill use aScannerto parse an input file (line by line, with a single word per line) and calladdWordfor each line. Be sure to updatesize. Convert everything to lowercase, too. removeWordmay be implemented recursively or iteratively. If you choose to do it recursively, you may want to use a helper method. Either way, be sure to returntrueif the word appeared in the lexicon and was removed, andfalseotherwise. This method is tricky, so think before you type!- For the iterator (see Other Trie Operations), create a helper method that recursively builds a
Vectorof words. Keep in mind that theLexiconNodes already maintain a sequence of their children in sorted order. That will help you iterate over the trie in alphabetical order easily. - The section Optional Extensions describes optional extensions for the lab. In these sections, you implement two recursive methods for manipulating the trie. You may create helper methods as needed for both of these methods. You may find it helpful to revisit the
printSubsetSumandcountSubsetSummethods from the Recursion Lab.