2 (** {1 Polymorphic binary trees with elements at nodes} *)
6 type tree 'a = Empty | Node (tree 'a) 'a (tree 'a)
8 let predicate is_empty (t:tree 'a)
9 ensures { result <-> t = Empty }
10 = match t with Empty -> true | Node _ _ _ -> false end
14 (** {2 Number of nodes} *)
21 let rec function size (t: tree 'a) : int = match t with
23 | Node l _ r -> 1 + size l + size r
26 lemma size_nonneg: forall t: tree 'a. 0 <= size t
28 lemma size_empty: forall t: tree 'a. 0 = size t <-> t = Empty
32 (** {2 Occurrences in a binary tree} *)
39 function occ (x: 'a) (t: tree 'a) : int = match t with
41 | Node l y r -> (if y = x then 1 else 0) + occ x l + occ x r
45 forall x: 'a, t: tree 'a. 0 <= occ x t
47 predicate mem (x: 'a) (t: tree 'a) =
52 (** {2 Height of a tree} *)
60 let rec function height (t: tree 'a) : int = match t with
62 | Node l _ r -> 1 + max (height l) (height r)
66 forall t: tree 'a. 0 <= height t
70 (** {2 In-order traversal} *)
78 let rec function inorder (t: tree 'a) : list 'a = match t with
80 | Node l x r -> inorder l ++ Cons x (inorder r)
85 (** {2 Pre-order traversal} *)
93 let rec function preorder (t: tree 'a) : list 'a = match t with
95 | Node l x r -> Cons x (preorder l ++ preorder r)
108 lemma inorder_length: forall t: tree 'a. length (inorder t) = size t
112 (** {2 Huet's zipper} *)
120 | Left (zipper 'a) 'a (tree 'a)
121 | Right (tree 'a) 'a (zipper 'a)
123 let rec function zip (t: tree 'a) (z: zipper 'a) : tree 'a =
126 | Left z x r -> zip (Node t x r) z
127 | Right l x z -> zip (Node l x t) z
130 (* navigating in a tree using a zipper *)
132 type pointed 'a = (tree 'a, zipper 'a)
134 let function start (t: tree 'a) : pointed 'a = (t, Top)
136 let function up (p: pointed 'a) : pointed 'a = match p with
137 | _, Top -> p (* do nothing *)
138 | l, Left z x r | r, Right l x z -> (Node l x r, z)
141 let function top (p: pointed 'a) : tree 'a = let t, z = p in zip t z
143 let function down_left (p: pointed 'a) : pointed 'a = match p with
144 | Empty, _ -> p (* do nothing *)
145 | Node l x r, z -> (l, Left z x r)
148 let function down_right (p: pointed 'a) : pointed 'a = match p with
149 | Empty, _ -> p (* do nothing *)
150 | Node l x r, z -> (r, Right l x z)
155 (* monomorphic AVL trees, for the purpose of Coma tests *)
163 val predicate lt elt elt
164 clone relations.TotalStrictOrder with
165 type t = elt, predicate rel = lt, axiom .
167 (* binary trees with height stored in nodes *)
168 type tree = E | N int tree elt tree
170 let function ht (t: tree) : int =
171 match t with E -> 0 | N h _ _ _ -> h end
173 (* smart constructor *)
174 let function node (l: tree) (x: elt) (r: tree) : tree =
175 N (1 + max (ht l) (ht r)) l x r
177 let rec ghost function height (t: tree) : int
178 ensures { result >= 0 }
181 | N _ l _ r -> 1 + max (height l) (height r)
184 predicate wf (t: tree) =
187 | N h l _ r -> h = height t && wf l && wf r
190 predicate mem (y: elt) (t: tree) =
193 | N _ l x r -> mem y l || y=x || mem y r
196 predicate tree_lt (t: tree) (y: elt) =
197 forall x. mem x t -> lt x y
199 predicate lt_tree (y: elt) (t: tree) =
200 forall x. mem x t -> lt y x
202 predicate bst (t: tree) =
205 | N _ l x r -> bst l && tree_lt l x && bst r && lt_tree x r
208 predicate avl (t: tree) =
211 | N _ l _ r -> avl l && avl r && -1 <= height l - height r <= 1