rbtree v0.1.5 Tree

A set of functions for red-black tree.

Red-black tree is a self-balancing binary search tree data structure. Although Elixir has excellent support for hash map, it does not include any sorted or ordered data structures. This package will be the underlying data structure for SortedMap and SortedSet. In addition to usual operations, it also supports ordinal indexing with time complexity O(log(N)) for versatility.

Author: Ricky Hanrickylqhan@gmail.com Modified from Data.Set.RBtree package in Haskell.

Link to this section Summary

Link to this section Functions

Link to this function comparator(term1, term2)
Link to this function delete_max(arg)
Link to this function delete_min(arg)
Link to this function difference(t1, arg2)
Link to this function do_to_string(pref, arg2)

Invoked in order to access the value stored under key in the given term term.

This function should return {:ok, value} where value is the value under key if it succeeded, or :error if the key does not exist in the structure.

Many of the functions defined in the Access module internally call this function. This function is also used when the square-brackets access syntax (structure[key]) is used: the fetch/2 callback implemented by the module that defines the structure struct is invoked and if it returns {:ok, value} then value is returned, or if it returns :error then nil is returned.

See the Map.fetch/2 and Keyword.fetch/2 implementations for examples of how to implement this callback.

Callback implementation for Access.fetch/2.

Link to this function filter_range(arg, min, max, l_inc \\ true, r_inc \\ true)
Link to this function filter_range_by_value(tree, min, max, l_inc \\ true, r_inc \\ true)
Link to this function from_list(l, tree_acc \\ {nil, 0})
Link to this function from_orddict(list, tree_acc \\ {nil, 0})
Link to this function get(tree, key, default)

Invoked in order to access the value stored under key in the given term term, defaulting to default if not present.

This function should return the value under the key key in term if there’s such key, otherwise default.

For most data structures, this can be implemented using fetch/2 internally; for example:

def get(structure, key, default) do
  case fetch(structure, key) do
    {:ok, value} -> value
    :error       -> default
  end
end

See the Map.get/3 and Keyword.get/3 implementations for more examples.

Callback implementation for Access.get/3.

Link to this function get_and_update(tree, key, fun)

Invoked in order to access the value under key and update it at the same time.

The implementation of this callback should invoke the passed function with the value under key key in the passed structure, or nil if the key is not present. This function should return either {value_to_return, new_value} or :pop.

If it returns {value_to_return, new_value}, the return value of this callback should be {value_to_return, new_term} where new_term is term after updating the value of key with new_value.

If it returns :pop, the return value of this callback should be {value, new_term} where value is the value under key or nil if not present, and new_term is term without the key key.

See the implementations of Map.get_and_update/3 or Keyword.get_and_update/3 for more examples.

Callback implementation for Access.get_and_update/3.

Link to this function has_key?(tree, key)
Link to this function insert(arg, k, v)
Link to this function intersection(t1, arg2)
Link to this function join(t1, k, t2)
Link to this function member?(arg, srch_key)

Invoked to “pop” the value under key out of the given term.

When the key key exists in the given term, the implementation should return a {value, new_term} tuple where value is the value that was under key and new_term is term without key.

When the key key is not present in the given term, a tuple {value, term} should be returned, where value is implementation-defined.

See the implementations for Map.pop/3 or Keyword.pop/3 for more examples.

Callback implementation for Access.pop/2.

Link to this function range(tree, a, b)
Link to this function reduce(tree, acc, fun)
Link to this function reduce_nodes(tree, acc, fun)
Link to this function reduce_nodes(order, arg, acc, fun)
Link to this function set(tree, key, value)
Link to this function singleton(key, value \\ nil)
Link to this function split(kx, arg2)
Link to this function to_list(tree, acc \\ [])