【发布时间】:2013-10-01 16:06:24
【问题描述】:
我正在寻找一个算法来解决一个问题,我需要在该树结构上找到一个数据节点的最接近匹配项。如果没有完全匹配,则回退到最接近的前缀。
例如,假设我有以下结构,其中单词(单词中的数字)是分支,带方括号的数字是数据(叶节点);我正在寻找一种算法,该算法将返回下表所示的一组结果。请注意路径分隔符为">"
one - [1]
/\
two five
/\ \
eight [12] nine
/ \
[128] [159]
+---------------------------+--------+---------------------------------------------+
| path | result | |
+---------------------------+--------+---------------------------------------------+
| one > five > nine | 159 | whole path matches |
| one > five | 1 | partial (only "one" matched) |
| one > two > eight | 128 | whole path matches |
| one > two | 12 | whole path matches |
| one > two > eight > seven | 128 | partial (only "one > two > eight" matched) |
| one > two > seven | 12 | partial (only "one > two" matched) |
+---------------------------+--------+---------------------------------------------+
我真的很喜欢 C++(STL 或 boost)库;但为此目的只指向一个漂亮的算法也同样好。
【问题讨论】:
标签: c++ algorithm data-structures stl search-tree