https://github.com/simonwhitaker/swift-heap
An implementation of the heap data structure in Swift
https://github.com/simonwhitaker/swift-heap
Last synced: 12 months ago
JSON representation
An implementation of the heap data structure in Swift
- Host: GitHub
- URL: https://github.com/simonwhitaker/swift-heap
- Owner: simonwhitaker
- Created: 2017-07-30T11:48:08.000Z (almost 9 years ago)
- Default Branch: main
- Last Pushed: 2023-02-14T16:58:02.000Z (over 3 years ago)
- Last Synced: 2025-06-02T01:17:05.726Z (about 1 year ago)
- Language: Swift
- Size: 18.6 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# An implementation of the heap data stucture in Swift
## Usage
A heap over a type that conforms to Comparable is a min-heap by default.
``` swift
var h = Heap();
h.add(4);
h.add(9);
h.add(2);
let i = h.top! // i is 2
```
Pass a `comparator` argument to the constructor for custom behaviour. For example, to use a max heap:
``` swift
var h = Heap(comparator: >);
h.add(4);
h.add(9);
h.add(2);
let i = h.top! // i is 9
```
Or to order strings by length:
``` swift
var h = Heap { $0.length < $1.length };
h.add("foo");
h.add("bar");
h.add("bo");
let s = h.top! // s is "bo"
```