1-
21import Fad.Chapter1
32
43namespace Chapter4
@@ -227,63 +226,64 @@ def mkTree [LT α] [DecidableRel (α := α) (· < ·)]
227226 List.length_filter_le,
228227 Nat.lt_add_one_of_le]
229228
230-
231229end BST1
232230
233231
234232namespace BST2
235233
236- variable {α : Type }
234+ variable {a : Type } [LT a] [DecidableRel (α := a) (· < ·)]
237235
238- inductive Tree (α : Type ) : Type
239- | null : Tree α
240- | node : Nat → (Tree α ) → α → (Tree α ) → Tree α
241- deriving Nonempty
236+ inductive Tree (a : Type ) : Type
237+ | null : Tree a
238+ | node : Nat → (Tree a ) → a → (Tree a ) → Tree a
239+ deriving Nonempty, Inhabited
242240
243241open Std.Format in
244242
245- def Tree.toFormat [ToString α ] : (t : Tree α ) → Std.Format
243+ def Tree.toFormat [ToString a ] : (t : Tree a ) → Std.Format
246244| .null => Std.Format.text "."
247245| .node _ t₁ x t₂ =>
248246 bracket "(" (f!"{x}" ++
249247 line ++ nest 2 t₁.toFormat ++ line ++ nest 2 t₂.toFormat) ")"
250248
251- instance [ToString α ] : Repr (Tree α ) where
249+ instance [ToString a ] : Repr (Tree a ) where
252250 reprPrec e _ := e.toFormat
253251
254- def Tree.height : Tree α → Nat
252+ def Tree.height : Tree a → Nat
255253 | .null => 0
256254 | .node x _ _ _ => x
257255
258- def Tree.flatten : Tree α → List α
256+ def Tree.flatten : Tree a → List a
259257| null => []
260258| node _ l x r => l.flatten ++ [x] ++ r.flatten
261259
262- def node (l : Tree α ) (x : α ) (r : Tree α ): Tree α :=
260+ def node (l : Tree a ) (x : a ) (r : Tree a ): Tree a :=
263261 Tree.node h l x r
264262 where h := 1 + (max l.height r.height)
265263
266- def bias : Tree α → Int
264+ def bias : Tree a → Int
267265 | .null => 0
268266 | .node _ l _ r => l.height - r.height
269267
270- def rotr : Tree α → Tree α
268+ def rotr : Tree a → Tree a
271269| .null => .null
272270| .node _ (.node _ ll y rl) x r => node ll y (node rl x r)
273271| .node _ .null _ _ => .null
274272
275- def rotl : Tree α → Tree α
273+ def rotl : Tree a → Tree a
276274| .null => .null
277275| .node _ ll y (.node _ lrl z rrl) => node (node ll y lrl) z rrl
278276| .node _ _ _ .null => .null
279277
280- def balance (t1 : Tree α ) (x : α ) (t2 : Tree α ) : Tree α :=
278+ def balance (t1 : Tree a ) (x : a ) (t2 : Tree a ) : Tree a :=
281279 if Int.natAbs (h1 - h2) ≤ 1 then
282280 node t1 x t2
283- else if h1 == h2 + 2 then
281+ else if h1 = h2 + 2 then
284282 rotateR t1 x t2
285- else
283+ else if h2 = h1 + 2 then
286284 rotateL t1 x t2
285+ else
286+ panic! "balance: impossible case"
287287 where
288288 h1 := t1.height
289289 h2 := t2.height
@@ -297,33 +297,45 @@ def balance (t1 : Tree α) (x : α) (t2 : Tree α) : Tree α :=
297297 else rotl (node t1 x (rotr t2))
298298
299299
300- def insert {α : Type } [LT α] [DecidableRel (α := α) (· < ·)]
301- : (x : α) -> Tree α -> Tree α
300+ def insert : (x : a) -> Tree a -> Tree a
302301 | x, .null => node .null x .null
303302 | x, .node h l y r =>
304303 if x < y then balance (insert x l) y r else
305304 if x > y then balance l y (insert x r) else .node h l y r
306305
307306
308- def mkTree [LT α] [DecidableRel (α := α) (· < ·)] : ( xs : List α ) → Tree α :=
309- Chapter1 .foldr insert (.null : Tree α )
307+ def mkTree : ( xs : List a ) → Tree a :=
308+ List .foldr insert (.null : Tree a )
310309
310+ -- #eval balance (mkTree [ 1,2,3,4 ] ) 4 (mkTree [])
311311
312- def balanceR (t₁ : Tree α ) (x : α ) (t₂ : Tree α ) : Tree α :=
312+ def balanceR (t₁ : Tree a ) (x : a ) (t₂ : Tree a ) : Tree a :=
313313 match t₁ with
314314 | Tree.null => Tree.null
315315 | Tree.node _ l y r =>
316316 if r.height ≥ t₂.height + 2
317317 then balance l y (balanceR r x t₂)
318318 else balance l y (node r x t₂)
319319
320+ -- def balanceL ...
321+ -- def gbalance ...
322+
323+ def mktree : List a → Tree a :=
324+ List.foldr insert Tree.null
325+
326+ def sort : List a → List a :=
327+ Tree.flatten ∘ mkTree
328+
329+ #eval sort [3 , 2 , 1 , 4 , 5 , 5 ] -- bug with duplicated elements!
330+
320331end BST2
321332
322333namespace DSet
323334open BST2 (insert node)
324335
325336abbrev Set a := BST2.Tree a
326337
338+
327339def member {a : Type } [LT a] [DecidableRel (α := a) (· < ·)] (x : a) : Set a → Bool
328340| .null => false
329341| .node _ l y r =>
0 commit comments