RedBlackTree v1.0.0 RedBlackTree View Source
Module for creating and managing red-black trees. Tree nodes have keys (a number that defines the relation between nodes) and data (anything you want).
A red-black tree is a approximately balanced binary tree that satisfies the following red-black properties:
- Every node is either red or black.
- The root is black. * We relax this rule as per to make deletion simpler
- Every leaf (NIL) is black.
- If a node is red, then both its children are black.
For each node, all simple paths from the node to descendant leaves contain the same number of black nodes.
Using the implementantion of insert from the article "Red-black trees in a functional setting" by Chris Okasaki Using the delete implementation from the article "FUNCTIONAL PEARL Deletion: The curse of the red-black tree" by Kimball Germane and Matthew Might
Link to this section Summary
Functions
Creates a tree node
Creates a tree node
deletes a node that has the given key
in a tree_root_node
tree
Inserts a node in a tree
Inserts a given new_node
in a tree_root_node
tree
Searches for a node that has the given key
in a tree_root_node
tree
Link to this section Functions
create() View Source
Creates a tree node.
Returns ${key: nil, left: nil, right: nil, data: nil, color: nil}
.
Examples
iex> RedBlackTree.create()
${key: nil, left: nil, right: nil, data: nil, color: nil}
create(key, data) View Source
Creates a tree node.
Returns ${key: nil, left: nil, right: nil, data: nil, color: nil}
.
Examples
iex> RedBlackTree.create(1, ${x: "my data"})
${key: 1, left: nil, right: nil, data: ${x: "my data"}), color: nil}
delete(tree_root_node, delete_key) View Source
deletes a node that has the given key
in a tree_root_node
tree.
Returns ${key: Number, left: %RBNode{}, right: %RBNode{}, data: :data, color: :red | :black | :blackblack }
.
Examples
iex> RedBlackTree.delete(%RBNode{}, %RBNode{})
%RBNode{}
insert(new_node) View Source
Inserts a node in a tree.
Returns ${key: Number, left: %RBNode{}, right: %RBNode{}, data: :data, color: :red | :black | :blackblack }
.
Examples
iex> RedBlackTree.create(%RBNode{})
%RBNode{}
insert(tree_root_node, new_node) View Source
Inserts a given new_node
in a tree_root_node
tree.
Returns ${key: Number, left: %RBNode{}, right: %RBNode{}, data: :data, color: :red | :black | :blackblack }
.
Examples
iex> RedBlackTree.create(%RBNode{}, %RBNode{})
%RBNode{}
search(tree_root_node, key) View Source
Searches for a node that has the given key
in a tree_root_node
tree.
Returns ${key: Number, left: %RBNode{}, right: %RBNode{}, data: :data, color: :red | :black | :blackblack }
.
Examples
iex> RedBlackTree.search(%RBNode{}, 5)
%RBNode{}