-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathProblem68.hs
46 lines (41 loc) · 1.27 KB
/
Problem68.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
module Problem68 where
import BinaryTree
import Problem67A (stringToTree,tree67)
import qualified Data.Foldable as FD
import Data.Function
import Data.List
treeToPreorder :: Tree a -> [a]
treeToPreorder = FD.toList
treeToInorder :: Tree a -> [a]
treeToInorder Empty = []
treeToInorder (Branch v l r) = treeToInorder l ++ [v] ++ treeToInorder r
preInTree :: Eq a => [a] -> [a] -> Tree a
preInTree seqP seqI
| null seqP && null seqI = Empty
| (a:as) <- seqP
, (b:bs) <- seqI
, (newIL,_:newIR) <- break (== a) seqI
, lenL <- length newIL
, (newPL,newPR) <- splitAt lenL . tail $ seqP
= Branch a (preInTree newPL newIL) (preInTree newPR newIR)
main :: IO ()
main = do
let t = tree67
po = treeToPreorder t
io = treeToInorder t
-- (a) preorder and inorder
putStrLn po
putStrLn io
-- (b) preorder cannot be used in the reverse direction
-- because a particular preorder sequence
-- might correspond to multiple binary trees
let t1 = Branch 'a' (singleton 'b') (singleton 'c')
t2 = Branch 'a' (Branch 'b' Empty (singleton 'c')) Empty
-- different trees
print $ t1 == t2
-- same preorder
print $ ((==) `on` treeToPreorder) t1 t2
-- (c)
let t' = preInTree po io
print t'
print $ t == t'