Skip to content

Instantly share code, notes, and snippets.

@nimish-mehta
Created May 25, 2016 10:37
Show Gist options
  • Save nimish-mehta/8826488dbcf6afb548ac36b7f8cabb87 to your computer and use it in GitHub Desktop.
Save nimish-mehta/8826488dbcf6afb548ac36b7f8cabb87 to your computer and use it in GitHub Desktop.
Red black tree in elixir
defmodule RedBlackTree do
# elixir translation of https://gist.github.com/mjn/2648040
# stucture of node: {Key, Value, Color, Smaller, Bigger}
# tree = nil
# tree = RedBlackTree.insert(tree, 2, 3)
# IO.inspect tree
# tree = RedBlackTree.insert(tree, 3, 4)
# IO.inspect tree
# tree = RedBlackTree.insert(tree, 6, 9)
# IO.inspect tree
# tree = RedBlackTree.insert(tree, 3, 11)
# IO.inspect tree
# tree = RedBlackTree.insert(tree, 8, 12)
# IO.inspect tree
# tree = RedBlackTree.insert(tree, 32, 50)
# IO.inspect "====done===="
# IO.inspect tree
# IO.inspect RedBlackTree.find(tree, 4)
# IO.inspect RedBlackTree.find(tree, 3)
# tree = RedBlackTree.insert(tree, 3, 52)
# IO.inspect RedBlackTree.find(tree, 3)
# IO.inspect RedBlackTree.find(tree, 32)
# IO.inspect tree
def find(nil, _) do
{:not_found}
end
def find({key, value, _, _, _}, key) do
{:found, {key, value}}
end
def find({not_key, _, _, left, _}, key) when key < not_key do
find(left, key)
end
def find({not_key, _, _, _, right}, key) when key > not_key do
find(right, key)
end
def insert(tree, key, value) do
make_black(do_insert(tree, key, value))
end
defp do_insert(nil, key, value) do
{key, value, :red, nil, nil}
end
defp do_insert({key, _, color, left, right}, key, value) do
{key, value, color, left, right}
end
defp do_insert({key_compared, value_compared, color_compared, left, right}, key, value) when key < key_compared do
balance({key_compared, value_compared, color_compared, do_insert(left, key, value), right})
end
defp do_insert({key_compared, value_compared, color_compared, left, right}, key, value) when key > key_compared do
balance({key_compared, value_compared, color_compared, left, do_insert(right, key, value)})
end
defp make_black({key, value, _, left, right}) do
{key, value, :black, left, right}
end
defp balance({key_parent, value_parent, :black, left_parent,
{key_right_child, value_right_child, :red, left_right_child,
{key_right_child_right_child, value_right_child_right_child, :red, left_right_child_right_child, right_right_child_right_child}}}) do
{key_right_child, value_right_child, :red,
{key_parent, value_parent, :black, left_parent, left_right_child},
{key_right_child_right_child, value_right_child_right_child, :black, left_right_child_right_child, right_right_child_right_child}}
end
defp balance({key_parent, value_parent, :black, left_parent,
{key_right_child, value_right_child, :red,
{key_right_child_left_child, value_right_child_left_child, :red, left_right_child_left_child, right_right_child_left_child},
right_child_right_child}}) do
{key_right_child_left_child, value_right_child_left_child, :red,
{key_parent, value_parent, :black, left_parent, left_right_child_left_child},
{key_right_child, value_right_child, :black, right_right_child_left_child, right_child_right_child}}
end
defp balance({key_parent, value_parent, :black,
{key_left_child, value_left_child, :red,
{key_left_child_left_child, value_left_child_left_child, :red, left_left_child_left_child, right_left_child_left_child},
right_left_child},
right_parent}) do
{key_left_child, value_left_child, :red,
{key_left_child_left_child, value_left_child_left_child, :black, left_left_child_left_child, right_left_child_left_child},
{key_parent, value_parent, :black, right_left_child, right_parent}};
end
defp balance({key_parent, value_parent, :black,
{key_left_child, value_left_child, :red, left_left_child,
{key_left_child_right_child, value_left_child_right_child, :red, left_left_child_right_child, right_left_child_right_child}},
right_parent}) do
{key_left_child_right_child, value_left_child_right_child, :red,
{key_left_child, value_left_child, :black, left_left_child, left_left_child_right_child},
{key_parent, value_parent, :black, right_left_child_right_child, right_parent}};
end
defp balance(tree), do: tree
end
@0b01
Copy link

0b01 commented May 23, 2017

Delete?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment