This module implements a red-black tree container.
This module is a submodule of std.container
.
import std.algorithm.comparison : equal; import std.container.rbtree; auto rbt = redBlackTree(3, 1, 4, 2, 5); writeln(rbt.front); // 1 assert(equal(rbt[], [1, 2, 3, 4, 5])); rbt.removeKey(1, 4); assert(equal(rbt[], [2, 3, 5])); rbt.removeFront(); assert(equal(rbt[], [3, 5])); rbt.insert([1, 2, 4]); assert(equal(rbt[], [1, 2, 3, 4, 5])); // Query bounds in O(log(n)) assert(rbt.lowerBound(3).equal([1, 2])); assert(rbt.equalRange(3).equal([3])); assert(rbt.upperBound(3).equal([4, 5])); // A Red Black tree with the highest element at front: import std.range : iota; auto maxTree = redBlackTree!"a > b"(iota(5)); assert(equal(maxTree[], [4, 3, 2, 1, 0])); // adding duplicates will not add them, but return 0 auto rbt2 = redBlackTree(1, 3); writeln(rbt2.insert(1)); // 0 assert(equal(rbt2[], [1, 3])); writeln(rbt2.insert(2)); // 1 // however you can allow duplicates auto ubt = redBlackTree!true([0, 1, 0, 1]); assert(equal(ubt[], [0, 0, 1, 1]));
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.
Element type for the tree
The range types for RedBlackTree
Check if any elements exist in the container. Returns false
if at least one element exists.
Returns the number of elements in the container.
1
).Duplicate this container. The resulting container contains a shallow copy of the elements.
n
)
Fetch a range that spans all the elements in the container.
1
)
The front
element in the container
1
)
The last element in the container
log(n)
)
in
operator. Check to see if the given element exists in the container.
log(n)
)
Compares two trees for equality.
n
)
Removes all elements from the container.
1
)
Insert a single element in the container. Note that this does not invalidate any ranges currently iterating the container.
log(n)
)
Insert a range of elements in the container. Note that this does not invalidate any ranges currently iterating the container.
m * log(n)
)
Remove an element from the container and return its value.
log(n)
)
Remove the front element from the container.
log(n)
)
Remove the back element from the container.
log(n)
)
Removes the given range from the container.
m * log(n)
) (where m is the number of elements in the range)Removes the given Take!Range
from the container
m * log(n)
) (where m is the number of elements in the range)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.
m log(n)
) (where m is the number of elements to remove) 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]));
Get a range from the container with all elements that are > e
according to the less comparator
log(n)
)
Get a range from the container with all elements that are < e
according to the less comparator
log(n)
)
Get a range from the container with all elements that are == e
according to the less comparator
log(n)
)
Formats the RedBlackTree into a sink
function. For more info see std.format.formatValue
. Note that this only is available when the element type can be formatted. Otherwise, the default toString
from Object is used.
Constructor. Pass in an array of elements, or individual elements to initialize the tree with.
Constructor. Pass in a range of elements to initialize the tree with.
Convenience function for creating a RedBlackTree!E
from a list of values.
allowDuplicates | Whether duplicates should be allowed (optional, default: false ) |
less | predicate to sort by (optional) |
E[] elems
| elements to insert into the rbtree (variadic arguments) |
Stuff range
|
range elements to insert into the rbtree (alternative to elems ) |
import std.range : iota; 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); // also works with ranges auto rbt6 = redBlackTree(iota(3)); auto rbt7 = redBlackTree!true(iota(3)); auto rbt8 = redBlackTree!"a > b"(iota(3)); auto rbt9 = redBlackTree!("a > b", true)(iota(3));
© 1999–2017 The D Language Foundation
Licensed under the Boost License 1.0.
https://dlang.org/phobos/std_container_rbtree.html