Skip to content
This repository has been archived by the owner on Jan 18, 2025. It is now read-only.

Commit

Permalink
problem55
Browse files Browse the repository at this point in the history
  • Loading branch information
ondanaoto committed Aug 5, 2024
1 parent fc1817e commit c87365a
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions Src/Problem55.lean
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,25 @@ def badTree : BinTree Nat :=
(leaf 2)

#eval badTree.isBalanced

universe u

/-- monad instance of `List` -/
instance : Monad List.{u} where
pure := @List.pure
bind := @List.bind
map := @List.map

partial def cbalTree : Nat → List (BinTree Unit)
| 0 => [.Empty]
| n + 1 => do
let q := n / 2
let r := n % 2
let indices := List.range (q+r+1) |>.drop q
let i ← indices
let left ← cbalTree i
let right ← cbalTree (n - i)
pure (BinTree.Node () left right)

#eval (cbalTree 4)
#eval (cbalTree 6)|>.map BinTree.isBalanced

0 comments on commit c87365a

Please sign in to comment.