0

我正在寻找一种算法来解决一个问题,在该算法中我维护一个树结构,我需要在该结构上找到数据节点的最接近匹配。如果没有完全匹配,则回退到最接近的前缀。

例如,如果说我有以下结构,其中单词(单词中的数字)是分支,带方括号的数字是数据(叶节点);我正在寻找一种算法,该算法将返回下表所示的一组结果。请注意,路径分隔符是">"

           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++(STLboost基于)的库;但是为此目的仅指向一个漂亮的算法也同样好。

4

1 回答 1

1

您正在寻找三元搜索树

http://en.wikipedia.org/wiki/Ternary_search_tree

于 2013-10-01T16:11:16.777 回答