Last active
April 10, 2017 14:15
-
-
Save slofurno/d45596d236e99424c843e7ee6c93cfaf to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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