This is a question for an interview: Write a program to implement autocomplete for a given set of N strings and positive weights. That is, given a prefix and an integer k, find the top k strings in the set among those that start with the prefix.
And I found a suggested algorithm here with ternary search tree and max heap: http://www.cs.princeton.edu/courses/archive/fall13/cos226/checklist/autocomplete.html
I kinda get the algorithm but I have no idea how to print out the top K words. One biggest confusion I have is: we can traverse the tree and get all the nodes with max weights, but how do we go back and get the words together?
Any help is appreciated.