表示无冗余树路径的数据结构

Data Structure for Representing Paths of a Tree Without Redundancy

考虑 Clojure 代码中的以下树结构:

(def tree [7 9 [7 5 3 [4 6 9] 9 3] 1 [2 7 9 9]])

例如,树中所有偶数的路径为:

[[2 3 0] [2 3 1] [4 0]]

这是一个列表列表。每个 'inner' 列表表示从树的根到感兴趣的叶的绝对路径。

我现在正在寻找一种数据结构来表示这种没有冗余的结果。如您所见,例如 [2 3] 的片段在两个条目中重复出现。我想出了一个嵌套的哈希映射,但也许有更简单的东西:

{2 {3 {0 true 1 true}
 4 {0 true}}

我想你可以使用 "deterministic acyclic finite state automaton (DAFSA) also called a directed acyclic word graph (DAWG)"

在您的数据中,所有路径都由一组字符串(或单词)组成。每条通往叶子的路径都代表一条通往偶数的路径。

我认为 DAWG 对于您的问题来说太过分了。您的路径后缀几乎不会被共享。所以 trie should be enough (this is actually your nested hash map approach). Also it's pretty easy to generate it in clojure.

的用法