Data Structures and Algorithms in Java
GitHubAuthor
  • Preface
    • Syllabus
    • Schedule
  • 0. Getting Started
    • 0.1. Environment Setup
    • 0.2. Quiz
  • 1. Java Essentials
    • 1.1. Abstraction
    • 1.2. Implementation
    • 1.3. Unit Testing
    • 1.4. Quiz
  • 2. Priority Queues
    • 2.1. Simple Priority Queues
    • 2.2. Binary Heap
    • 2.3. Unit Testing
    • 2.4. Benchmarking
    • 2.5. Quiz
  • 3. Sorting Algorithms
    • 3.1. Abstraction
    • 3.2. Comparison-based Sort
    • 3.3. Divide & Conquer Sort
    • 3.4. Distribution-based Sort
    • 3.5. Quiz
    • 3.6. Homework
  • 4. Binary Search Trees
    • 4.1. Binary Search Trees
    • 4.2. Balanced BST
    • 4.2. AVL Trees
    • 4.3. Red-Black Trees
    • 4.4. Quiz
  • 5. Tries
    • 5.1. Concept
    • 5.2. Implementation
    • 5.3. Quiz
    • 5.4. Homework
  • 6. Disjoint Sets
    • 6.1. Concept
    • 6.2. Implementation
    • 6.3. Quiz
  • 7. Graphs
    • 7.1. Implementation
    • 7.2. Cycle Detection
    • 7.3. Topological Sorting
    • 7.4. Quiz
  • 8. Minimum Spanning Trees
    • 8.1. Abstraction
    • 8.2. Prim's Algorithm
    • 8.3. Kruskal’s Algorithm
    • 8.4. Edmonds' Algorithm
    • 8.5. Quiz
    • 8.6. Homework
  • 9. Network Flow
    • 9.1. Flow Network
    • 9.2. Ford-Fulkerson Algorithm
    • 9.3. Simplex Algorithm
    • 9.3. Quiz
  • 10. Dynamic Programming
    • 10.1. Fibonacci Sequence
    • 10.2. Tower of Hanoi
    • 10.3. Longest Common Subsequence
    • 10.4. Quiz
Powered by GitBook

©2023 Emory University - All rights reserved

On this page
  • Auto-Complete
  • Tasks
  • Extra credit
  • Notes

Was this helpful?

Export as PDF
  1. 5. Tries

5.4. Homework

This section explains the homework assignment regarding Autocomplete.

Previous5.3. QuizNext6. Disjoint Sets

Last updated 2 years ago

Was this helpful?

Auto-Complete

Most virtual keyboards provide the option of auto-complete, which gives a list of candidate words that you wish to type given a prefix you enter. For instance, when you type "sh", it gives a list of candidate words such as "she", "shell", "ship", etc.

"sh" -> ["she", "ship", "shell, ...]

If you select a word from the candidates, it should learn your selection as the top candidate for that prefix. For instance, if you select "ship" from the example above, the next time you enter the same prefix "sh", it should give a list of candidates where the first item is "ship" instead of "she".

"sh" -> ["ship", "she", "shell, ...]

Your task is to write a program that gives a list of candidate words for any prefix and learns the selected candidates.

Tasks

  • Create a class called under the package:

    • This class extends the abstract class , which extends .

    • The value type of the generic T can be a collection of strings (e.g., List<String>).

    • You must create a constructor that takes two parameters, dict_file and max, and calls its , which reads all words in the dictionary file (e.g., ).

  • Override the getCandidates() method that takes a prefix and returns a list of candidate words matching the prefix:

    • The max-number of candidates in the list must be the return value of the getMax() method.

    • The most recently selected candidate should appear as the 1st item, the 2nd most recently selected candidate should appear as the 2nd item, and so on.

    • The rest of the candidate list should be filled with the shortest words matching the prefix.

    • If there are more than one candidate with the same lengths, they should be sorted alphabetically.

    • Make sure the same candidate does not appear more than once.

  • Override the pickCandidate() method that takes a prefix and a selected candidate, and saves the information:

    • This is the most recently selected candidate for that particular prefix. It must appear as the first item in the candidate list when the same prefix is entered next time.

  • Submit AutocompleteHW.java to Canvas.

Extra credit

  • Feel free to change the generic type, representing the value type of each TrieNode, to anything other than List<String> (List<something else>).

  • The getCandidates() method:

    • Must show the most frequently picked candidate first, the 2nd-most frequently picked candidate next, and so on.

    • If there are more than one candidate with the same frequency, sort them by recency.

    • The rest of candidates should be filled as in the original task.

  • You must submit both AutocompleteHW and AutocompleteHWExtra to earn the extra credit.

Notes

  • Do not change the dictionary file. If you find anything peculiar about the dictionary file, please let me know so everyone works on the same copy of the dictionary file.

  • Please test your program yourself. We will evaluate your program using our unit tests and measure the performance (both speed and accuracy).

  • If you are having trouble with implementing getCandidates(), think about how to traverse the trie given any node.

  • Your program should be able to handle prefixes or candidates that do not exist in the dictionary.

  • All picked candidates should be introduced as real words to the trie.

  • Whitespaces are not allowed as input; thus, you should trim all input strings.

Create a class called under the package.

Take a look at if you are not familiar with methods in the standard library.

If you are having trouble with implementing pickCandidate(), take a look at .

You are not allowed to use any type of to store candidates for this homework.

AutocompleteHW
autocomplete
Autocomplete
Trie
super constructor
dict.txt
AutocompleteHWExtra
autocomplete
Map
Trie#put
Map