Skip to content

Commit 6070c5f

Browse files
committed
Add functions for comparing trees.
One approach to crossover for trees is to swap subtrees at a shared point in the two trees. These functions will help with that.
1 parent b0395ca commit 6070c5f

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

package.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ dependencies:
2323
- base >= 4.11 && < 5
2424
- random
2525
- QuickCheck
26+
- containers
2627

2728
library:
2829
source-dirs: src
@@ -38,6 +39,7 @@ executables:
3839
dependencies:
3940
- hs
4041
- random
42+
- containers
4143

4244
tests:
4345
hs-test:

src/Tree.hs

+8
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,11 @@ maxDepth :: Int -> Tree -> Int
6464
maxDepth d (Branch o a b) = if m > n then m else n where m = maxDepth (d + 1) a
6565
n = maxDepth (d + 1) b
6666
maxDepth d (Leaf a) = d
67+
68+
labelTree :: Tree -> Int -> Set Int -> Set Int
69+
labelTree (Leaf a) n xs = (singleton n) `union` xs
70+
labelTree (Branch o a b) n xs = (singleton n) `union` (labelTree a (2*n + 1) xs) `union` (labelTree b (2*n + 2) xs)
71+
72+
intersectionOfTreeLabels :: Tree -> Tree -> Set Int
73+
intersectionOfTreeLabels t1 t2 = (labelTree t2 0 empty) `intersection` (labelTree t1 0 empty)
74+

0 commit comments

Comments
 (0)