| 1 |
@safe pure unittest |
| 2 |
{ |
| 3 |
import std.container.rbtree; |
| 4 |
|
| 5 |
import std.algorithm.comparison : equal; |
| 6 |
import std.container.rbtree; |
| 7 |
|
| 8 |
auto rbt = redBlackTree(3, 1, 4, 2, 5); |
| 9 |
assert(rbt.front == 1); |
| 10 |
assert(equal(rbt[], [1, 2, 3, 4, 5])); |
| 11 |
|
| 12 |
rbt.removeKey(1, 4); |
| 13 |
assert(equal(rbt[], [2, 3, 5])); |
| 14 |
|
| 15 |
rbt.removeFront(); |
| 16 |
assert(equal(rbt[], [3, 5])); |
| 17 |
|
| 18 |
rbt.insert([1, 2, 4]); |
| 19 |
assert(equal(rbt[], [1, 2, 3, 4, 5])); |
| 20 |
|
| 21 |
// Query bounds in O(log(n)) |
| 22 |
assert(rbt.lowerBound(3).equal([1, 2])); |
| 23 |
assert(rbt.equalRange(3).equal([3])); |
| 24 |
assert(rbt.upperBound(3).equal([4, 5])); |
| 25 |
|
| 26 |
// A Red Black tree with the highest element at front: |
| 27 |
import std.range : iota; |
| 28 |
auto maxTree = redBlackTree!"a > b"(iota(5)); |
| 29 |
assert(equal(maxTree[], [4, 3, 2, 1, 0])); |
| 30 |
|
| 31 |
// adding duplicates will not add them, but return 0 |
| 32 |
auto rbt2 = redBlackTree(1, 3); |
| 33 |
assert(rbt2.insert(1) == 0); |
| 34 |
assert(equal(rbt2[], [1, 3])); |
| 35 |
assert(rbt2.insert(2) == 1); |
| 36 |
|
| 37 |
// however you can allow duplicates |
| 38 |
auto ubt = redBlackTree!true([0, 1, 0, 1]); |
| 39 |
assert(equal(ubt[], [0, 0, 1, 1])); |
| 40 |
} |
| 41 |
|
| 42 |
@safe pure unittest |
| 43 |
{ |
| 44 |
import std.container.rbtree; |
| 45 |
|
| 46 |
import std.range : iota; |
| 47 |
|
| 48 |
auto rbt1 = redBlackTree(0, 1, 5, 7); |
| 49 |
auto rbt2 = redBlackTree!string("hello", "world"); |
| 50 |
auto rbt3 = redBlackTree!true(0, 1, 5, 7, 5); |
| 51 |
auto rbt4 = redBlackTree!"a > b"(0, 1, 5, 7); |
| 52 |
auto rbt5 = redBlackTree!("a > b", true)(0.1, 1.3, 5.9, 7.2, 5.9); |
| 53 |
|
| 54 |
// also works with ranges |
| 55 |
auto rbt6 = redBlackTree(iota(3)); |
| 56 |
auto rbt7 = redBlackTree!true(iota(3)); |
| 57 |
auto rbt8 = redBlackTree!"a > b"(iota(3)); |
| 58 |
auto rbt9 = redBlackTree!("a > b", true)(iota(3)); |
| 59 |
} |
| 60 |
|