type 'a starred_level = Root | Starring_Left of 'a starred_nonroot | Starring_Right of 'a starred_nonroot
and 'a starred_nonroot = { parent : 'a starred_level; sibling: 'a tree };;
- type 'a zipper = { tree : 'a starred_level; filler: 'a tree };;
+ type 'a zipper = { level : 'a starred_level; filler: 'a tree };;
let rec move_botleft (z : 'a zipper) : 'a zipper =
(* returns z if the targetted node in z has no children *)
(* else returns move_botleft (zipper which results from moving down and left in z) *)
<!--
- let {tree; filler} = z
+ let {level; filler} = z
in match filler with
| Leaf _ -> z
| Node(left, right) ->
- let zdown = {tree = Starring_Left {parent = tree; sibling = right}; filler = left}
+ let zdown = {level = Starring_Left {parent = level; sibling = right}; filler = left}
in move_botleft zdown
;;
-->
(* else returns move_right_or_up (result of moving up in z) *)
<!--
- let {tree; filler} = z
- in match tree with
- | Starring_Left {parent; sibling = right} -> Some {tree = Starring_Right {parent; sibling = filler}; filler = right}
+ let {level; filler} = z
+ in match level with
+ | Starring_Left {parent; sibling = right} -> Some {level = Starring_Right {parent; sibling = filler}; filler = right}
| Root -> None
| Starring_Right {parent; sibling = left} ->
- let z' = {tree = parent; filler = Node(left, filler)}
+ let z' = {level = parent; filler = Node(left, filler)}
in move_right_or_up z'
;;
-->
The following function takes an 'a tree and returns an 'a zipper focused on its root:
let new_zipper (t : 'a tree) : 'a zipper =
- {tree = Root; filler = t}
+ {level = Root; filler = t}
;;
Finally, we can use a mutable reference cell to define a function that enumerates a tree's fringe until it's exhausted:
let make_fringe_enumerator (t: 'a tree) =
- (* create a zipper targetting the root of t *)
- let zstart = new_zipper t
- in let zbotleft = move_botleft zstart
+ (* create a zipper targetting the botleft of t *)
+ let zbotleft = move_botleft (new_zipper t)
(* create a refcell initially pointing to zbotleft *)
in let zcell = ref (Some zbotleft)
(* construct the next_leaf function *)
in let next_leaf () : 'a option =
match !zcell with
- | None -> (* we've finished enumerating the fringe *)
- None
| Some z -> (
(* extract label of currently-targetted leaf *)
let Leaf current = z.filler
| Some z' -> Some (move_botleft z')
(* return saved label *)
in Some current
+ | None -> (* we've finished enumerating the fringe *)
+ None
)
(* return the next_leaf function *)
in next_leaf