Report a bug If you spot a problem with this page, click here to create a Bugzilla issue. Improve this page Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using local clone.

std.container.rbtree

This module implements a red-black tree container.
This module is a submodule of std.container.

Source: std/container/rbtree.d

License:
Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at boost.org/LICENSE_1_0.txt).
Authors:
Steven Schveighoffer, Andrei Alexandrescu
class RedBlackTree(T, alias less = "a < b", bool allowDuplicates = false) if (is(typeof(binaryFun!less(T.init, T.init))));
Implementation of a red-black tree container.
All inserts, removes, searches, and any function in general has complexity of Ο(lg(n)).

To use a different comparison than "a < b", pass a different operator string that can be used by std.functional.binaryFun, or pass in a function, delegate, functor, or any type where less(a, b) results in a bool value.

Note that less should produce a strict ordering. That is, for two unequal elements a and b, less(a, b) == !less(b, a). less(a, a) should always equal false.

If allowDuplicates is set to true, then inserting the same element more than once continues to add more elements. If it is false, duplicate elements are ignored on insertion. If duplicates are allowed, then new elements are inserted after all existing duplicate elements.
alias Elem = T;
Element type for the tree
alias Range = RBRange!(RBNode*);
alias ConstRange = RBRange!(const(RBNode)*);
alias ImmutableRange = RBRange!(immutable(RBNode)*);
The range types for RedBlackTree
@property bool empty();
Check if any elements exist in the container. Returns false if at least one element exists.
const @property size_t length();
Returns the number of elements in the container.

Complexity: Ο(1).

@property RedBlackTree dup();
Duplicate this container. The resulting container contains a shallow copy of the elements.

Complexity: Ο(n)

Range opSlice();
const ConstRange opSlice();
immutable ImmutableRange opSlice();
Fetch a range that spans all the elements in the container.

Complexity: Ο(1)

Elem front();
The front element in the container

Complexity: Ο(1)

Elem back();
The last element in the container

Complexity: Ο(log(n))

const bool opBinaryRight(string op)(Elem e) if (op == "in");
in operator. Check to see if the given element exists in the container.

Complexity: Ο(log(n))

bool opEquals(Object rhs);
Compares two trees for equality.

Complexity: Ο(n)

void clear();
Removes all elements from the container.

Complexity: Ο(1)

size_t stableInsert(Stuff)(Stuff stuff) if (isImplicitlyConvertible!(Stuff, Elem));
Insert a single element in the container. Note that this does not invalidate any ranges currently iterating the container.
Returns:
The number of elements inserted.

Complexity: Ο(log(n))

size_t stableInsert(Stuff)(Stuff stuff) if (isInputRange!Stuff && isImplicitlyConvertible!(ElementType!Stuff, Elem));
alias insert = stableInsert;
Insert a range of elements in the container. Note that this does not invalidate any ranges currently iterating the container.
Returns:
The number of elements inserted.

Complexity: Ο(m * log(n))

Elem removeAny();
Remove an element from the container and return its value.

Complexity: Ο(log(n))

void removeFront();
Remove the front element from the container.

Complexity: Ο(log(n))

void removeBack();
Remove the back element from the container.

Complexity: Ο(log(n))

Range remove(Range r);
Removes the given range from the container.
Returns:
A range containing all of the elements that were after the given range.

Complexity: Ο(m * log(n)) (where m is the number of elements in the range)

Range remove(Take!Range r);
Removes the given Take!Range from the container
Returns:
A range containing all of the elements that were after the given range.

Complexity: Ο(m * log(n)) (where m is the number of elements in the range)

size_t removeKey(U...)(U elems) if (allSatisfy!(isImplicitlyConvertibleToElem, U));
size_t removeKey(U)(U[] elems) if (isImplicitlyConvertible!(U, Elem));
size_t removeKey(Stuff)(Stuff stuff) if (isInputRange!Stuff && isImplicitlyConvertible!(ElementType!Stuff, Elem) && !isDynamicArray!Stuff);
Removes elements from the container that are equal to the given values according to the less comparator. One element is removed for each value given which is in the container. If allowDuplicates is true, duplicates are removed only if duplicate values are given.
Returns:
The number of elements removed.

Complexity: Ο(m log(n)) (where m is the number of elements to remove)

Example:

auto rbt = redBlackTree!true(0, 1, 1, 1, 4, 5, 7);
rbt.removeKey(1, 4, 7);
assert(equal(rbt[], [0, 1, 1, 5]));
rbt.removeKey(1, 1, 0);
assert(equal(rbt[], [5]));

Range upperBound(Elem e);
const ConstRange upperBound(Elem e);
immutable ImmutableRange upperBound(Elem e);
Get a range from the container with all elements that are > e according to the less comparator

Complexity: Ο(log(n))

Range lowerBound(Elem e);
const ConstRange lowerBound(Elem e);
immutable ImmutableRange lowerBound(Elem e);
Get a range from the container with all elements that are < e according to the less comparator

Complexity: Ο(log(n))

auto equalRange(this This)(Elem e);
Get a range from the container with all elements that are == e according to the less comparator

Complexity: Ο(log(n))

const void toString(scope void delegate(const(char)[]) sink, FormatSpec!char fmt);
Formats the RedBlackTree into a sink function. For more info see std.format.formatValue
this(Elem[] elems...);
Constructor. Pass in an array of elements, or individual elements to initialize the tree with.
this(Stuff)(Stuff stuff) if (isInputRange!Stuff && isImplicitlyConvertible!(ElementType!Stuff, Elem));
Constructor. Pass in a range of elements to initialize the tree with.
this();

auto redBlackTree(E)(E[] elems...);
auto redBlackTree(bool allowDuplicates, E)(E[] elems...);
auto redBlackTree(alias less, E)(E[] elems...);
auto redBlackTree(alias less, bool allowDuplicates, E)(E[] elems...) if (is(typeof(binaryFun!less(E.init, E.init))));
Convenience function for creating a RedBlackTree!E from a list of values.
Examples:
auto rbt1 = redBlackTree(0, 1, 5, 7);
auto rbt2 = redBlackTree!string("hello", "world");
auto rbt3 = redBlackTree!true(0, 1, 5, 7, 5);
auto rbt4 = redBlackTree!"a > b"(0, 1, 5, 7);
auto rbt5 = redBlackTree!("a > b", true)(0.1, 1.3, 5.9, 7.2, 5.9);