Last active
December 14, 2017 17:54
-
-
Save kunigami/9e6135a124f48655815636033b6fdbaa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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