In the previous ternary search tree and its implementation, you can use ternary search tree to implement the searching box only hints, because the ternary search tree prefix matching efficiency is very high, The general idea is as follows (many of which can be modified according to their own needs, I just write my approach):
For example, when searching for songs, smart tips:
Build ternary Search Tree
- Put the string of all song names in a map, key for the song name, value to store the song information, can be a class object domain, where you can follow the key value of the same song to accumulate the number of plays, and the song name to pinyin, using the pinyin4j
- Convert map to Song object list store
- A new field is added to the node class, and index is used to hold an object's subscript value in the list (this is the key part of the ternary search tree, which is to get the corresponding index after all the song names that are prefixed with a are queried. Get (index) directly in list to get the appropriate information for that song
- Insert the song name Pinyin and the song's index in the list into the ternary search tree by one end, for the reason reference--------------------------, in order for the ternary Search tree to balance
Inquire
When querying, such as user input A, all song names prefixed with a are searched.
- Go to ternary search tree to query the name of a song prefixed with a, and deposit Map,key as the song name Pinyin, value is index
- Get information about the song according to index
- Put the number of songs played as value,index as key into TreeMap to sort, rewrite comparator descending order
- Take N records from TreeMap, get corresponding resultlist, get relevant song information according to index
- Return
To summarize, ternary search tree is used to query all songs that match the prefix
After all the matching songs are queried, there is a series of processing based on the sort of a field and so on.
Ternary search tree query + topk sort
Extended
Highlight function
First letter matching, that is, to build a tree, query after the order of the number of playback to heavy
Serialization of the trie to avoid rebuilding the trie every time the restart causes the service to be unavailable
......
Ternary search Tree App--smart tips for searching boxes