This repository has been archived by the owner on Apr 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
2_1_2_fold_tree_traversal.hs
110 lines (98 loc) · 3.73 KB
/
2_1_2_fold_tree_traversal.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
{-
GHCi> tree = Branch (Branch Nil 1 (Branch Nil 2 Nil)) 3 (Branch Nil 4 Nil)
GHCi> foldr (:) [] tree
[1,2,3,4]
GHCi> foldr (:) [] $ PreO tree
[3,1,2,4]
GHCi> foldr (:) [] $ PostO tree
[2,1,4,3]
GHCi> foldr (:) [] $ LevelO tree
[3,1,4,2]
-}
data Tree a = Nil | Branch (Tree a) a (Tree a) deriving (Eq, Show)
newtype Preorder a = PreO (Tree a) deriving (Eq, Show)
newtype Postorder a = PostO (Tree a) deriving (Eq, Show)
newtype Levelorder a = LevelO (Tree a) deriving (Eq, Show)
{-
3
/ \
1 4
\
2
-}
tree = Branch (Branch Nil 1 (Branch Nil 2 Nil)) 3 (Branch Nil 4 Nil)
-- correct (in-order)
{-
f = (:)
ini = []
in-order:
foldr (:) [] tree = foldr (:) ((:) 3 (foldr (:) [] (Branch Nil 4 Nil))) (Branch Nil 1 (Branch Nil 2 Nil)) =
foldr (:) ((:) 3 (foldr (:) ((:) 4 (foldr (:) [] Nil)) Nil)) (Branch Nil 1 (Branch Nil 2 Nil)) =
foldr (:) ((:) 3 (foldr (:) ((:) 4 []) Nil)) (Branch Nil 1 (Branch Nil 2 Nil)) =
foldr (:) ((:) 3 [4]) (Branch Nil 1 (Branch Nil 2 Nil)) =
foldr (:) [3,4] (Branch Nil 1 (Branch Nil 2 Nil)) =
foldr (:) ((:) 1 (foldr (:) [3,4] (Branch Nil 2 Nil))) Nil =
foldr (:) ((:) 1 (foldr (:) ((:) 2 (foldr (:) [3,4] Nil)) Nil)) Nil =
foldr (:) ((:) 1 (foldr (:) ((:) 2 [3,4]) Nil)) Nil =
foldr (:) ((:) 1 [2, 3, 4]) Nil =
foldr (:) [1,2,3,4] Nil =
[1, 2, 3, 4]
-}
instance Foldable Tree where
foldr f ini Nil = ini
foldr f ini (Branch l v r) = foldr f (f v (foldr f ini r)) l
{-
f = (:)
ini = []
pre-order:
foldr (:) [] tree = f v (foldr f (foldr f ini r) l) =
(:) 3 (foldr (:) (foldr (:) [] (Branch Nil 4 Nil)) (Branch Nil 1 (Branch Nil 2 Nil))) =
(:) 3 (foldr (:) ((:) 4 (foldr (:) (foldr (:) [] Nil) Nil)) (Branch Nil 1 (Branch Nil 2 Nil))) =
(:) 3 (foldr (:) [4] (Branch Nil 1 (Branch Nil 2 Nil))) =
(:) 3 ((:) 1 (foldr (:) (foldr (:) [4] (Branch Nil 2 Nil)) Nil)) =
(:) 3 ((:) 1 (foldr (:) ((:) 2 (foldr (:) (foldr (:) [4] Nil) Nil)) Nil)) =
(:) 3 ((:) 1 (foldr (:) [2,4] Nil)) =
(:) 3 ((:) 1 [2,4]) =
(:) 3 [1,2,4] =
[3,1,2,4]
-}
instance Foldable Preorder where
foldr f ini (PreO Nil) = ini
foldr f ini (PreO (Branch l v r)) = f v (foldr f (foldr f ini (PreO r)) (PreO l))
{-
f = (:)
ini = []
post-order:
foldr (:) [] (Branch (Branch Nil 1 (Branch Nil 2 Nil)) 3 (Branch Nil 4 Nil)) =
foldr (:) (foldr (:) ((:) 3 []) (Branch Nil 4 Nil)) (Branch Nil 1 (Branch Nil 2 Nil)) =
foldr (:) (foldr (:) [3] (Branch Nil 4 Nil)) (Branch Nil 1 (Branch Nil 2 Nil)) =
foldr (:) (foldr (:) (foldr (:) ((:) 4 [3]) Nil) Nil) (Branch Nil 1 (Branch Nil 2 Nil)) =
foldr (:) [4,3] (Branch Nil 1 (Branch Nil 2 Nil)) =
foldr (:) (foldr (:) ((:) 1 [4,3]) (Branch Nil 2 Nil)) Nil =
foldr (:) [1,4,3] (Branch Nil 2 Nil) =
foldr (:) (foldr (:) ((:) 2 [1,4,3] Nil)) Nil =
[2,1,4,3]
-}
instance Foldable Postorder where
foldr f ini (PostO Nil) = ini
foldr f ini (PostO (Branch l v r)) = foldr f (foldr f (f v ini) (PostO r)) (PostO l)
{-
level-order:
foldr (:) [] (Branch (Branch Nil 1 (Branch Nil 2 Nil)) 3 (Branch Nil 4 Nil)) =
helper [(Branch (Branch Nil 1 (Branch Nil 2 Nil)) 3 (Branch Nil 4 Nil))] =
(:) 3 (helper [(Branch Nil 1 (Branch Nil 2 Nil)), (Branch Nil 4 Nil)]) =
(:) 3 ((:) 1 (helper [(Branch Nil 4 Nil), (Branch Nil 2 Nil)])) =
(:) 3 ((:) 1 ((:) 4 helper [(Branch Nil 2 Nil)])) =
(:) 3 ((:1) ((:) 4 ((:) 2 []))) =
[3,1,4,2]
-}
instance Foldable Levelorder where
foldr f ini (LevelO Nil) = ini
foldr f ini (LevelO tree) = helper [tree] where
helper [] = ini
helper (Nil:xs) = helper xs
helper ((Branch ln v rn):xs) = f v (helper $ xs ++ [ln, rn])
ex1 = foldr (:) [] tree
ex2 = foldr (:) [] $ PreO tree
ex3 = foldr (:) [] $ PostO tree
ex4 = foldr (:) [] $ LevelO tree