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 a local clone.
This module provides a BinaryHeap (aka priority queue) adaptor that makes a binary heap out of any user-provided random-access range.
This module is a submodule of std.container.
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).
import std.algorithm.comparison : equal; import std.range : take; auto maxHeap = heapify([4, 7, 3, 1, 5]); assert(maxHeap.take(3).equal([7, 5, 4])); auto minHeap = heapify!"a > b"([4, 7, 3, 1, 5]); assert(minHeap.take(3).equal([1, 3, 4]));
BinaryHeap(Store, alias less = "a < b") if (isRandomAccessRange!Store || isRandomAccessRange!(typeof(Store.init)));
- Implements a binary heap container on top of a given random-access range type (usually T) or a random-access container type (usually Array!T). The documentation of
BinaryHeapwill refer to the underlying range or container as the store of the heap.The binary heap induces structure over the underlying store such that accessing the largest element (by using the front property) is a Ο(1) operation and extracting it (by using the removeFront() method) is done fast in Ο(log n) time. If less is the less-than operator, which is the default option, then
BinaryHeapdefines a so-called max-heap that optimizes extraction of the largest elements. To define a min-heap, instantiate
BinaryHeapwith "a > b" as its predicate. Simply extracting elements from a
BinaryHeapcontainer is tantamount to lazily fetching elements of Store in descending order. Extracting elements from the
BinaryHeapto completion leaves the underlying store sorted in ascending order but, again, yields elements in descending order. If Store is a range, the
BinaryHeapcannot grow beyond the size of that range. If Store is a container that supports insertBack, the
BinaryHeapmay grow by adding elements to the container.Examples:Example from "Introduction to Algorithms" Cormen et al, p 146
import std.algorithm.comparison : equal; int a = [ 4, 1, 3, 2, 16, 9, 10, 14, 8, 7 ]; auto h = heapify(a); // largest element assert(h.front == 16); // a has the heap property assert(equal(a, [ 16, 14, 10, 8, 7, 9, 3, 2, 4, 1 ]));Examples:
BinaryHeapimplements the standard input range interface, allowing lazy iteration of the underlying range in descending order.
import std.algorithm.comparison : equal; import std.range : take; int a = [4, 1, 3, 2, 16, 9, 10, 14, 8, 7]; auto top5 = heapify(a).take(5); assert(top5.equal([16, 14, 10, 9, 8]));
- Converts the store
sinto a heap. If
initialSizeis specified, only the first
sare transformed into a heap, after which the heap can grow up to r.length (if Store is a range) or indefinitely (if Store is a container with insertBack). Performs Ο(min(r.length,
initialSize)) evaluations of less.
- Takes ownership of a store. After this, manipulating
smay make the heap work incorrectly.
- Takes ownership of a store assuming it already was organized as a heap.
- Clears the heap. Returns the portion of the store from 0 up to length, which satisfies the heap property.
- @property bool
trueif the heap is empty,
- @property BinaryHeap
- Returns a duplicate of the heap. The underlying store must also support a
- @property size_t
- Returns the length of the heap.
- @property size_t
- Returns the capacity of the heap, which is the length of the underlying store (if the store is a range) or the capacity of the underlying store (if the store is a container).
- @property ElementType!Store
- Returns a copy of the front of the heap, which is the largest element according to less.
- Clears the heap by detaching it from the underlying store.
valueinto the store. If the underlying store is a range and length == capacity, throws an exception.
- Removes the largest element from the heap.
- Removes the largest element from the heap and returns a copy of it. The element still resides in the heap's store. For performance reasons you may want to use removeFront with heaps of objects that are expensive to copy.
- Replaces the largest element in the store with
- If the heap has room to grow, inserts
valueinto the store and returns
true. Otherwise, if less(
value, front), calls replaceFront(
value) and returns again
true. Otherwise, leaves the heap unaffected and returns
false. This method is useful in scenarios where the smallest k elements of a set of candidates must be collected.
- BinaryHeap!(Store, less)
heapify(alias less = "a < b", Store)(Store
- Convenience function that returns a BinaryHeap!Store object initialized with
2010- Andrei Alexandrescu. All rights reserved by the respective holders. | Page generated by Ddoc on Sat Jan 21 02:53:25 2017