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:

  1. Every node is either red or black.
  2. The root is black. * We relax this rule as per to make deletion simpler
  3. Every leaf (NIL) is black.
  4. If a node is red, then both its children are black.
  5. 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

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}

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}
Link to this function

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{}

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{}
Link to this function

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{}
Link to this function

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{}