trie - (match c with Some(v) -> v | None...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
module M = Map.Make(struct type t = char let compare = compare end) m exception NotFound e type 'a trie = Node of ('a option * (('a trie) M.t) * int) t type 'a cursor = char list * 'a trie t let empty: 'a trie = Node(None, M.empty, 0) l let put (t: 'a trie) (l: char list) (v: 'a) : 'a trie = let rec helper t l = let Node(c, m, sz) = t in let bind = if (c = None) then 0 else 1 in match l with [] -> (Node(Some(v), m, sz + 1 - bind), 1 - bind) | h::t -> if (M.mem h m) then let child = M.find h m in let (newChild, up) = helper child t in (Node(c, M.add h newChild m, sz+up), up) else let (newChild, up) = helper empty t in (Node(c, M.add h newChild m, sz+up), up) in let (rv, _) = helper t l in rv let rec get (t: 'a trie) (l: char list) : 'a = let Node(c, m, sz) = t in match l with [] ->
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: (match c with Some(v) -> v | None -> raise NotFound) | h::t -> if (M.mem h m) then let child = M.find h m in get child t else raise NotFound let size (t: 'a trie) : int = let Node(_, _, sz) = t in sz let cursor (d: 'a trie) (l: char list) : 'a cursor = let rec helper d l r = match l with -> (d, r) | h::t -> let Node(c, m, sz) = d in if (M.mem h m) then let child = M.find h m in helper child t (h::r) else raise NotFound in let (d, r) = helper d l in (r, d) let advance (c: 'a cursor) (n: char) : 'a cursor option = let (p, d) = c in let Node(c, m, sz) = d in if (M.mem n m) then let child = M.find n m in Some((n::p), child) else None let getc (c: 'a cursor) : 'a option = let (p, d) = c in let Node(c, m, sz) = d in c...
View Full Document

Page1 / 2

trie - (match c with Some(v) -> v | None...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online