Skip to content

A maintained Swift heap package that provides min and max heaps in both iterative and recursive variants.

License

Notifications You must be signed in to change notification settings

swiftpackages/Heap

Repository files navigation

Heap for Swift

macOS ubuntu docs Codacy Badge

Heap

A maintained swift heap package that provides min and max heaps in both iterative and recursive variants.

Getting Started

You can easily add as a requirement with SwiftPM.

Know what you’re doing?

Here are some quick copypastas for you

.package(url: "https://github.com/swiftpackages/Heap.git", from: "1.2.0"),
.product(name: "Heap", package: "Heap"),

Need a reminder?

Your Package.swift file should look something like this

// swift-tools-version:5.3

import PackageDescription

let package = Package(
    name: "SuperCoolProject",
    products: [
        .library(
            name: "SuperCoolProject",
            targets: ["SuperCoolProject"]),
    ],
    dependencies: [
        .package(url: "https://github.com/swiftpackages/Heap.git", from: "1.2.0"),
    ],
    targets: [
        .target(
            name: "SuperCoolProject",
            dependencies: [
                .product(name: "Heap", package: "Heap"),
            ]),
        .testTarget(
            name: "SuperCoolProject",
            dependencies: ["SuperCoolProject"])
    ]
)

Usage

The datatype must conform to Comparable.

// initialize MinHeap<Int>
var minHeap = MinHeap<Int>()

// initialize MaxHeap<Int>
var maxHeap = MaxHeap<Int>()

// initialize MinHeapRecursive<Int>
var minHeap = MinHeapRecursive<Int>()

// initialize MaxHeapRecursive<Int>
var maxHeap = MaxHeapRecursive<Int>()

// get root node value without removing it
heap.peak()

// get root node value and remove it
heap.pull()

Iteration

With v1.2.0 Heap conforms to Sequence allowing you to iterate through the stored items.

var heap = MinHeap<Int>()

heap.add(2)
heap.add(1)
heap.add(3)

for value in heap {
    print(value)
}
/* 1
   2
   3 */

// heap can still be used as if it wasn't iterated over.
heap.pull() // 1
heap.pull() // 2
heap.pull() // 3

Additional Documentation

You can find the full documentation on the documentation website.

About

A maintained Swift heap package that provides min and max heaps in both iterative and recursive variants.

Topics

Resources

License

Security policy

Stars

Watchers

Forks

Sponsor this project

Packages

No packages published

Languages