| 1 |
@system unittest |
| 2 |
{ |
| 3 |
import std.container.binaryheap; |
| 4 |
|
| 5 |
import std.algorithm.comparison : equal; |
| 6 |
import std.range : take; |
| 7 |
auto maxHeap = heapify([4, 7, 3, 1, 5]); |
| 8 |
assert(maxHeap.take(3).equal([7, 5, 4])); |
| 9 |
|
| 10 |
auto minHeap = heapify!"a > b"([4, 7, 3, 1, 5]); |
| 11 |
assert(minHeap.take(3).equal([1, 3, 4])); |
| 12 |
} |
| 13 |
|
| 14 |
@system unittest |
| 15 |
{ |
| 16 |
import std.container.binaryheap; |
| 17 |
|
| 18 |
import std.algorithm.comparison : equal; |
| 19 |
int[] a = [ 4, 1, 3, 2, 16, 9, 10, 14, 8, 7 ]; |
| 20 |
auto h = heapify(a); |
| 21 |
// largest element |
| 22 |
assert(h.front == 16); |
| 23 |
// a has the heap property |
| 24 |
assert(equal(a, [ 16, 14, 10, 8, 7, 9, 3, 2, 4, 1 ])); |
| 25 |
} |
| 26 |
|
| 27 |
@system unittest |
| 28 |
{ |
| 29 |
import std.container.binaryheap; |
| 30 |
|
| 31 |
import std.algorithm.comparison : equal; |
| 32 |
import std.range : take; |
| 33 |
int[] a = [4, 1, 3, 2, 16, 9, 10, 14, 8, 7]; |
| 34 |
auto top5 = heapify(a).take(5); |
| 35 |
assert(top5.equal([16, 14, 10, 9, 8])); |
| 36 |
} |
| 37 |
|
| 38 |
@system unittest |
| 39 |
{ |
| 40 |
import std.container.binaryheap; |
| 41 |
|
| 42 |
import std.conv : to; |
| 43 |
import std.range.primitives; |
| 44 |
{ |
| 45 |
// example from "Introduction to Algorithms" Cormen et al., p 146 |
| 46 |
int[] a = [ 4, 1, 3, 2, 16, 9, 10, 14, 8, 7 ]; |
| 47 |
auto h = heapify(a); |
| 48 |
h = heapify!"a < b"(a); |
| 49 |
assert(h.front == 16); |
| 50 |
assert(a == [ 16, 14, 10, 8, 7, 9, 3, 2, 4, 1 ]); |
| 51 |
auto witness = [ 16, 14, 10, 9, 8, 7, 4, 3, 2, 1 ]; |
| 52 |
for (; !h.empty; h.removeFront(), witness.popFront()) |
| 53 |
{ |
| 54 |
assert(!witness.empty); |
| 55 |
assert(witness.front == h.front); |
| 56 |
} |
| 57 |
assert(witness.empty); |
| 58 |
} |
| 59 |
{ |
| 60 |
int[] a = [ 4, 1, 3, 2, 16, 9, 10, 14, 8, 7 ]; |
| 61 |
int[] b = new int[a.length]; |
| 62 |
BinaryHeap!(int[]) h = BinaryHeap!(int[])(b, 0); |
| 63 |
foreach (e; a) |
| 64 |
{ |
| 65 |
h.insert(e); |
| 66 |
} |
| 67 |
assert(b == [ 16, 14, 10, 8, 7, 3, 9, 1, 4, 2 ], to!string(b)); |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
@system unittest |
| 72 |
{ |
| 73 |
import std.container.binaryheap; |
| 74 |
|
| 75 |
import std.stdio; |
| 76 |
import std.algorithm.comparison : equal; |
| 77 |
import std.container.binaryheap; |
| 78 |
|
| 79 |
int[] a = [ 4, 1, 3, 2, 16, 9, 10, 14, 8, 7 ]; |
| 80 |
auto h = heapify(a); |
| 81 |
|
| 82 |
// Internal representation of Binary Heap tree |
| 83 |
assert(a.equal([16, 14, 10, 8, 7, 9, 3, 2, 4, 1])); |
| 84 |
|
| 85 |
h.replaceFront(30); |
| 86 |
// Value 16 was replaced by 30 |
| 87 |
assert(a.equal([30, 14, 10, 8, 7, 9, 3, 2, 4, 1])); |
| 88 |
|
| 89 |
// Making changes to the Store will be seen in the Heap |
| 90 |
a[0] = 40; |
| 91 |
assert(h.front() == 40); |
| 92 |
|
| 93 |
// Inserting a new element will reallocate the Store, leaving |
| 94 |
// the original Store unchanged. |
| 95 |
h.insert(20); |
| 96 |
assert(a.equal([40, 14, 10, 8, 7, 9, 3, 2, 4, 1])); |
| 97 |
|
| 98 |
// Making changes to the original Store will not affect the Heap anymore |
| 99 |
a[0] = 60; |
| 100 |
assert(h.front() == 40); |
| 101 |
} |
| 102 |
|