Joël Quenneville
September 01, 2021
35

# Inverting a Binary Tree in One Line of Elm

Joël Quenneville talks about Inverting a binary tree with 1 line of Elm at the Elm Online Meetup on September 1st 2021.

## Joël Quenneville

September 01, 2021

## Transcript

1. ### Folding Inverting a binary tree with a single line of

Elm Joël Quenneville @joelquen

6. ### fold (\v l r -> Node v r l) Empty

Joël Quenneville @joelquen

10. ### Deﬁning a binary tree type Tree a = Empty |

Node a (Tree a) (Tree a) Joël Quenneville @joelquen

12. ### myTree : Tree Int myTree = Node 1 Empty Empty

Joël Quenneville @joelquen
13. ### myTree : Tree Int myTree = Node 1 (Node 2

Empty Empty) (Node 3 Empty Empty) Joël Quenneville @joelquen
14. ### myTree : Tree Int myTree = Node 1 (Node 2

(Node 4 Empty Empty) (Node 5 Empty Empty) ) (Node 3 Empty (Node 6 Empty Empty) ) Joël Quenneville @joelquen

20. ### → Left children are red → Right children are green

Joël Quenneville @joelquen

25. ### What is fold? fold (\v l r -> Node v

r l) Empty yourTree Joël Quenneville @joelquen
26. ### fold : (a -> b -> b -> b) ->

b -> Tree a -> b Joël Quenneville @joelquen
27. ### fold : (a -> b -> b -> b) --

??? -> b -- ??? -> Tree a -- tree being folded -> b -- return value Joël Quenneville @joelquen

36. ### fold : (a -> b -> b -> b) --

aggregation -> b -- start accumulator -> Tree a -> b Joël Quenneville @joelquen
37. ### Aggregating function add3 : Int -> Int -> Int ->

Int add3 currentVal leftAcc rightAcc = currentVal + leftAcc + rightAcc Joël Quenneville @joelquen
38. ### Summing a tree fold add3 -- aggregation 0 -- start

accumulator tree Joël Quenneville @joelquen
39. ### Further discussion → What other common operations can be described

as aggregations of a collection? Joël Quenneville @joelquen

42. ### type Tree a = Node a (Tree a) (Tree a)

| Empty Joël Quenneville @joelquen
43. ### Signatures type Tree a = Node a (Tree a) (Tree

a) -- a -> Tree a -> Tree a -> Tree a | Empty -- Tree a Joël Quenneville @joelquen
44. ### Replace Tree a with b type Tree a = Node

a (Tree a) (Tree a) -- a -> b -> b -> b | Empty -- b Joël Quenneville @joelquen
45. ### fold : (a -> b -> b -> b) ->

b -> Tree a -> b Joël Quenneville @joelquen

47. ### fold : (a -> b -> b -> b) --

Replace Node -> b -- Replace Empty -> Tree a -> b Joël Quenneville @joelquen
48. ### add3 : Int -> Int -> Int -> Int add3

a b c = a + b + c Joël Quenneville @joelquen
49. ### Summing a tree Tree.fold add3 -- replace Node with add3

0 -- replace Empty with 0 someTree Joël Quenneville @joelquen
50. ### Node 1 (Node 2 (Node 4 Empty Empty) Empty )

(Node 3 Empty Empty) Joël Quenneville @joelquen

(add3 3 Empty Empty) Joël Quenneville @joelquen

(add3 3 0 0) Joël Quenneville @joelquen

0 ) (add3 3 0 0) Joël Quenneville @joelquen
54. ### add3 1 (add3 2 -- | 4 -- | 6

0 -- | ) (add3 3 0 0) Joël Quenneville @joelquen
55. ### add3 1 6 (add3 3 0 0) -- 3 Joël

Quenneville @joelquen

@joelquen

58. ### fold Node -- replace Node with Node Empty -- replace

Empty with Empty someTree Joël Quenneville @joelquen
59. ### Lesson: We can use fold to disassemble and rebuild a

tree Joël Quenneville @joelquen
60. ### node : a -> Tree a -> Tree a ->

Tree a node value left right = Node value left right Joël Quenneville @joelquen
61. ### inverseNode : a -> Tree a -> Tree a ->

Tree a inverseNode value left right = Node value right left Joël Quenneville @joelquen
62. ### Tree.fold inverseNode -- replace Node with inverseNode Empty -- replace

Empty with Empty someTree Joël Quenneville @joelquen
63. ### Node 1 (Node 2 (Node 4 Empty Empty) Empty )

(Node 3 Empty Empty) Joël Quenneville @joelquen
64. ### inverseNode 1 (inverseNode 2 (inverseNode 4 Empty Empty) Empty )

(inverseNode 3 Empty Empty) Joël Quenneville @joelquen
65. ### inverseNode 1 (inverseNode 2 (inverseNode 4 Empty Empty) -- swap

two empties Empty ) (inverseNode 3 Empty Empty) -- swap two empties Joël Quenneville @joelquen
66. ### inverseNode 1 (inverseNode 2 (Node 4 Empty Empty) -- swap

this Empty -- with this ) (Node 3 Empty Empty) Joël Quenneville @joelquen
67. ### inverseNode 1 (Node 2 Empty (Node 4 Empty Empty) )

(Node 3 Empty Empty) Joël Quenneville @joelquen
68. ### inverseNode 1 (Node 2 -- | Empty -- | swap

this (Node 4 Empty Empty) -- | ) (Node 3 Empty Empty) -- with this Joël Quenneville @joelquen
69. ### Node 1 (Node 3 Empty Empty) (Node 2 Empty (Node

4 Empty Empty) ) Joël Quenneville @joelquen

71. ### Further discussion → How can this technique be applied to

other data structures? Joël Quenneville @joelquen

73. ### Pseudocode IF EMPTY your_base_case ELSE RECURSE LEFT RECURSE RIGHT your_action

Joël Quenneville @joelquen
74. ### Abstraction over recursion fold : (a -> b -> b

-> b) -- action to do recursively -> b -- base case -> Tree a -- tree -> b -- return Joël Quenneville @joelquen
75. ### fold inverseNode -- recursively inverse node Empty -- base case

tree Joël Quenneville @joelquen
76. ### fold : (a -> b -> b -> b) ->

b -> Tree a -> b fold fn baseCase tree = case tree of Node val left right -> fn val (fold fn baseCase left) (fold fn baseCase right) Empty -> baseCase Joël Quenneville @joelquen

78. ### Further discussion → What other abstractions might built on top

of recursion? Joël Quenneville @joelquen

80. ### 3 Perspectives on those args fold : (a -> b

-> b -> b) -- ??? -> b -- ??? -> Tree a -> b Joël Quenneville @joelquen

@joelquen

84. ### Invert a binary tree in 1 line of Elm fold

(\v l r -> Node v r l) Empty tree Joël Quenneville @joelquen