Skip to content

Instantly share code, notes, and snippets.

@kunigami
Last active December 14, 2017 17:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kunigami/9e6135a124f48655815636033b6fdbaa to your computer and use it in GitHub Desktop.
Save kunigami/9e6135a124f48655815636033b6fdbaa to your computer and use it in GitHub Desktop.
let rec insert: 'a. key -> 'a -> 'a trie -> 'a trie =
fun tree value trie -> match (tree, trie) with
| (Empty, Trie (_, children)) -> Trie (Some value, children)
| (Node (root, left, right), Trie (trieValue, children)) ->
let trieForRoot = try
M.find root children
with
Not_found -> empty
in
let trieForLeft = try
find left trieForRoot
with
Not_found -> empty
in
let newTrieForLeft = insert right value trieForLeft in
let newTrieForRoot = insert left newTrieForLeft trieForRoot in
let newChildren = M.add root newTrieForRoot children in
Trie (trieValue, newChildren)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment