This file contains 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
def gfe(head], _, []), do: {head, []} | |
def gfe([], {:empty, :empty}, new_queue), do: {nil, new_queue} | |
def gfe([], front, new_queue), do: {front, new_queue} | |
def gfe([{head_item, head_prio} = head | []], front, new_queue) do | |
{front_item, front_prio} = front | |
if head_prio > front_prio do | |
{head, new_queue ++ [front]} | |
else | |
{front, new_queue ++ [head]} | |
end |
This file contains 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
defp gfe([]), do: nil | |
defp gfe(queue) do | |
{_, front_elem} = queue | |
|> Enum.group_by(fn {_, prio} -> prio end) | |
|> Map.to_list | |
|> List.last | |
{List.last(front_elem), queue -- [List.last(front_elem)]} | |
end |
This file contains 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
@spec insert_with_priority(list(tuple()), tuple()) :: list(tuple()) | |
def insert_with_priority(queue, {_item, _prio} = elem) do | |
queue ++ [elem] | |
end |
This file contains 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
@spec front(list(tuple())) :: tuple() | nil | |
def front([]), do: nil | |
def front(queue) do | |
{result, _} = gfe queue | |
result | |
end |
This file contains 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
def increase_element_priority([], _, _), do: [] | |
def increase_element_priority(queue, {item, prio} = elem, new_prio) | |
when new_prio >= prio do | |
pos = position_by_order queue, elem | |
first_half = (queue |> Enum.take(pos)) -- [elem] | |
sec_half = queue |> Enum.slice(pos, length(queue)) | |
first_half ++ [{item, new_prio}] ++ sec_half | |
end |
This file contains 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
setup %{conn: conn} = config do | |
case config[:login_as] do | |
:client -> | |
user = insert_user username: config[:username] | |
conn = assign conn, :current_user, user | |
{:ok, conn: conn, user: user} | |
:admin -> | |
admin = insert_admin username: config[:username] | |
conn = assign conn, :current_user, admin |
This file contains 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
def new(value) do | |
%{value: value, left: :leaf, right: :leaf} | |
end | |
@doc """ | |
Creates and inserts a node with its value as 'node_value' into the tree. | |
""" | |
@spec insert(%{} | :leaf, any) :: %{} | |
def insert(:leaf, node_value), do: new node_value |
This file contains 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
@doc """ | |
Removes a node with 'node_value' from the given 'tree'. Returns :leaf if the | |
node does not exist. | |
""" | |
@spec delete_node(%{}, any) :: %{} | nil | |
def delete_node(tree, node_value) do | |
if exists?(tree, node_value) do | |
delete tree, node_value | |
else |
This file contains 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
@doc """ | |
Does a Depth-First Search in the given 'tree'. The nodes' values are | |
returned in a list. The order of the search is passed into 'order' using | |
the atoms ':in_order', ':pre_order' or ':post_order' | |
""" | |
@spec depth_first_search(%{}, atom) :: list(any) | |
def depth_first_search(tree, order) when order == :pre_order or | |
order == :in_order or | |
order == :post_order do |
This file contains 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
@doc """ | |
Performs a Breadth-First Search in the given 'tree'. The nodes' values are | |
returned as a list. | |
""" | |
@spec breadth_first_search(%{}) :: list(any) | |
def breadth_first_search(tree) do | |
bfs(tree) | |
end |
OlderNewer