Skip to content

Instantly share code, notes, and snippets.

@slofurno
Last active April 10, 2017 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save slofurno/d45596d236e99424c843e7ee6c93cfaf to your computer and use it in GitHub Desktop.
Save slofurno/d45596d236e99424c843e7ee6c93cfaf to your computer and use it in GitHub Desktop.
defmodule BinaryTree do
defstruct [:left, :right, :value]
def invert(%__module__{left: left, right: right} = root) do
%{root| left: invert(right), right: invert(left)}
end
def invert(nil), do: nil
def test do
three = %BinaryTree{value: 3}
one = %BinaryTree{value: 1}
six = %BinaryTree{value: 6}
nine = %BinaryTree{value: 9}
seven = %BinaryTree{value: 7, left: six, right: nine}
two = %BinaryTree{value: 2, left: one, right: three}
four = %BinaryTree{value: 4, left: two, right: seven}
2 = four.left.value
1 = four.left.left.value
inverted = invert(four)
7 = inverted.left.value
9 = inverted.left.left.value
:ok
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment