Given the following…
(def inTree
'((1 2)
(1 2 3)
(1 2 4 5 9)
(1 2 4 10 15)
(1 2 4 20 25)))
How would you transform it to this trie?
(def outTrie
'(1
(2 ()
(3 ())
(4 (5
(9 ()))
(10
(15 ()))
(20
(25 ()))))))
Here’s a cleaned up solution. This fixes a bug Brian’s add-to-trie method since it’s currently dependent upon you inserting the seqs in increasing-length order. It also allows querying the trie by prefix, which is a common use case.
Note the memory usage here is higher since it stores the values in the leaf nodes of the trie so you can perform searches.