From 84e6753005e57827c78b50ad0a26ffa0d0da55e4 Mon Sep 17 00:00:00 2001 From: Joe Zhao Date: Mon, 6 Apr 2015 13:53:37 +0800 Subject: +59 +60 --- H59.hs | 13 +++++++++++++ H60.hs | 18 ++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 H59.hs create mode 100644 H60.hs diff --git a/H59.hs b/H59.hs new file mode 100644 index 0000000..3d44ac3 --- /dev/null +++ b/H59.hs @@ -0,0 +1,13 @@ +module H59 where + +import Tree + +hbalTree :: Int -> [Tree Char] +hbalTree 0 = [Empty] +hbalTree h = [Branch 'x' x y | let tl = concatMap hbalTree $ filter (>=0) [h-2,h-1],x <- tl, y <- tl] + +hbalTree' 1 = [Branch 'x' Empty Empty] +hbalTree' h = hbalhelper (h-1) [Branch 'x' Empty Empty] [Empty] + where + hbalhelper 0 x _ = x + hbalhelper n x y = hbalhelper (n-1) [Branch 'x' l r| let tl = x ++ y, l <- tl, r <- tl] x diff --git a/H60.hs b/H60.hs new file mode 100644 index 0000000..6f3f1b0 --- /dev/null +++ b/H60.hs @@ -0,0 +1,18 @@ +import Tree +import H59 + +import Data.Maybe +import Data.List + +hbalTreeNodes :: Int -> [Tree Char] +hbalTreeNodes 0 = [Empty] +hbalTreeNodes n = concatMap (filter ((==n).countNodes).hbalTree') [minH..maxH] + where + -- Shenle + minNodesSeq = 0:1:zipWith ((+).(1+)) minNodesSeq (tail minNodesSeq) + + minH = ceiling $ logBase 2 $ fromIntegral (n+1) + maxH = (fromJust $ findIndex (>n) minNodesSeq) - 1 + countNodes Empty = 0 + countNodes (Branch _ t1 t2) = 1 + (countNodes t1) + (countNodes t2) + -- cgit v1.2.3-70-g09d2