如果您要处理的是大型数据集,建议您考虑将其实现。我将一小部分Ruby做到了这一点:
require 'rubygems'require 'redis'class RedisTrie TERMINAL = '+' def initialize(prefix) @prefix = prefix @r = Redis.new end def add_word(word) w = word.gsub(/[^a-zA-Z0-9_-]/, '') key = "#{@prefix}:" w.each_char do |c| @r.zset_add key, c.bytes.first, c key += c end @r.zset_add key, 0, TERMINAL end def add_words(*words) words.flatten.compact.each {|word| add_word word} end def suggest(text) @r.zset_range("#{@prefix}:#{text}", 0, -1).map do |c| (c == TERMINAL) ? text : suggest(text + c) end.flatten endendrt = RedisTrie.new('trie')rt.add_words %w( apple automobile carwash oil-change cranky five ruthie axe auto )p rt.suggest(ARGV.shift.to_s)例如:
$ ruby RedisTrie.rb["apple", "auto", "automobile", "axe", "carwash", "cranky", "five", "oil-change", "ruthie"]$ ruby RedisTrie.rb a["apple", "auto", "automobile", "axe"]$ ruby RedisTrie.rb au["auto", "automobile"]$ ruby RedisTrie.rb aux[]
在Wikipedia的Tries条目上阅读有关Tries的更多信息。
您肯定会优化您的建议方法,以不返回所有值,而只返回找到的前X个值。这将使迭代整个数据结构的目的无效。



