Created
September 7, 2022 17:28
-
-
Save rugyoga/632850c5fcb5ae628e80b619c3a9680e to your computer and use it in GitHub Desktop.
Tree example
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 Tree do | |
alias __MODULE__ | |
use Audit | |
defstruct left: nil, item: nil, right: nil, __audit_trail__: nil | |
def add(nil, item), do: %Tree{item: item} | |
def add(tree = %Tree{left: l, item: i, right: r}, item) do | |
cond do | |
item < i -> %Tree{audit(tree) | left: add(l, item)} | |
item > i -> %Tree{audit(tree) | right: add(r, item)} | |
true -> tree | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment